{
“cells”: [
{

“cell_type”: “markdown”, “id”: “7aa3885c”, “metadata”: {}, “source”: [

“# Functions tutorialn”, “n”, “In astromodels functions can be used as spectral shapes for sources, or to describe time-dependence, phase-dependence, or links among parameters.n”, “n”, “To get the list of available functions just do:”

]

}, {

“cell_type”: “code”, “execution_count”: 1, “id”: “0f6e7bbc”, “metadata”: {

“execution”: {

“iopub.execute_input”: “2021-08-17T08:09:34.806773Z”, “iopub.status.busy”: “2021-08-17T08:09:34.804988Z”, “iopub.status.idle”: “2021-08-17T08:09:37.537036Z”, “shell.execute_reply”: “2021-08-17T08:09:37.537675Z”

}

}, “outputs”: [], “source”: [

“%%capturen”, “from astromodels import *

]

}, {

“cell_type”: “code”, “execution_count”: 2, “id”: “20619302”, “metadata”: {

“execution”: {

“iopub.execute_input”: “2021-08-17T08:09:37.555304Z”, “iopub.status.busy”: “2021-08-17T08:09:37.553959Z”, “iopub.status.idle”: “2021-08-17T08:09:37.560769Z”, “shell.execute_reply”: “2021-08-17T08:09:37.562040Z”

}

}, “outputs”: [

{
“data”: {
“text/html”: [

“<div><table id="table4547701904">n”, “<thead><tr><th>name</th><th>Description</th></tr></thead>n”, “<tr><td>Asymm_Gaussian_on_sphere</td><td>A bidimensional Gaussian function on a sphere (in spherical coordinates)n”, “see https://en.wikipedia.org/wiki/Gaussian_function#Two-dimensional_Gaussian_function</td></tr>n”, “<tr><td>Band</td><td>Band model from Band et al., 1993, parametrized with the peak energy</td></tr>n”, “<tr><td>Band_Calderone</td><td>The Band model from Band et al. 1993, implemented however in a way which reduces the covariances between the parameters (Calderone et al., MNRAS, 448, 403C, 2015)</td></tr>n”, “<tr><td>Band_grbm</td><td>Band model from Band et al., 1993, parametrized with the cutoff energy</td></tr>n”, “<tr><td>Blackbody</td><td>A blackbody function</td></tr>n”, “<tr><td>Broken_powerlaw</td><td>A broken power law function</td></tr>n”, “<tr><td>Cauchy</td><td>The Cauchy distribution</td></tr>n”, “<tr><td>Constant</td><td>Return k</td></tr>n”, “<tr><td>Continuous_injection_diffusion</td><td>Positron and electrons diffusing away from the accelerator</td></tr>n”, “<tr><td>Continuous_injection_diffusion_ellipse</td><td>Positron and electrons diffusing away from the accelerator</td></tr>n”, “<tr><td>…</td><td>…</td></tr>n”, “<tr><td>SmoothlyBrokenPowerLaw</td><td>A Smoothly Broken Power Law</td></tr>n”, “<tr><td>SpatialTemplate_2D</td><td>User input Spatial Template. Expected to be normalized to 1/sr</td></tr>n”, “<tr><td>StepFunction</td><td>A function which is constant on the interval lower_bound - upper_bound and 0 outside the interval. The extremes of the interval are counted as part of the interval.</td></tr>n”, “<tr><td>StepFunctionUpper</td><td>A function which is constant on the interval lower_bound - upper_bound and 0 outside the interval. The upper interval is open.</td></tr>n”, “<tr><td>Super_cutoff_powerlaw</td><td>A power law with a super-exponential cutoff</td></tr>n”, “<tr><td>TbAbs</td><td>Photometric absorption (Tbabs implementation), f(E) = exp(- NH * sigma(E)) contributed by Dominique Eckert</td></tr>n”, “<tr><td>TemplateModel</td><td>A template model</td></tr>n”, “<tr><td>Truncated_gaussian</td><td>A truncated Gaussian function defined on the interval between the lower_bound (a) and upper_bound (b)</td></tr>n”, “<tr><td>Uniform_prior</td><td>A function which is constant on the interval lower_bound - upper_bound and 0 outside the interval. The extremes of the interval are counted as part of the interval.</td></tr>n”, “<tr><td>WAbs</td><td>Photometric absorption (Wabs implementation), f(E) = exp(- NH * sigma(E)) contributed by Dominique Eckert</td></tr>n”, “<tr><td>_ComplexTestFunction</td><td>A useless function to be used during automatic tests</td></tr>n”, “</table></div>”

], “text/plain”: [

” name …n”, “————————————– …n”, ” Asymm_Gaussian_on_sphere …n”, ” Band …n”, ” Band_Calderone …n”, ” Band_grbm …n”, ” Blackbody …n”, ” Broken_powerlaw …n”, ” Cauchy …n”, ” Constant …n”, ” Continuous_injection_diffusion …n”, “Continuous_injection_diffusion_ellipse …n”, ” … …n”, ” SmoothlyBrokenPowerLaw …n”, ” SpatialTemplate_2D …n”, ” StepFunction …n”, ” StepFunctionUpper …n”, ” Super_cutoff_powerlaw …n”, ” TbAbs …n”, ” TemplateModel …n”, ” Truncated_gaussian …n”, ” Uniform_prior …n”, ” WAbs …n”, ” _ComplexTestFunction …”

]

}, “execution_count”: 2, “metadata”: {}, “output_type”: “execute_result”

}

], “source”: [

“list_functions()”

]

}, {

“cell_type”: “markdown”, “id”: “8fab12ce”, “metadata”: {}, “source”: [

“If you need more info about a function, you can obtain it by using:”

]

}, {

“cell_type”: “code”, “execution_count”: 3, “id”: “02032c31”, “metadata”: {

“execution”: {

“iopub.execute_input”: “2021-08-17T08:09:37.571237Z”, “iopub.status.busy”: “2021-08-17T08:09:37.570055Z”, “iopub.status.idle”: “2021-08-17T08:09:37.577023Z”, “shell.execute_reply”: “2021-08-17T08:09:37.577887Z”

}

}, “outputs”: [

{
“data”: {
“text/html”: [

“<ul>n”, “n”, “<li>description: A Gaussian function</li>n”, “n”, “<li>formula: $ K \frac{1}{\sigma \sqrt{2 \pi}}\exp{\frac{(x-\mu)^2}{2~(\sigma)^2}} $</li>n”, “n”, “<li>default parameters: n”, “<ul>n”, “n”, “<li>F: n”, “<ul>n”, “n”, “<li>value: 1.0</li>n”, “n”, “<li>desc: Integral between -inf and +inf. Fix this to 1 to obtain a Normal distribution</li>n”, “n”, “<li>min_value: None</li>n”, “n”, “<li>max_value: None</li>n”, “n”, “<li>unit: </li>n”, “n”, “<li>is_normalization: False</li>n”, “n”, “<li>delta: 0.1</li>n”, “n”, “<li>free: True</li>n”, “n”, “</ul>n”, “n”, “</li>n”, “n”, “<li>mu: n”, “<ul>n”, “n”, “<li>value: 0.0</li>n”, “n”, “<li>desc: Central value</li>n”, “n”, “<li>min_value: None</li>n”, “n”, “<li>max_value: None</li>n”, “n”, “<li>unit: </li>n”, “n”, “<li>is_normalization: False</li>n”, “n”, “<li>delta: 0.1</li>n”, “n”, “<li>free: True</li>n”, “n”, “</ul>n”, “n”, “</li>n”, “n”, “<li>sigma: n”, “<ul>n”, “n”, “<li>value: 1.0</li>n”, “n”, “<li>desc: standard deviation</li>n”, “n”, “<li>min_value: 1e-12</li>n”, “n”, “<li>max_value: None</li>n”, “n”, “<li>unit: </li>n”, “n”, “<li>is_normalization: False</li>n”, “n”, “<li>delta: 0.1</li>n”, “n”, “<li>free: True</li>n”, “n”, “</ul>n”, “n”, “</li>n”, “n”, “</ul>n”, “n”, “</li>n”, “n”, “</ul>n”

], “text/plain”: [

“<IPython.core.display.HTML object>”

]

}, “metadata”: {}, “output_type”: “display_data”

}

], “source”: [

“Gaussian.info()”

]

}, {

“cell_type”: “markdown”, “id”: “9d751244”, “metadata”: {}, “source”: [

“Note that you don’t need to create an instance in order to call the info() method.”

]

}, {

“cell_type”: “markdown”, “id”: “7a9a8e91”, “metadata”: {}, “source”: [

“## Creating functionsn”, “n”, “Functions can be created in two different ways. We can create an instance with the default values for the parameters like this:”

]

}, {

“cell_type”: “code”, “execution_count”: 4, “id”: “1c2771e4”, “metadata”: {

“execution”: {

“iopub.execute_input”: “2021-08-17T08:09:37.585232Z”, “iopub.status.busy”: “2021-08-17T08:09:37.584241Z”, “iopub.status.idle”: “2021-08-17T08:09:37.591904Z”, “shell.execute_reply”: “2021-08-17T08:09:37.592724Z”

}

}, “outputs”: [], “source”: [

“powerlaw_instance = Powerlaw()”

]

}, {

“cell_type”: “markdown”, “id”: “8b8548d0”, “metadata”: {}, “source”: [

“or we can specify on construction specific values for the parameters:”

]

}, {

“cell_type”: “code”, “execution_count”: 5, “id”: “4ab7be35”, “metadata”: {

“execution”: {

“iopub.execute_input”: “2021-08-17T08:09:37.600992Z”, “iopub.status.busy”: “2021-08-17T08:09:37.599323Z”, “iopub.status.idle”: “2021-08-17T08:09:37.602185Z”, “shell.execute_reply”: “2021-08-17T08:09:37.605777Z”

}

}, “outputs”: [], “source”: [

“powerlaw_instance = Powerlaw(K=0.01, index=-2.2)”

]

}, {

“cell_type”: “markdown”, “id”: “d06c4d30”, “metadata”: {}, “source”: [

“If you don’t remember the names of the parameters just call the .info() method as in powerlaw.info() as demonstrated above.”

]

}, {

“cell_type”: “markdown”, “id”: “3d3ecfc3”, “metadata”: {}, “source”: [

“## Getting information about an instancen”, “n”, “Using the `.display()` method we get a representation of the instance which exploits the features of the environment we are using. If we are running inside a IPython notebook, a rich representation with the formula of the function will be displayed (if available). Otherwise, in a normal terminal, the latex formula will not be rendered:”

]

}, {

“cell_type”: “code”, “execution_count”: 6, “id”: “0dc229a9”, “metadata”: {

“execution”: {

“iopub.execute_input”: “2021-08-17T08:09:37.616218Z”, “iopub.status.busy”: “2021-08-17T08:09:37.614850Z”, “iopub.status.idle”: “2021-08-17T08:09:37.622708Z”, “shell.execute_reply”: “2021-08-17T08:09:37.623748Z”

}

}, “outputs”: [

{
“data”: {
“text/html”: [

“<ul>n”, “n”, “<li>description: A simple power-law</li>n”, “n”, “<li>formula: $ K~\frac{x}{piv}^{index} $</li>n”, “n”, “<li>parameters: n”, “<ul>n”, “n”, “<li>K: n”, “<ul>n”, “n”, “<li>value: 0.01</li>n”, “n”, “<li>desc: Normalization (differential flux at the pivot value)</li>n”, “n”, “<li>min_value: 1e-30</li>n”, “n”, “<li>max_value: 1000.0</li>n”, “n”, “<li>unit: </li>n”, “n”, “<li>is_normalization: True</li>n”, “n”, “<li>delta: 0.1</li>n”, “n”, “<li>free: True</li>n”, “n”, “</ul>n”, “n”, “</li>n”, “n”, “<li>piv: n”, “<ul>n”, “n”, “<li>value: 1.0</li>n”, “n”, “<li>desc: Pivot value</li>n”, “n”, “<li>min_value: None</li>n”, “n”, “<li>max_value: None</li>n”, “n”, “<li>unit: </li>n”, “n”, “<li>is_normalization: False</li>n”, “n”, “<li>delta: 0.1</li>n”, “n”, “<li>free: False</li>n”, “n”, “</ul>n”, “n”, “</li>n”, “n”, “<li>index: n”, “<ul>n”, “n”, “<li>value: -2.2</li>n”, “n”, “<li>desc: Photon index</li>n”, “n”, “<li>min_value: -10.0</li>n”, “n”, “<li>max_value: 10.0</li>n”, “n”, “<li>unit: </li>n”, “n”, “<li>is_normalization: False</li>n”, “n”, “<li>delta: 0.20099999999999998</li>n”, “n”, “<li>free: True</li>n”, “n”, “</ul>n”, “n”, “</li>n”, “n”, “</ul>n”, “n”, “</li>n”, “n”, “</ul>n”

], “text/plain”: [

” * description: A simple power-lawn”, ” * formula: $ K~\frac{x}{piv}^{index} $n”, ” * parameters:n”, ” * K:n”, ” * value: 0.01n”, ” * desc: Normalization (differential flux at the pivot value)n”, ” * min_value: 1.0e-30n”, ” * max_value: 1000.0n”, ” * unit: ‘’n”, ” * is_normalization: truen”, ” * delta: 0.1n”, ” * free: truen”, ” * piv:n”, ” * value: 1.0n”, ” * desc: Pivot valuen”, ” * min_value: nulln”, ” * max_value: nulln”, ” * unit: ‘’n”, ” * is_normalization: falsen”, ” * delta: 0.1n”, ” * free: falsen”, ” * index:n”, ” * value: -2.2n”, ” * desc: Photon indexn”, ” * min_value: -10.0n”, ” * max_value: 10.0n”, ” * unit: ‘’n”, ” * is_normalization: falsen”, ” * delta: 0.20099999999999998n”, ” * free: true”

]

}, “metadata”: {}, “output_type”: “display_data”

}

], “source”: [

“powerlaw_instance.display()”

]

}, {

“cell_type”: “markdown”, “id”: “4c791123”, “metadata”: {}, “source”: [

“It is also possible to get the text-only representation by simply printing the object like this:”

]

}, {

“cell_type”: “code”, “execution_count”: 7, “id”: “d6aad4e5”, “metadata”: {

“execution”: {

“iopub.execute_input”: “2021-08-17T08:09:37.632777Z”, “iopub.status.busy”: “2021-08-17T08:09:37.631583Z”, “iopub.status.idle”: “2021-08-17T08:09:37.637370Z”, “shell.execute_reply”: “2021-08-17T08:09:37.637982Z”

}

}, “outputs”: [

{

“name”: “stdout”, “output_type”: “stream”, “text”: [

” * description: A simple power-lawn”, ” * formula: $ K~\frac{x}{piv}^{index} $n”, ” * parameters:n”, ” * K:n”, ” * value: 0.01n”, ” * desc: Normalization (differential flux at the pivot value)n”, ” * min_value: 1.0e-30n”, ” * max_value: 1000.0n”, ” * unit: ‘’n”, ” * is_normalization: truen”, ” * delta: 0.1n”, ” * free: truen”, ” * piv:n”, ” * value: 1.0n”, ” * desc: Pivot valuen”, ” * min_value: nulln”, ” * max_value: nulln”, ” * unit: ‘’n”, ” * is_normalization: falsen”, ” * delta: 0.1n”, ” * free: falsen”, ” * index:n”, ” * value: -2.2n”, ” * desc: Photon indexn”, ” * min_value: -10.0n”, ” * max_value: 10.0n”, ” * unit: ‘’n”, ” * is_normalization: falsen”, ” * delta: 0.20099999999999998n”, ” * free: truen”, “n”

]

}

], “source”: [

“print(powerlaw_instance)”

]

}, {

“cell_type”: “markdown”, “id”: “c727e4ac”, “metadata”: {}, “source”: [

“<div class="alert alert-info">n”, “n”, “Note: the `.display()` method of an instance displays the current values of the parameters, while the .info() method demonstrated above (for which you don’t need an instance) displays the default values of the parameters.n”, “n”, “</div>n”, “n”

]

}, {

“cell_type”: “markdown”, “id”: “704facff”, “metadata”: {}, “source”: [

“## Modifying parametersn”, “n”, “Modifying a parameter of a function is easy:”

]

}, {

“cell_type”: “code”, “execution_count”: 8, “id”: “7c1a4e0d”, “metadata”: {

“execution”: {

“iopub.execute_input”: “2021-08-17T08:09:37.647521Z”, “iopub.status.busy”: “2021-08-17T08:09:37.646410Z”, “iopub.status.idle”: “2021-08-17T08:09:37.656718Z”, “shell.execute_reply”: “2021-08-17T08:09:37.657470Z”

}

}, “outputs”: [

{
“data”: {
“text/html”: [

“Parameter K = 1.2 []n”, “(min_value = 0.5, max_value = 15.0, delta = 0.25, free = True)”

], “text/plain”: [

“Parameter K = 1.2 []n”, “(min_value = 0.5, max_value = 15.0, delta = 0.25, free = True)”

]

}, “metadata”: {}, “output_type”: “display_data”

}

], “source”: [

“# Modify current valuen”, “n”, “powerlaw_instance.K = 1.2n”, “n”, “# Modify minimumn”, “powerlaw_instance.K.min_value = 0.5n”, “n”, “# Modify maximumn”, “powerlaw_instance.K.max_value = 15n”, “n”, “# We can also modify minimum and maximum at the same timen”, “powerlaw_instance.K.bounds = (0.5, 15)n”, “n”, “# Modifying the delta for the parametern”, “# (which can be used by downstream software for fitting, for example)n”, “powerlaw_instance.K.delta = 0.25n”, “n”, “# Fix the parametern”, “powerlaw_instance.K.fix = Truen”, “n”, “# or equivalentlyn”, “powerlaw_instance.K.free = Falsen”, “n”, “# Free it againn”, “powerlaw_instance.K.fix = Falsen”, “n”, “# or equivalentlyn”, “powerlaw_instance.K.free = Truen”, “n”, “# We can verify what we just did by printing again the whole function as shown above,n”, “# or simply printing the parameter:n”, “powerlaw_instance.K.display()”

]

}, {

“cell_type”: “markdown”, “id”: “26f025ee”, “metadata”: {}, “source”: [

“## Using physical unitsn”, “n”, “Astromodels uses the facility defined in astropy.units to make easier to convert between units during interactive analysis, when assigning to parameters. In order for functions to be aware of their units, they must be part of a `Source. Let’s create one:”

]

}, {

“cell_type”: “code”, “execution_count”: 9, “id”: “69097927”, “metadata”: {

“execution”: {

“iopub.execute_input”: “2021-08-17T08:09:37.668248Z”, “iopub.status.busy”: “2021-08-17T08:09:37.666870Z”, “iopub.status.idle”: “2021-08-17T08:09:37.669903Z”, “shell.execute_reply”: “2021-08-17T08:09:37.671029Z”

}

}, “outputs”: [], “source”: [

“powerlaw_instance = Powerlaw()n”, “n”, “point_source = PointSource("my_point_source",ra=0,dec=0, spectral_shape=powerlaw_instance)”

]

}, {

“cell_type”: “markdown”, “id”: “b5d41c04”, “metadata”: {}, “source”: [

“Now we can see the units”

]

}, {

“cell_type”: “code”, “execution_count”: 10, “id”: “769a58d5”, “metadata”: {

“execution”: {

“iopub.execute_input”: “2021-08-17T08:09:37.681581Z”, “iopub.status.busy”: “2021-08-17T08:09:37.680587Z”, “iopub.status.idle”: “2021-08-17T08:09:37.685412Z”, “shell.execute_reply”: “2021-08-17T08:09:37.686027Z”

}

}, “outputs”: [

{
“data”: {
“text/html”: [

“<ul>n”, “n”, “<li>description: A simple power-law</li>n”, “n”, “<li>formula: $ K~\frac{x}{piv}^{index} $</li>n”, “n”, “<li>parameters: n”, “<ul>n”, “n”, “<li>K: n”, “<ul>n”, “n”, “<li>value: 1.0</li>n”, “n”, “<li>desc: Normalization (differential flux at the pivot value)</li>n”, “n”, “<li>min_value: 1e-30</li>n”, “n”, “<li>max_value: 1000.0</li>n”, “n”, “<li>unit: keV-1 s-1 cm-2</li>n”, “n”, “<li>is_normalization: True</li>n”, “n”, “<li>delta: 0.1</li>n”, “n”, “<li>free: True</li>n”, “n”, “</ul>n”, “n”, “</li>n”, “n”, “<li>piv: n”, “<ul>n”, “n”, “<li>value: 1.0</li>n”, “n”, “<li>desc: Pivot value</li>n”, “n”, “<li>min_value: None</li>n”, “n”, “<li>max_value: None</li>n”, “n”, “<li>unit: keV</li>n”, “n”, “<li>is_normalization: False</li>n”, “n”, “<li>delta: 0.1</li>n”, “n”, “<li>free: False</li>n”, “n”, “</ul>n”, “n”, “</li>n”, “n”, “<li>index: n”, “<ul>n”, “n”, “<li>value: -2.01</li>n”, “n”, “<li>desc: Photon index</li>n”, “n”, “<li>min_value: -10.0</li>n”, “n”, “<li>max_value: 10.0</li>n”, “n”, “<li>unit: </li>n”, “n”, “<li>is_normalization: False</li>n”, “n”, “<li>delta: 0.20099999999999998</li>n”, “n”, “<li>free: True</li>n”, “n”, “</ul>n”, “n”, “</li>n”, “n”, “</ul>n”, “n”, “</li>n”, “n”, “</ul>n”

], “text/plain”: [

” * description: A simple power-lawn”, ” * formula: $ K~\frac{x}{piv}^{index} $n”, ” * parameters:n”, ” * K:n”, ” * value: 1.0n”, ” * desc: Normalization (differential flux at the pivot value)n”, ” * min_value: 1.0e-30n”, ” * max_value: 1000.0n”, ” * unit: keV-1 s-1 cm-2n”, ” * is_normalization: truen”, ” * delta: 0.1n”, ” * free: truen”, ” * piv:n”, ” * value: 1.0n”, ” * desc: Pivot valuen”, ” * min_value: nulln”, ” * max_value: nulln”, ” * unit: keVn”, ” * is_normalization: falsen”, ” * delta: 0.1n”, ” * free: falsen”, ” * index:n”, ” * value: -2.01n”, ” * desc: Photon indexn”, ” * min_value: -10.0n”, ” * max_value: 10.0n”, ” * unit: ‘’n”, ” * is_normalization: falsen”, ” * delta: 0.20099999999999998n”, ” * free: true”

]

}, “metadata”: {}, “output_type”: “display_data”

}

], “source”: [

“powerlaw_instance.display()”

]

}, {

“cell_type”: “code”, “execution_count”: 11, “id”: “4d91314e”, “metadata”: {

“execution”: {

“iopub.execute_input”: “2021-08-17T08:09:37.693570Z”, “iopub.status.busy”: “2021-08-17T08:09:37.691559Z”, “iopub.status.idle”: “2021-08-17T08:09:37.699479Z”, “shell.execute_reply”: “2021-08-17T08:09:37.700032Z”

}

}, “outputs”: [

{
“data”: {
“text/latex”: [

“$\mathrm{keV}$”

], “text/plain”: [

“Unit("keV")”

]

}, “execution_count”: 11, “metadata”: {}, “output_type”: “execute_result”

}

], “source”: [

“powerlaw_instance.x_unit”

]

}, {

“cell_type”: “code”, “execution_count”: 12, “id”: “df09d29d”, “metadata”: {

“execution”: {

“iopub.execute_input”: “2021-08-17T08:09:37.706216Z”, “iopub.status.busy”: “2021-08-17T08:09:37.705124Z”, “iopub.status.idle”: “2021-08-17T08:09:37.713478Z”, “shell.execute_reply”: “2021-08-17T08:09:37.714426Z”

}

}, “outputs”: [

{
“data”: {
“text/latex”: [

“$\mathrm{\frac{1}{keV\,s\,cm^{2}}}$”

], “text/plain”: [

“Unit("1 / (cm2 keV s)")”

]

}, “execution_count”: 12, “metadata”: {}, “output_type”: “execute_result”

}

], “source”: [

“powerlaw_instance.y_unit”

]

}, {

“cell_type”: “code”, “execution_count”: 13, “id”: “05399db7”, “metadata”: {

“execution”: {

“iopub.execute_input”: “2021-08-17T08:09:37.725282Z”, “iopub.status.busy”: “2021-08-17T08:09:37.724275Z”, “iopub.status.idle”: “2021-08-17T08:09:37.732383Z”, “shell.execute_reply”: “2021-08-17T08:09:37.733197Z”

}

}, “outputs”: [

{

“name”: “stdout”, “output_type”: “stream”, “text”: [

“Parameter K = 0.1223 [1 / (cm2 keV s)]n”, “(min_value = 1e-30, max_value = 1000.0, delta = 0.1, free = True)n”, “Parameter K = 1.2230000000000013e-08 [1 / (cm2 keV s)]n”, “(min_value = 1e-30, max_value = 1000.0, delta = 0.1, free = True)n”

]

}

], “source”: [

“import astropy.units as un”, “n”, “# Express the differential flux at the pivot energy in 1 / (MeV cm2 s)n”, “n”, “powerlaw_instance.K = (122.3 / (u.MeV * u.cm * u.cm * u.s))n”, “n”, “print(powerlaw_instance.K)n”, “n”, “# Express the differential flux at the pivot energy in 1 / (GeV m2 s)n”, “powerlaw_instance.K = (122.3 / (u.GeV * u.m * u.m * u.s))n”, “n”, “n”, “print(powerlaw_instance.K)”

]

}, {

“cell_type”: “markdown”, “id”: “a5f9a78c”, “metadata”: {}, “source”: [

“We see that astromodels does the unit conversion for us in the background!n”, “n”, “However, astropy units are very slow and we would not want to deal with them or set parameters with units during a fit. Thus, if you do not specify units when setting a parameter, the value is assumed to have the units specified in the construction of the function. “

]

}, {

“cell_type”: “code”, “execution_count”: 14, “id”: “fc75adb3”, “metadata”: {

“execution”: {

“iopub.execute_input”: “2021-08-17T08:09:37.899583Z”, “iopub.status.busy”: “2021-08-17T08:09:37.807417Z”, “iopub.status.idle”: “2021-08-17T08:09:52.215700Z”, “shell.execute_reply”: “2021-08-17T08:09:52.216664Z”

}

}, “outputs”: [

{

“name”: “stdout”, “output_type”: “stream”, “text”: [

“17.8 µs ± 220 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)n”

]

}

], “source”: [

“%timeit powerlaw_instance.K = 1”

]

}, {

“cell_type”: “code”, “execution_count”: 15, “id”: “d3156f0e”, “metadata”: {

“execution”: {

“iopub.execute_input”: “2021-08-17T08:09:52.563914Z”, “iopub.status.busy”: “2021-08-17T08:09:52.381432Z”, “iopub.status.idle”: “2021-08-17T08:09:54.979674Z”, “shell.execute_reply”: “2021-08-17T08:09:54.980311Z”

}

}, “outputs”: [

{

“name”: “stdout”, “output_type”: “stream”, “text”: [

“339 µs ± 5.38 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)n”

]

}

], “source”: [

“%timeit powerlaw_instance.K = (122.3 / (u.MeV * u.cm * u.cm * u.s))”

]

}, {

“cell_type”: “markdown”, “id”: “77372262”, “metadata”: {}, “source”: [

“As you can see using astropy.units requires about 10x more than using a plain assignment. In an interactive analysis you are unlikely to notice the difference, but if you use units in a loop or during a fit this slow-down will add up an become very noticeable. Note that this is a feature of astropy.units, not of astromodels.

]

}, {

“cell_type”: “markdown”, “id”: “2cc339bd”, “metadata”: {}, “source”: [

“## Composing functionsn”, “n”, “We can create arbitrary complex functions by combining “primitive” functions using the normal math operators:”

]

}, {

“cell_type”: “code”, “execution_count”: 16, “id”: “a1ac2d29”, “metadata”: {

“execution”: {

“iopub.execute_input”: “2021-08-17T08:09:54.987236Z”, “iopub.status.busy”: “2021-08-17T08:09:54.986255Z”, “iopub.status.idle”: “2021-08-17T08:09:54.990168Z”, “shell.execute_reply”: “2021-08-17T08:09:54.990803Z”

}

}, “outputs”: [

{

“name”: “stdout”, “output_type”: “stream”, “text”: [

“(Gaussian{1} + Powerlaw{2})n”

]

}

], “source”: [

“composite = Gaussian() + Powerlaw()n”, “n”, “# Instead of the usual .display(), which would print all the many parameters,n”, “# let’s print just the description of the new composite functions:n”, “print(composite.description)”

]

}, {

“cell_type”: “markdown”, “id”: “2e483ffb”, “metadata”: {}, “source”: [

“These expressions can be as complex as needed. For example:”

]

}, {

“cell_type”: “code”, “execution_count”: 17, “id”: “a0515b1c”, “metadata”: {

“execution”: {

“iopub.execute_input”: “2021-08-17T08:09:54.998327Z”, “iopub.status.busy”: “2021-08-17T08:09:54.997365Z”, “iopub.status.idle”: “2021-08-17T08:09:55.004636Z”, “shell.execute_reply”: “2021-08-17T08:09:55.005245Z”

}

}, “outputs”: [

{

“name”: “stdout”, “output_type”: “stream”, “text”: [

“((Sin{1} * 3) + (((Powerlaw{2} ** 2) * (Gaussian{3} + 5)) / 3.0))n”

]

}

], “source”: [

“crazy_function = 3 * Sin() + Powerlaw()**2 * (5+Gaussian()) / 3.0n”, “n”, “print(crazy_function.description)”

]

}, {

“cell_type”: “markdown”, “id”: “a6e31315”, “metadata”: {}, “source”: [

“The numbers between `{}` enumerate the unique functions which constitute a composite function. This is useful because composite functions can be created starting from pre-existing instances of functions, in which case the same instance can be used more than once. For example:”

]

}, {

“cell_type”: “code”, “execution_count”: 18, “id”: “23a20a4d”, “metadata”: {

“execution”: {

“iopub.execute_input”: “2021-08-17T08:09:55.012686Z”, “iopub.status.busy”: “2021-08-17T08:09:55.011650Z”, “iopub.status.idle”: “2021-08-17T08:09:55.015073Z”, “shell.execute_reply”: “2021-08-17T08:09:55.016076Z”

}

}, “outputs”: [

{

“name”: “stdout”, “output_type”: “stream”, “text”: [

“((Powerlaw{1} * 2) + ((Powerlaw{1} + 3) * Sin{2}))n”

]

}

], “source”: [

“a_powerlaw = Powerlaw()n”, “a_sin = Sin()n”, “n”, “another_composite = 2 * a_powerlaw + (3 + a_powerlaw) * a_sinn”, “n”, “print(another_composite.description)”

]

}, {

“cell_type”: “markdown”, “id”: “a11d4b81”, “metadata”: {}, “source”: [

“In this case the same instance of a power law has been used twice. Changing the value of the parameters for “a_powerlaw” will affect also the second part of the expression. Instead, by doing this:”

]

}, {

“cell_type”: “code”, “execution_count”: 19, “id”: “2dabe87c”, “metadata”: {

“execution”: {

“iopub.execute_input”: “2021-08-17T08:09:55.024001Z”, “iopub.status.busy”: “2021-08-17T08:09:55.022913Z”, “iopub.status.idle”: “2021-08-17T08:09:55.026762Z”, “shell.execute_reply”: “2021-08-17T08:09:55.028582Z”

}

}, “outputs”: [

{

“name”: “stdout”, “output_type”: “stream”, “text”: [

“((Powerlaw{1} * 2) + ((Powerlaw{2} + 3) * Sin{3}))n”

]

}

], “source”: [

“another_composite2 = 2 * Powerlaw() + (3 + Powerlaw()) * Sin()n”, “n”, “print(another_composite2.description)”

]

}, {

“cell_type”: “markdown”, “id”: “4465ee2e”, “metadata”: {}, “source”: [

“we will end up with two independent sets of parameters for the two power laws. The difference can be seen immediately from the number of parameters of the two composite functions:”

]

}, {

“cell_type”: “code”, “execution_count”: 20, “id”: “1a17b152”, “metadata”: {

“execution”: {

“iopub.execute_input”: “2021-08-17T08:09:55.034812Z”, “iopub.status.busy”: “2021-08-17T08:09:55.033792Z”, “iopub.status.idle”: “2021-08-17T08:09:55.037594Z”, “shell.execute_reply”: “2021-08-17T08:09:55.038479Z”

}

}, “outputs”: [

{

“name”: “stdout”, “output_type”: “stream”, “text”: [

“6n”, “9n”

]

}

], “source”: [

“print(len(another_composite.parameters)) # 6 parametersn”, “print(len(another_composite2.parameters)) # 9 parameters”

]

}, {

“cell_type”: “markdown”, “id”: “68db1ab8”, “metadata”: {}, “source”: [

“## Creating custom functions”

]

}, {

“cell_type”: “code”, “execution_count”: null, “id”: “36538b7b”, “metadata”: {}, “outputs”: [], “source”: []

}

], “metadata”: {

“jupytext”: {

“formats”: “ipynb,md”

}, “kernelspec”: {

“display_name”: “Python 3”, “language”: “python”, “name”: “python3”

}, “language_info”: {

“codemirror_mode”: {

“name”: “ipython”, “version”: 3

}, “file_extension”: “.py”, “mimetype”: “text/x-python”, “name”: “python”, “nbconvert_exporter”: “python”, “pygments_lexer”: “ipython3”, “version”: “3.7.11”

}

}, “nbformat”: 4, “nbformat_minor”: 5

}