.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "user/_auto_howtos/fun_vec_representation.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_user__auto_howtos_fun_vec_representation.py: Support vector representation of function values in CUQIpy geometries and samples ==================================================================================== In this demo, we illustrate the utility of an introduced representation of variables in CUQIpy (besides the already introduced parameter and function value representation). This representation, is a vector representation of the function values. The function values generally can be an n-dimensional array or any other type of object. When needed and when possible, this introduced representation is used to represent the function values as a vector (1D array). In particular, it is used to represent the function values of samples in a vector format. This is useful for computing statistics on the function values of samples. .. GENERATED FROM PYTHON SOURCE LINES 8-10 Import the necessary modules ----------------------------- .. GENERATED FROM PYTHON SOURCE LINES 10-15 .. code-block:: Python import cuqi import numpy as np import matplotlib.pyplot as plt .. GENERATED FROM PYTHON SOURCE LINES 16-17 Set the seed for reproducibility .. GENERATED FROM PYTHON SOURCE LINES 17-19 .. code-block:: Python np.random.seed(0) .. GENERATED FROM PYTHON SOURCE LINES 20-22 Create a 1D and a mapped 2D geometries -------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 24-26 Create a 1D KLExpansion geometry with 7 modes (and hence 7 KL expansion coefficients) and a 100-point grid defined on the interval [0, 1] .. GENERATED FROM PYTHON SOURCE LINES 26-31 .. code-block:: Python geom_1D = cuqi.geometry.KLExpansion( grid=np.linspace(0, 1, 100), num_modes=7 ) .. GENERATED FROM PYTHON SOURCE LINES 32-35 Create a mapped 2D Image geometry. We first define a threshold map that gives 1 if x is greater than or equal to 1. Otherwise, it gives zero. Then we create the geometry .. GENERATED FROM PYTHON SOURCE LINES 35-42 .. code-block:: Python def my_map(x): return np.where(x >= 1, 1, 0) geom_2D = cuqi.geometry.MappedGeometry( cuqi.geometry.Image2D((7, 7)), map=my_map) .. GENERATED FROM PYTHON SOURCE LINES 43-44 Query the geometries `fun_is_array` property .. GENERATED FROM PYTHON SOURCE LINES 44-47 .. code-block:: Python print(geom_1D.fun_is_array) print(geom_2D.fun_is_array) .. rst-class:: sphx-glr-script-out .. code-block:: none True True .. GENERATED FROM PYTHON SOURCE LINES 48-50 This property indicates whether the function value representation is an array (not necessarily a 1D array) or not. .. GENERATED FROM PYTHON SOURCE LINES 52-53 Query the number of parameters for both geometries .. GENERATED FROM PYTHON SOURCE LINES 53-56 .. code-block:: Python print(geom_1D.par_dim) print(geom_2D.par_dim) .. rst-class:: sphx-glr-script-out .. code-block:: none 7 49 .. GENERATED FROM PYTHON SOURCE LINES 57-58 Query the `shape` of the function value representation for both geometries .. GENERATED FROM PYTHON SOURCE LINES 58-61 .. code-block:: Python print(geom_1D.fun_shape) print(geom_2D.fun_shape) .. rst-class:: sphx-glr-script-out .. code-block:: none (100,) (7, 7) .. GENERATED FROM PYTHON SOURCE LINES 62-64 Query the `shape` of the vector representation of the function values for both geometries .. GENERATED FROM PYTHON SOURCE LINES 64-67 .. code-block:: Python print(geom_1D.funvec_shape) print(geom_2D.funvec_shape) .. rst-class:: sphx-glr-script-out .. code-block:: none (100,) (49,) .. GENERATED FROM PYTHON SOURCE LINES 68-70 Create random samples corresponding to each geometry ---------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 72-76 Create samples of random parameter values that correspond to each geometry. For the 1D geometry, we create 5 random samples from a Gaussian distribution with a zero mean and a variance of 1. These samples are samples of the KL coefficients equipped with the KL geometry object. .. GENERATED FROM PYTHON SOURCE LINES 76-79 .. code-block:: Python x_kl = cuqi.distribution.Gaussian(np.zeros(geom_1D.par_dim), 1, geometry=geom_1D) samples_1D = x_kl.sample(5) .. GENERATED FROM PYTHON SOURCE LINES 80-83 For the 2D geometry, we create 5 random samples from a Gaussian distribution with a "smile" image mean and a 0.01 variance. The smile image given by the 7 by 7 array below .. GENERATED FROM PYTHON SOURCE LINES 83-93 .. code-block:: Python smile = np.array([[0, 0, 0, 0, 0, 0, 0], [0, 1, 1, 0, 1, 1, 0], [0, 0, 0, 0, 0, 0, 0], [1, 0, 0, 0, 0, 0, 1], [1, 0, 0, 0, 0, 0, 1], [0, 1, 1, 1, 1, 1, 0], [0, 0, 0, 0, 0, 0, 0]]) x = cuqi.distribution.Gaussian(smile.ravel(), 0.01, geometry=geom_2D) samples_2D = x.sample(5) .. GENERATED FROM PYTHON SOURCE LINES 94-95 We plot the smile for illustration .. GENERATED FROM PYTHON SOURCE LINES 95-97 .. code-block:: Python plt.imshow(smile, cmap='Greys_r') .. image-sg:: /user/_auto_howtos/images/sphx_glr_fun_vec_representation_001.png :alt: fun vec representation :srcset: /user/_auto_howtos/images/sphx_glr_fun_vec_representation_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 98-99 We plot a 2D sample (the sample with index 4 in this case) .. GENERATED FROM PYTHON SOURCE LINES 99-102 .. code-block:: Python samples_2D.plot(4) .. image-sg:: /user/_auto_howtos/images/sphx_glr_fun_vec_representation_002.png :alt: fun vec representation :srcset: /user/_auto_howtos/images/sphx_glr_fun_vec_representation_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none [] .. GENERATED FROM PYTHON SOURCE LINES 103-105 Obtain the corresponding samples of function values (`funvals`) --------------------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 105-109 .. code-block:: Python samples_1D.funvals samples_2D.funvals .. rst-class:: sphx-glr-script-out .. code-block:: none CUQIpy Samples: --------------- Ns (number of samples): 5 Geometry: MappedGeometry(Image2D[49: (7, 7)]) Shape: (7, 7, 5) Samples: [[[0. 0. 0. 0. 0.] [0. 0. 0. 0. 0.] [0. 0. 0. 0. 0.] [0. 0. 0. 0. 0.] [0. 0. 0. 0. 0.] [0. 0. 0. 0. 0.] [0. 0. 0. 0. 0.]] [[0. 0. 0. 0. 0.] [0. 0. 0. 0. 1.] [0. 1. 1. 0. 1.] [0. 0. 0. 0. 0.] [0. 1. 1. 1. 1.] [1. 1. 1. 1. 1.] [0. 0. 0. 0. 0.]] [[0. 0. 0. 0. 0.] [0. 0. 0. 0. 0.] [0. 0. 0. 0. 0.] [0. 0. 0. 0. 0.] [0. 0. 0. 0. 0.] [0. 0. 0. 0. 0.] [0. 0. 0. 0. 0.]] [[0. 1. 1. 1. 1.] [0. 0. 0. 0. 0.] [0. 0. 0. 0. 0.] [0. 0. 0. 0. 0.] [0. 0. 0. 0. 0.] [0. 0. 0. 0. 0.] [1. 1. 1. 0. 0.]] [[1. 0. 0. 0. 1.] [0. 0. 0. 0. 0.] [0. 0. 0. 0. 0.] [0. 0. 0. 0. 0.] [0. 0. 0. 0. 0.] [0. 0. 0. 0. 0.] [0. 0. 0. 1. 0.]] [[0. 0. 0. 0. 0.] [0. 0. 1. 1. 0.] [0. 0. 0. 1. 0.] [1. 1. 1. 0. 0.] [0. 0. 0. 1. 0.] [1. 1. 0. 1. 1.] [0. 0. 0. 0. 0.]] [[0. 0. 0. 0. 0.] [0. 0. 0. 0. 0.] [0. 0. 0. 0. 0.] [0. 0. 0. 0. 0.] [0. 0. 0. 0. 0.] [0. 0. 0. 0. 0.] [0. 0. 0. 0. 0.]]] .. GENERATED FROM PYTHON SOURCE LINES 110-112 Note that the `funvals` property of the `Samples` class returns a `Samples` object .. GENERATED FROM PYTHON SOURCE LINES 114-115 query the `shape` of the `.funvals` property for both samples objects .. GENERATED FROM PYTHON SOURCE LINES 115-121 .. code-block:: Python print("samples_1D.funvals.shape") print(samples_1D.funvals.shape) print("samples_2D.funvals.shape") print(samples_2D.funvals.shape) .. rst-class:: sphx-glr-script-out .. code-block:: none samples_1D.funvals.shape (100, 5) samples_2D.funvals.shape (7, 7, 5) .. GENERATED FROM PYTHON SOURCE LINES 122-124 Note that for the 2D geometry, the `shape` of the `funvals` is (7, 7, 5). This is because the function value representation is a 2D array. .. GENERATED FROM PYTHON SOURCE LINES 126-127 query the `shape` of the `.funvals.vector` property for both samples objects .. GENERATED FROM PYTHON SOURCE LINES 127-133 .. code-block:: Python print("samples_1D.funvals.vector.shape") print(samples_1D.funvals.vector.shape) print("samples_2D.funvals.vector.shape") print(samples_2D.funvals.vector.shape) .. rst-class:: sphx-glr-script-out .. code-block:: none samples_1D.funvals.vector.shape (100, 5) samples_2D.funvals.vector.shape (49, 5) .. GENERATED FROM PYTHON SOURCE LINES 134-140 Now for both geometries, the samples in the converted Samples object is a two dimensional arrays. In this case, executing the `funvals` property leads to calling the `par2fun` method of the geometry to convert the parameter values to function values. Then executing the `vector` property leads to calling the `fun2vec` method of the geometry (if implemented) to convert the function values to a vector representation of these values. .. GENERATED FROM PYTHON SOURCE LINES 142-143 query the `shape` of the `.vector` property for both geometries .. GENERATED FROM PYTHON SOURCE LINES 143-149 .. code-block:: Python print("samples_1D.vector.shape") print(samples_1D.vector.shape) print("samples_2D.vector.shape") print(samples_2D.vector.shape) .. rst-class:: sphx-glr-script-out .. code-block:: none samples_1D.vector.shape (7, 5) samples_2D.vector.shape (49, 5) .. GENERATED FROM PYTHON SOURCE LINES 150-152 For samples_1D, we query the `is_par` and `is_vec` properties for all representations of the samples .. GENERATED FROM PYTHON SOURCE LINES 152-173 .. code-block:: Python print("samples_1D.is_par") print(samples_1D.is_par) print("samples_1D.is_vec") print(samples_1D.is_vec) print("samples_1D.vector.is_par") print(samples_1D.vector.is_par) print("samples_1D.vector.is_vec") print(samples_1D.vector.is_vec) print("samples_1D.funvals.is_par") print(samples_1D.funvals.is_par) print("samples_1D.funvals.is_vec") print(samples_1D.funvals.is_vec) print("samples_1D.funvals.vector.is_par") print(samples_1D.funvals.vector.is_par) print("samples_1D.funvals.vector.is_vec") print(samples_1D.funvals.vector.is_vec) .. rst-class:: sphx-glr-script-out .. code-block:: none samples_1D.is_par True samples_1D.is_vec True samples_1D.vector.is_par True samples_1D.vector.is_vec True samples_1D.funvals.is_par False samples_1D.funvals.is_vec True samples_1D.funvals.vector.is_par False samples_1D.funvals.vector.is_vec True .. GENERATED FROM PYTHON SOURCE LINES 174-176 Note that for this 1D geometry, the `is_vec` property is always `True`. This is because the function value representation is a 1D array. .. GENERATED FROM PYTHON SOURCE LINES 178-179 We do the same for samples_2D .. GENERATED FROM PYTHON SOURCE LINES 179-200 .. code-block:: Python print("samples_2D.is_par") print(samples_2D.is_par) print("samples_2D.is_vec") print(samples_2D.is_vec) print("samples_2D.vector.is_par") print(samples_2D.vector.is_par) print("samples_2D.vector.is_vec") print(samples_2D.vector.is_vec) print("samples_2D.funvals.is_par") print(samples_2D.funvals.is_par) print("samples_2D.funvals.is_vec") print(samples_2D.funvals.is_vec) print("samples_2D.funvals.vector.is_par") print(samples_2D.funvals.vector.is_par) print("samples_2D.funvals.vector.is_vec") print(samples_2D.funvals.vector.is_vec) .. rst-class:: sphx-glr-script-out .. code-block:: none samples_2D.is_par True samples_2D.is_vec True samples_2D.vector.is_par True samples_2D.vector.is_vec True samples_2D.funvals.is_par False samples_2D.funvals.is_vec False samples_2D.funvals.vector.is_par False samples_2D.funvals.vector.is_vec True .. GENERATED FROM PYTHON SOURCE LINES 201-204 Note that the property `samples_2D.funvals.is_vec` is false, but after converting the function values to a vector representation using the `vector` property, the property `samples_2D.vector.funvals.vector.is_vec` is true. .. GENERATED FROM PYTHON SOURCE LINES 206-208 Computing statistics on parameter and on function values for samples (the 1D geometry case) -------------------------------------------------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 210-215 Compute and plot mean on parameter values and on function values. Note that the default behavior of the `plot_mean` method is to compute the mean on the parameters then apply the `par2fun` map on the mean. Passing the argument `plot_par=True` results in plotting the mean of the original parameter values (the KL expansion coefficients). .. GENERATED FROM PYTHON SOURCE LINES 215-226 .. code-block:: Python print("samples_1D.plot_mean with plot_par=True") plt.figure() samples_1D.plot_mean(plot_par=True) print("samples_1D.plot_mean") plt.figure() samples_1D.plot_mean(color='r') print("samples_1D.funvals.plot_mean") samples_1D.funvals.plot_mean(color='b', linestyle='--') .. rst-class:: sphx-glr-horizontal * .. image-sg:: /user/_auto_howtos/images/sphx_glr_fun_vec_representation_003.png :alt: Sample mean :srcset: /user/_auto_howtos/images/sphx_glr_fun_vec_representation_003.png :class: sphx-glr-multi-img * .. image-sg:: /user/_auto_howtos/images/sphx_glr_fun_vec_representation_004.png :alt: Sample mean :srcset: /user/_auto_howtos/images/sphx_glr_fun_vec_representation_004.png :class: sphx-glr-multi-img .. rst-class:: sphx-glr-script-out .. code-block:: none samples_1D.plot_mean with plot_par=True samples_1D.plot_mean samples_1D.funvals.plot_mean [] .. GENERATED FROM PYTHON SOURCE LINES 227-232 Note that in the second figure, the mean in both cases is the same, i. e., applying the KL map (the linear combination of the of the KL expansion basis vectors weighted by the given KL coefficients) on the average KL coefficients is the same as applying the KL map on each sample then computing the mean of the result. .. GENERATED FROM PYTHON SOURCE LINES 234-235 Compute variance on parameter values and on function values .. GENERATED FROM PYTHON SOURCE LINES 235-245 .. code-block:: Python print("samples_1D.plot_variance with plot_par=True") plt.figure() samples_1D.plot_variance(plot_par=True) print("samples_1D.plot_variance") plt.figure() samples_1D.plot_variance(color='r') print("samples_1D.funvals.plot_variance") samples_1D.funvals.plot_variance(color='b', linestyle='--') .. rst-class:: sphx-glr-horizontal * .. image-sg:: /user/_auto_howtos/images/sphx_glr_fun_vec_representation_005.png :alt: Sample variance :srcset: /user/_auto_howtos/images/sphx_glr_fun_vec_representation_005.png :class: sphx-glr-multi-img * .. image-sg:: /user/_auto_howtos/images/sphx_glr_fun_vec_representation_006.png :alt: Sample variance :srcset: /user/_auto_howtos/images/sphx_glr_fun_vec_representation_006.png :class: sphx-glr-multi-img .. rst-class:: sphx-glr-script-out .. code-block:: none samples_1D.plot_variance with plot_par=True samples_1D.plot_variance samples_1D.funvals.plot_variance [] .. GENERATED FROM PYTHON SOURCE LINES 246-250 Here in the second figure, the results of computing the variance differ in both cases. Additionally, computing the variance of the parameters then applying the KL map can result in negative values. .. GENERATED FROM PYTHON SOURCE LINES 252-254 Computing statistics on parameter and on function values for samples (the 2D geometry case) ---------------------------------------------------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 256-258 Compute mean on parameter values, function values, and vector representation of function values .. GENERATED FROM PYTHON SOURCE LINES 258-273 .. code-block:: Python print("samples_2D.plot_mean") plt.figure() samples_2D.plot_mean(vmin=0, vmax=1) plt.colorbar() print("samples_2D.funvals.plot_mean") plt.figure() samples_2D.funvals.plot_mean(vmin=0, vmax=1) plt.colorbar() print("samples_2D.funvals.vector.plot_mean") plt.figure() samples_2D.funvals.vector.plot_mean(vmin=0, vmax=1) plt.colorbar() .. rst-class:: sphx-glr-horizontal * .. image-sg:: /user/_auto_howtos/images/sphx_glr_fun_vec_representation_007.png :alt: Sample mean :srcset: /user/_auto_howtos/images/sphx_glr_fun_vec_representation_007.png :class: sphx-glr-multi-img * .. image-sg:: /user/_auto_howtos/images/sphx_glr_fun_vec_representation_008.png :alt: Sample mean :srcset: /user/_auto_howtos/images/sphx_glr_fun_vec_representation_008.png :class: sphx-glr-multi-img * .. image-sg:: /user/_auto_howtos/images/sphx_glr_fun_vec_representation_009.png :alt: Sample mean :srcset: /user/_auto_howtos/images/sphx_glr_fun_vec_representation_009.png :class: sphx-glr-multi-img .. rst-class:: sphx-glr-script-out .. code-block:: none samples_2D.plot_mean samples_2D.funvals.plot_mean samples_2D.funvals.vector.plot_mean .. GENERATED FROM PYTHON SOURCE LINES 274-276 Compute variance on parameter values, function values, and vector representation of function values .. GENERATED FROM PYTHON SOURCE LINES 276-291 .. code-block:: Python print("samples_2D.plot_variance") plt.figure() samples_2D.plot_variance(vmin=0, vmax=1) plt.colorbar() print("samples_2D.funvals.plot_variance") plt.figure() samples_2D.funvals.plot_variance(vmin=0, vmax=1) plt.colorbar() print("samples_2D.funvals.vector.plot_variance") plt.figure() samples_2D.funvals.vector.plot_variance(vmin=0, vmax=1) plt.colorbar() .. rst-class:: sphx-glr-horizontal * .. image-sg:: /user/_auto_howtos/images/sphx_glr_fun_vec_representation_010.png :alt: Sample variance :srcset: /user/_auto_howtos/images/sphx_glr_fun_vec_representation_010.png :class: sphx-glr-multi-img * .. image-sg:: /user/_auto_howtos/images/sphx_glr_fun_vec_representation_011.png :alt: Sample variance :srcset: /user/_auto_howtos/images/sphx_glr_fun_vec_representation_011.png :class: sphx-glr-multi-img * .. image-sg:: /user/_auto_howtos/images/sphx_glr_fun_vec_representation_012.png :alt: Sample variance :srcset: /user/_auto_howtos/images/sphx_glr_fun_vec_representation_012.png :class: sphx-glr-multi-img .. rst-class:: sphx-glr-script-out .. code-block:: none samples_2D.plot_variance samples_2D.funvals.plot_variance samples_2D.funvals.vector.plot_variance .. GENERATED FROM PYTHON SOURCE LINES 292-303 Note that computing the samples mean then applying the thresholding, the first case, is not equivalent to applying the thresholding on the samples then computing their mean (the latter two cases). Similar observation can be made about computing the variance. In fact, the variance in the first case is just zero because the parameter sample variance (before thresholding) is smaller than 1, the thresholding, then, forces all the values to zero. Also note that, in this case, we are able to compute the statistics directly on the function values, but for some geometries, this may not be possible and we may need to use the vector representation of the function values to compute the statistics. .. GENERATED FROM PYTHON SOURCE LINES 305-307 Plotting samples ------------------------------------ .. GENERATED FROM PYTHON SOURCE LINES 309-310 Plot samples from `samples_2D`: .. GENERATED FROM PYTHON SOURCE LINES 310-314 .. code-block:: Python print("samples_2D.plot") plt.figure() samples_2D.plot([1, 4]) .. image-sg:: /user/_auto_howtos/images/sphx_glr_fun_vec_representation_013.png :alt: fun vec representation :srcset: /user/_auto_howtos/images/sphx_glr_fun_vec_representation_013.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none samples_2D.plot [, ] .. GENERATED FROM PYTHON SOURCE LINES 315-316 Plot samples from `samples_2D.funvals`: .. GENERATED FROM PYTHON SOURCE LINES 316-320 .. code-block:: Python print("samples_2D.funvals.plot") plt.figure() samples_2D.funvals.plot([1, 4]) .. image-sg:: /user/_auto_howtos/images/sphx_glr_fun_vec_representation_014.png :alt: fun vec representation :srcset: /user/_auto_howtos/images/sphx_glr_fun_vec_representation_014.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none samples_2D.funvals.plot [, ] .. GENERATED FROM PYTHON SOURCE LINES 321-322 Plot samples from `samples_2D.funvals.vector`: .. GENERATED FROM PYTHON SOURCE LINES 322-326 .. code-block:: Python print("samples_2D.funvals.vector.plot") plt.figure() samples_2D.funvals.vector.plot([1, 4]) .. image-sg:: /user/_auto_howtos/images/sphx_glr_fun_vec_representation_015.png :alt: fun vec representation :srcset: /user/_auto_howtos/images/sphx_glr_fun_vec_representation_015.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none samples_2D.funvals.vector.plot [, ] .. GENERATED FROM PYTHON SOURCE LINES 327-332 The results are the same in all the three cases. Note that for the first case, the default behavior of the `plot` method is to convert the samples to the function values and then plot them. If one is interested in plotting the original parameter values, one can pass the argument `plot_par=True` to the `plot` method. .. GENERATED FROM PYTHON SOURCE LINES 332-335 .. code-block:: Python print("samples_2D.plot([1, 4], plot_par=True)") plt.figure() samples_2D.plot([1, 4], plot_par=True) .. image-sg:: /user/_auto_howtos/images/sphx_glr_fun_vec_representation_016.png :alt: fun vec representation :srcset: /user/_auto_howtos/images/sphx_glr_fun_vec_representation_016.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none samples_2D.plot([1, 4], plot_par=True) [, ] .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 1.212 seconds) .. _sphx_glr_download_user__auto_howtos_fun_vec_representation.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: fun_vec_representation.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: fun_vec_representation.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: fun_vec_representation.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_