Skip to content

Commit

Permalink
continued document tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
byrdie committed Dec 4, 2024
1 parent 424b25c commit 350510c
Showing 1 changed file with 43 additions and 41 deletions.
84 changes: 43 additions & 41 deletions docs/tutorials/PolynomialFunctionArray.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"cell_type": "raw",
"id": "33c91c6520a84dfb",
"metadata": {
"editable": true,
"raw_mimetype": "text/restructuredtext",
"slideshow": {
"slide_type": ""
Expand All @@ -21,46 +22,46 @@
},
{
"cell_type": "code",
"execution_count": null,
"id": "887eedc318fabcad",
"metadata": {
"editable": true,
"slideshow": {
"slide_type": ""
},
"tags": []
},
"outputs": [],
"source": [
"import numpy as np\n",
"import named_arrays as na\n",
"import astropy.units as u\n",
"import astropy.visualization\n",
"\n",
"astropy.visualization.quantity_support();"
]
],
"outputs": [],
"execution_count": null
},
{
"cell_type": "raw",
"id": "971456c65a8ec009",
"metadata": {
"editable": true,
"raw_mimetype": "text/restructuredtext",
"slideshow": {
"slide_type": ""
},
"tags": []
},
"source": [
":class:`PolynomialFitFunctionArrays` are defined similarly to :class:`FunctionArrays` by a set of `inputs` (indepent variables) and `outputs` (dependent variables), but with additional parameters `degree`, `axis_polynomial`, and `components_polynomial` used to fit a polynomial function to the function.\n",
":class:`named_arrays.PolynomialFitFunctionArrays` are defined similarly to :class:`named_arrays.FunctionArrays` by a set of `inputs` (indepent variables) and `outputs` (dependent variables), but with additional parameters `degree`, `axis_polynomial`, and `components_polynomial` used to fit a polynomial function to the function.\n",
"\n",
"Start by defining a three-dimensional input grid dependent on wavelength, position.x, and position.y."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1393884e560bc1b9",
"metadata": {},
"outputs": [],
"source": [
"inputs = na.SpectralPositionalVectorLinearSpace(\n",
" start=na.SpectralPositionalVectorArray(\n",
Expand Down Expand Up @@ -94,33 +95,33 @@
")\n",
"\n",
"inputs"
]
],
"outputs": [],
"execution_count": null
},
{
"cell_type": "raw",
"id": "a8183a186530ddb0",
"metadata": {
"editable": true,
"raw_mimetype": "",
"slideshow": {
"slide_type": ""
},
"tags": []
},
"source": [
"Using the defined inputs, we calculate a second degree polynomial in ``outputs.x`` and ``outputs.y`` with 6 total non-zero coefficients. Each `x` and `y` has a constant offset, one linear term and one quadratic term. Coefficients c and d depend on both time and wavlength."
]
"source": "Using the defined inputs, we calculate a second degree polynomial in ``outputs.x`` and ``outputs.y`` with 6 total non-zero coefficients. Each `x` and `y` has a constant offset, one linear term and one quadratic term. Coefficients c and d depend on both time and wavlength."
},
{
"cell_type": "code",
"execution_count": null,
"id": "3734706d26357898",
"metadata": {
"editable": true,
"slideshow": {
"slide_type": ""
},
"tags": []
},
"outputs": [],
"source": [
"t = na.ScalarLinearSpace(-10 * u.s, 10 * u.s, num=3, axis='time')\n",
"\n",
Expand All @@ -133,28 +134,27 @@
" x=1 * u.mm + a * inputs.position.x + b * inputs.position.y ** 2 ,\n",
" y=5 * u.mm + c * inputs.position.y + d * inputs.position.x ** 2,\n",
")"
]
],
"outputs": [],
"execution_count": null
},
{
"cell_type": "raw",
"id": "ccc6d512e02a30a",
"metadata": {
"editable": true,
"raw_mimetype": "text/restructuredtext",
"slideshow": {
"slide_type": ""
},
"tags": []
},
"source": [
"Defining a :class:`PolynomialFitFunctionArray` of degree 2 with components ``position.x`` and ``position.y`` and `axis_polynomial` ``x`` and ``y`` will fit a second degree polynomial to the inputs and outputs. ``fit.coefficients`` gives the coefficients of the ordinary (linear?) least squares fit to the inputs and outputs. The fit coefficients match those used to create the outputs to machine preceision."
]
"source": "Defining a :class:`named_arrays.PolynomialFitFunctionArray` of degree 2 with components :python:`position.x` and :python:`position.y` and `axis_polynomial` :python:`\"x\"` and :python:`\"y\"` will fit a second degree polynomial to the inputs and outputs. :python:`fit.coefficients` gives the coefficients of the ordinary (linear?) least squares fit to the inputs and outputs. The fit coefficients match those used to create the outputs to machine preceision."
},
{
"cell_type": "code",
"execution_count": null,
"id": "b736fe5de8bf2d7e",
"metadata": {},
"outputs": [],
"source": [
"fit = na.PolynomialFitFunctionArray(\n",
" inputs=inputs,\n",
Expand All @@ -169,43 +169,46 @@
"print(coefficients.components['position.y*position.y'].x-b)\n",
"print(coefficients.components['position.y'].y-c)\n",
"print(coefficients.components['position.x*position.x'].y-d)"
]
],
"outputs": [],
"execution_count": null
},
{
"cell_type": "raw",
"id": "9f74444af3cd0c4f",
"metadata": {
"editable": true,
"raw_mimetype": "text/restructuredtext",
"slideshow": {
"slide_type": ""
},
"tags": []
},
"source": [
"Calling a polynomal function array with a set of inputs uses the fit polynomial to calculate a new set of outputs and returns a :class:`FunctionArray` with those specfied inputs and calculated outputs. The rms error between the fit and original outputs is low."
]
"source": "Calling a polynomal function array with a set of inputs uses the fit polynomial to calculate a new set of outputs and returns a :class:`FunctionArray` with those specfied inputs and calculated outputs. The rms error between the fit and original outputs is low."
},
{
"cell_type": "code",
"execution_count": null,
"id": "6a2b44484163c4ad",
"metadata": {
"editable": true,
"slideshow": {
"slide_type": ""
},
"tags": []
},
"outputs": [],
"source": [
"best_fit_quad = fit(fit.inputs)\n",
"rms_error = np.sqrt(np.square(best_fit_quad.outputs - outputs).sum())\n",
"rms_error"
]
],
"outputs": [],
"execution_count": null
},
{
"cell_type": "raw",
"id": "6fc5a84f1e009567",
"metadata": {
"editable": true,
"raw_mimetype": "text/restructuredtext",
"slideshow": {
"slide_type": ""
Expand All @@ -218,10 +221,8 @@
},
{
"cell_type": "code",
"execution_count": null,
"id": "5d8fe6270948c56b",
"metadata": {},
"outputs": [],
"source": [
"fit_linear = na.PolynomialFitFunctionArray(\n",
" inputs=inputs,\n",
Expand All @@ -233,28 +234,27 @@
"best_fit_linear = fit_linear(fit.inputs)\n",
"rms_error = np.sqrt(np.square(best_fit_linear.outputs - outputs).sum())\n",
"rms_error"
]
],
"outputs": [],
"execution_count": null
},
{
"cell_type": "raw",
"id": "b2796f50bbf1d7a4",
"metadata": {
"editable": true,
"raw_mimetype": "text/restructuredtext",
"slideshow": {
"slide_type": ""
},
"tags": []
},
"source": [
":mod:`named_arrays` plotting routines make it easy to visualize the orginal outputs, and fit outputs, on their inputs grids as a function of wavlength and time."
]
"source": ":mod:`named_arrays` plotting routines make it easy to visualize the orginal outputs, and fit outputs, on their inputs grids as a function of wavlength and time."
},
{
"cell_type": "code",
"execution_count": null,
"id": "3cd63c9428020f63",
"metadata": {},
"outputs": [],
"source": [
"original_output_y = fit.outputs.y.value\n",
"quadratic_fit_output_y = best_fit_quad.outputs.y.value\n",
Expand All @@ -274,14 +274,14 @@
" ax=ax,\n",
")\n",
"fig.suptitle('Orginal Function Array');"
]
],
"outputs": [],
"execution_count": null
},
{
"cell_type": "code",
"execution_count": null,
"id": "1fb0817f55f4679c",
"metadata": {},
"outputs": [],
"source": [
"fig, ax = na.plt.subplots(\n",
" axis_cols='wavelength',\n",
Expand All @@ -297,14 +297,14 @@
" ax=ax,\n",
")\n",
"fig.suptitle('Quadratic Fit');"
]
],
"outputs": [],
"execution_count": null
},
{
"cell_type": "code",
"execution_count": null,
"id": "4c2f891801be4bd5",
"metadata": {},
"outputs": [],
"source": [
"fig, ax = na.plt.subplots(\n",
" axis_cols='wavelength',\n",
Expand All @@ -320,7 +320,9 @@
" ax=ax,\n",
")\n",
"fig.suptitle('Linear Fit');"
]
],
"outputs": [],
"execution_count": null
}
],
"metadata": {
Expand All @@ -339,7 +341,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.1"
"version": "3.12.7"
}
},
"nbformat": 4,
Expand Down

0 comments on commit 350510c

Please sign in to comment.