def resample_dataset(dataset: DataSet, sample_rate: Tuple[int], copy_metadata: bool = False, output_parameter_name: Optional[str] = None) -> DataSet: """ Given a dataset resample the measurement array Args: dataset: Dataset to be slice sample_rate: Tuple with for each axis the sample rate. Must be a postive integer copy_metadata: If True then copy the metadata of the input dataset output_parameter_name: Name of the output array Returns: Dataset with sliced data """ zarray = dataset.default_parameter_array() if output_parameter_name is None: output_parameter_name = zarray.name slice_objects = tuple([ slice(0, size, sample_rate[jj]) for jj, size in enumerate(zarray.shape) ]) return _slice_dataset(dataset, slice_objects, output_parameter_name, copy_metadata=copy_metadata, verbose=0)
def average_multirow_dataset(dataset: DataSet, number_of_repetitions: int, new_values=None, parameter_name: str = 'signal', output_parameter_name: str = 'signal') -> DataSet: """ Calculate the averaged signal from a 2D dataset with repeated rows Args: dataset: Dataset containing the data to be averaged number_of_repetitions: Number of rows over which to average new_values: Optional new values for the averaged axis parameter_name: Name of data array to process output_parameter_name: Name of output array Returns: Averaged dataset """ zarray = dataset.default_parameter_array(parameter_name) set_arrays = zarray.set_arrays xarray = set_arrays[1] yarray = set_arrays[0] if new_values is None: number_of_blocks = int(zarray.shape[0] / number_of_repetitions) new_values = np.linspace(yarray[0], yarray[-1], number_of_blocks) data = zarray ncolumns = data.shape[1] averaged_signal = data.transpose().reshape(-1, number_of_repetitions).mean(1).reshape(ncolumns, -1).transpose() dataset_averaged = qtt.data.makeDataSet2Dplain(xarray.name, xarray[0], yarray.name, new_values, zname=output_parameter_name, z=averaged_signal, xunit=xarray.unit, yunit=yarray.unit, zunit=zarray.unit) return dataset_averaged
def process_dataarray( dataset: DataSet, input_array_name: str, output_array_name: Optional[str], processing_function: Callable, label: Optional[str] = None, unit: Optional[str] = None, ) -> DataSet: """ Apply a function to a DataArray in a DataSet Args: dataset: Input dataset containing the data array input_array_name: Name of the data array to be processed output_array_nane: Name of the output array or None to operate in place processing_function: Method to apply to the data array label: Label for the output array unit: Unit for the output array """ array = dataset.default_parameter_array(input_array_name) data = processing_function(np.array(array)) if label is None: label = array.label if unit is None: unit = array.unit if output_array_name is None: array.ndarray[:] = data else: data_array = DataArray(array_id=output_array_name, name=output_array_name, label=label, set_arrays=array.set_arrays, preset_data=data, unit=unit) dataset.add_array(data_array) return dataset
def _slice_dataset(dataset: DataSet, slice_objects: Sequence[slice], output_parameter_name: Optional[str], copy_metadata: bool, verbose: int = 0): """ Slice the measurement array of a dataset and adjust the setpoints arrays accordingly """ zarray = dataset.default_parameter_array() if output_parameter_name is None: output_parameter_name = zarray.name set_arrays = zarray.set_arrays yarray = set_arrays[0] scan_dimension = dataset_dimension(dataset) is_1d_dataset = scan_dimension == 1 is_2d_dataset = scan_dimension == 2 if verbose: print(f'slice_dataset: dimension {scan_dimension} slice_objects {slice_objects}') if is_1d_dataset: signal_window = zarray[tuple(slice_objects)] dataset_window = qtt.data.makeDataSet1Dplain(yarray.name, yarray[slice_objects[0]], yname=output_parameter_name, y=signal_window, xunit=yarray.unit, yunit=zarray.unit) elif is_2d_dataset: xarray = set_arrays[1] signal_window = zarray[tuple(slice_objects)] dataset_window = qtt.data.makeDataSet2Dplain(xarray.name, xarray[0][slice_objects[1]], yarray.name, yarray[slice_objects[0]], zname=output_parameter_name, z=signal_window, xunit=xarray.unit, yunit=yarray.unit, zunit=zarray.unit) else: raise NotImplementedError('slicing a multi-dimensional dataset of dimension {scan_dimension} is not supported') if copy_metadata: dataset_window.metadata = copy.deepcopy(dataset.metadata) return dataset_window
def plot_dataset(dataset: DataSet, parameter_names: Optional[list] = None, fig: Optional[int] = 1) -> None: """ Plot a dataset to matplotlib figure window Args: dataset: DataSet to be plotted parameter_names: List of arrays to be plotted fig: Specification if Matplotlib figure window """ if parameter_names is None: parameter_names = [dataset.default_parameter_name()] if parameter_names == 'all': parameter_names = [ name for name in dataset.arrays.keys() if not dataset.arrays[name].is_setpoint ] default_array = dataset.default_parameter_array() if fig: plt.figure(fig) plt.clf() if len(default_array.shape) >= 2: if len(parameter_names) > 1: arrays = [ dataset.default_parameter_array(parameter_name) for parameter_name in parameter_names ] plot_handle = MatPlot(*arrays, num=fig) else: plot_handle = MatPlot(dataset.default_parameter_array( parameter_names[0]), num=fig) else: for idx, parameter_name in enumerate(parameter_names): if idx == 0: plot_handle = MatPlot( dataset.default_parameter_array(parameter_name), num=fig) else: plot_handle.add( dataset.default_parameter_array(parameter_name, ))
def slice_dataset(dataset: DataSet, window: Sequence[float], axis: int = 0, verbose: int = 0, copy_metadata: bool = False, output_parameter_name=None) -> DataSet: """ Given a dataset and a window for the horizontal axis return the dataset with selected window Args: dataset: Dataset to be slice window: Specification of the window to be selected axis: Axis used for slicing verbose: Verbosity level copy_metadata: If True then copy the metadata of the input dataset output_parameter_name: Name of the output array Returns: Dataset with sliced data """ zarray = dataset.default_parameter_array() if output_parameter_name is None: output_parameter_name = zarray.name set_arrays = zarray.set_arrays yarray = set_arrays[0] scan_dimension = dataset_dimension(dataset) is_1d_dataset = scan_dimension == 1 if is_1d_dataset: if not axis == 0: raise AssertionError('for a 1D dataset axis should be 0') else: xarray = set_arrays[1] slice_objects = [slice(0, size) for jj, size in enumerate(zarray.shape)] if axis == 0: slice_array = yarray start_idx = int(np.floor(np.interp(window[0], slice_array.ndarray, np.arange(slice_array.ndarray.size)))) end_idx = int(np.interp(window[1], slice_array.ndarray, np.arange(slice_array.ndarray.size))) slice_objects[0] = slice(start_idx, end_idx) else: slice_array = xarray start_idx = int(np.floor(np.interp(window[0], slice_array.ndarray[0], np.arange(slice_array.ndarray[0].size)))) end_idx = int(np.interp(window[1], slice_array.ndarray[0], np.arange(slice_array.ndarray[0].size))) slice_objects[1] = slice(start_idx, end_idx) return _slice_dataset(dataset, tuple(slice_objects), output_parameter_name, copy_metadata=copy_metadata, verbose=0)
def analyse_RTS(dataset: DataSet, fig: int = 1) -> dict: time = default_setpoint_array(dataset) rtsdata = np.array(dataset.default_parameter_array()) num_bins = 40 counts, bins = np.histogram(rtsdata, bins=num_bins) bincentres = np.array([(bins[i] + bins[i + 1]) / 2 for i in range(0, len(bins) - 1)]) par_fit, result_dict = fit_double_gaussian(bincentres, counts) split = result_dict['split'] plt.figure(fig) plt.clf() plt.subplot(1, 2, 1) plt.plot(time[:10000], rtsdata[:10000], '.', label='signal') plt.xlabel('Time') plt.title('Selection of points') plt.subplot(1, 2, 2) _plot_rts_histogram(rtsdata, num_bins, par_fit, split, 'Histogram') return {}
def analyse_polarization_line(dataset: DataSet, fig: int = 1, verbose=0) -> dict: """ Analyse dataset with polarization line """ if verbose: print('analyse_polarization_line: dataset: %s' % dataset.location) signal = dataset.default_parameter_array() delta = default_setpoint_array(dataset, signal.name) lever_arm = 80 delta_uev = np.array(delta) * lever_arm signal = qtt.algorithms.generic.smoothImage(signal) kb = scipy.constants.physical_constants['Boltzmann constant in eV/K'][ 0] * 1e6 kT = 75e-3 * kb # effective electron temperature in ueV par_fit, initial_parameters, results = fit_pol_all(delta_uev, signal, kT) plot_polarization_fit(delta_uev, signal, results, fig) return {}
def plot_dataset(dataset: DataSet, scanjob, save=True) -> None: """ Plot a dataset to matplotlib figure window Args: dataset: DataSet to be plotted scanjob: scanjob of the measurement save: Select if you want to save the plots """ parameter_names = [ name for name in dataset.arrays.keys() if not dataset.arrays[name].is_setpoint ] default_array = dataset.default_parameter_array() # Path for saving base_loc = dataset.default_io.base_location folder = '\\' + dataset.location + '\\' label = str(scanjob.get('dataset_label')) path = base_loc + folder + label # 2D plots if len(default_array.shape) >= 2: for idx, parameter_name in enumerate(parameter_names): plot_handle = MatPlot(dataset.arrays[parameter_name], num=idx) plot_handle.rescale_axis() if save == True: plt.savefig(path + str(idx) + '.png') # 1D plots else: for idx, parameter_name in enumerate(parameter_names): plot_handle = MatPlot(dataset.arrays[parameter_name], num=idx) plot_handle.rescale_axis() if save == True: plt.savefig(path + str(idx) + '.png')
def dataset_dimension(dataset: DataSet) -> int: """ Return dimension of DataSet """ return len(dataset.default_parameter_array().set_arrays)
def _parse_1d_dataset(dataset: DataSet) -> tuple: y_data = np.array(dataset.default_parameter_array()) x_data = np.array(qtt.data.default_setpoint_array(dataset)) return x_data, y_data