The class that generates and stores dataset used for testing of fitting algorithms
| 24 | |
| 25 | |
| 26 | class DataForFittingTest: |
| 27 | """ |
| 28 | The class that generates and stores dataset used for testing of fitting algorithms |
| 29 | """ |
| 30 | |
| 31 | def __init__(self, **kwargs): |
| 32 | self.spectra = None |
| 33 | self.weights = None |
| 34 | self.data_input = None |
| 35 | |
| 36 | self.generate_dataset(**kwargs) |
| 37 | |
| 38 | def generate_dataset( |
| 39 | self, |
| 40 | *, |
| 41 | n_pts=101, |
| 42 | pts_range=(0, 100), |
| 43 | n_spectra=3, |
| 44 | n_gaus_centers_range=(20, 80), |
| 45 | gauss_std_range=(10, 20), |
| 46 | weights_range=(0.1, 1), |
| 47 | n_data_dimensions=(8,), |
| 48 | axis=0, |
| 49 | ): |
| 50 | if n_data_dimensions: |
| 51 | data_dim = n_data_dimensions |
| 52 | else: |
| 53 | data_dim = (1,) |
| 54 | |
| 55 | # Values for 'energy' axis |
| 56 | self.x_values = np.mgrid[pts_range[0] : pts_range[1] : n_pts * 1j] |
| 57 | |
| 58 | # Centers of gaussians are evenly spread in the range |
| 59 | gaussian_centers = np.mgrid[n_gaus_centers_range[0] : n_gaus_centers_range[1] : n_spectra * 1j] |
| 60 | # Standard deviations are uniformly distributed in the range |
| 61 | gaussian_std = np.random.rand(n_spectra) * (gauss_std_range[1] - gauss_std_range[0]) + gauss_std_range[0] |
| 62 | |
| 63 | self.spectra = _generate_gaussian_spectra( |
| 64 | x_values=self.x_values, gaussian_centers=gaussian_centers, gaussian_std=gaussian_std |
| 65 | ) |
| 66 | |
| 67 | # The number of pixels in the flattened multidimensional image |
| 68 | dims = np.prod(data_dim) |
| 69 | # Generate data for every pixel of the multidimensional image |
| 70 | self.weights = np.random.rand(n_spectra, dims) * (weights_range[1] - weights_range[0]) + weights_range[0] |
| 71 | self.data_input = np.matmul(self.spectra, self.weights) |
| 72 | |
| 73 | if n_data_dimensions: |
| 74 | # Convert weights and data from 2D to multidimensional arrays |
| 75 | self.weights = np.reshape(self.weights, np.insert(data_dim, 0, n_spectra)) |
| 76 | self.data_input = np.reshape(self.data_input, np.insert(data_dim, 0, n_pts)) |
| 77 | |
| 78 | if axis: # If axis != 0 |
| 79 | # Create copy of the array (np.moveaxis creates view of the array) |
| 80 | self.weights = np.array(np.moveaxis(self.weights, 0, axis)) |
| 81 | self.data_input = np.array(np.moveaxis(self.data_input, 0, axis)) |
| 82 | else: |
| 83 | # Convert weights and data to 1D arrays representing a single point |
no outgoing calls