def get_dataset(ds): """ Get a dataset from a results dict, a string or a dataset Args: ds (dict, str or qcodes.DataSet): the data to be put in the dataset Returns: ds (qcodes.DataSet) dataset made from the input data """ if isinstance(ds, dict): ds = ds.get('dataset', None) if ds is None: return None if isinstance(ds, str): ds = qcodes.load_data(ds) return ds
def test_new_loop_1D_None_result(self): arrs = {} p_measure = Parameter("p_measure", set_cmd=None) with Measurement("new_loop_1D_None_result") as msmt: for k, val in enumerate(Sweep(self.p_sweep.sweep(0, 1, 0.1))): arr = arrs.setdefault(msmt.action_indices, np.zeros(msmt.loop_shape)) if not k % 2: p_measure(k) else: p_measure(None) arr[k] = msmt.measure(p_measure) verify_msmt(msmt, arrs, allow_nan=True) # Verify that the measurement dataset records the correct measurement type data = load_data(msmt.dataset.location) self.assertEqual(data.metadata.get("measurement_type"), "Measurement")
def show_num(id, useQT=False, **kwargs): """ Show and return plot and data for id in current instrument. Args: id(number): id of instrument useQT: Use pyqtgraph as an alternative to Matplotlib **kwargs: Are passed to plot function Returns: plot, data : returns the plot and the dataset """ if not getattr(CURRENT_EXPERIMENT, "init", True): raise RuntimeError("Experiment not initalized. " "use qc.Init(mainfolder, samplename)") str_id = '{0:03d}'.format(id) t = qc.DataSet.location_provider.fmt.format(counter=str_id) data = qc.load_data(t) plots = [] for value in data.arrays.keys(): if "set" not in value: if useQT: plot = QtPlot( getattr(data, value), fig_x_position=CURRENT_EXPERIMENT['plot_x_position'], **kwargs) title = "{} #{}".format(CURRENT_EXPERIMENT["sample_name"], str_id) plot.subplots[0].setTitle(title) plot.subplots[0].showGrid(True, True) else: plot = MatPlot(getattr(data, value), **kwargs) title = "{} #{}".format(CURRENT_EXPERIMENT["sample_name"], str_id) plot.subplots[0].set_title(title) plot.subplots[0].grid() plots.append(plot) return data, plots
def show_num(id, useQT=False, do_plots=True): """ Show and return plot and data for id in current instrument. Args: id(number): id of instrument do_plots: Default False: if false no plots are produced. Returns: plot, data : returns the plot and the dataset """ if not getattr(CURRENT_EXPERIMENT, "init", True): raise RuntimeError("Experiment not initalized. " "use qc.Init(mainfolder, samplename)") str_id = '{0:03d}'.format(id) t = qc.DataSet.location_provider.fmt.format(counter=str_id) data = qc.load_data(t) if do_plots: plots = [] for value in data.arrays.keys(): if "set" not in value: if useQT: plot = QtPlot( getattr(data, value), fig_x_position=CURRENT_EXPERIMENT['plot_x_position']) title = "{} #{}".format(CURRENT_EXPERIMENT["sample_name"], str_id) plot.subplots[0].setTitle(title) plot.subplots[0].showGrid(True, True) else: plot = MatPlot(getattr(data, value)) title = "{} #{}".format(CURRENT_EXPERIMENT["sample_name"], str_id) plot.subplots[0].set_title(title) plot.subplots[0].grid() plots.append(plot) else: plots = None return data, plots
def logCallback(self, index): ''' Function called when a log entry is selected ''' logging.info('logCallback!') logging.debug('logCallback: index %s' % str(index)) self.__debug['last'] = index pp = index.parent() row = index.row() tag = pp.child(row, 2).data() # load data if tag is not None: print('logCallback! tag %s' % tag) try: logging.debug('load tag %s' % tag) data = qcodes.load_data(tag) self.qplot.clear() infotxt = 'arrays: ' + ', '.join(list(data.arrays.keys())) q = pp.child(row, 1).model() q.setData(pp.child(row, 1), infotxt) param_name = self.plot_parameter(data) if param_name is not None: logging.info( 'using parameter %s for plotting' % param_name) self.qplot.add(getattr(data, param_name)) else: logging.info('could not find parameter for DataSet') except Exception as e: print('logCallback! error ...') print(e) logging.warning(e) pass
def load_dataset(location, io=None, verbose=0): """ Load a dataset from storage An attempt is made to automatically detect the formatter. Supported are currently GNUPlotFormat and HDF5Format Args: location (str): either the relative or full location io (None or qcodes.DiskIO): Returns: dataset (DataSet or None) """ if io is None: io = qcodes.DataSet.default_io formatters = [qcodes.DataSet.default_formatter] try: from qcodes.data.hdf5_format_hickle import HDF5FormatHickle formatters += [HDF5FormatHickle()] except: pass from qcodes.data.hdf5_format import HDF5Format formatters += [HDF5Format()] from qcodes.data.gnuplot_format import GNUPlotFormat formatters += [GNUPlotFormat()] data = None for ii, hformatter in enumerate(formatters): try: if verbose: print('%d: %s' % (ii, hformatter)) data = qcodes.load_data(location, formatter=hformatter, io=io) if len(data.arrays) == 0: data = None raise Exception( 'empty dataset, probably a HDF5 format misread by GNUPlotFormat' ) logging.debug('load_data: loaded %s with %s' % (location, hformatter)) except Exception as ex: logging.info( 'load_data: location %s: failed for formatter %d: %s' % (location, ii, hformatter)) if verbose: print(ex) pass finally: if data is not None: if isinstance(hformatter, GNUPlotFormat): # workaround for bug in GNUPlotFormat not saving the units if '__dataset_metadata' in data.metadata: dataset_meta = data.metadata['__dataset_metadata'] for key, array_metadata in dataset_meta[ 'arrays'].items(): if key in data.arrays: if data.arrays[key].unit is None: if verbose: print( 'load_dataset: updating unit for %s' % key) data.arrays[key].unit = array_metadata[ 'unit'] if verbose: print('succes with formatter %s' % hformatter) break if verbose: if data is None: print('could not load data from %s, returning None' % location) return data
def show_num(ids, samplefolder=None, useQT=False, avg_sub='', do_plots=True, savepng=True, fig_size=[6, 4], clim=None, dataname=None, xlim=None, ylim=None, transpose=False, auto_color_scale: Optional[bool] = None, cutoff_percentile: Optional[Union[Tuple[Number, Number], Number]] = None, **kwargs): """ Show and return plot and data. Args: ids (number, list): id or list of ids of dataset(s) samplefolder (str): Sample folder if loading data from different sample than the initialized. useQT (boolean): If true plots with QTplot instead of matplotlib avg_sub (str: 'col' or 'row'): Subtracts average from either each collumn ('col') or each row ('row') do_plots: (boolean): if false no plots are produced. dataname (str): If given only plots dataset with that name savepng (boolean): If true saves matplotlib figure as png fig_size [6,4]: Figure size in inches clim [cmin,cmax]: Set min and max of colorbar to cmin and cmax respectrively xlim [xmin,xmax]: Set limits on x axis ylim [ymin,ymax]: set limits on y axis transpose (boolean): Transpose data to be plotted (only works for 2D scans and qc.MatPlot) auto_color_scale: if True, the colorscale of heatmap plots will be automatically adjusted to disregard outliers. cutoff_percentile: percentile of data that may maximally be clipped on both sides of the distribution. If given a tuple (a,b) the percentile limits will be a and 100-b. See also the plotting tuorial notebook. **kwargs: Are passed to plot function Returns: data, plots : returns the plots and the datasets """ # default values if auto_color_scale is None: auto_color_scale = qcodes.config.plotting.auto_color_scale.enabled if cutoff_percentile is None: cutoff_percentile = cast( Tuple[Number, Number], tuple(qcodes.config.plotting.auto_color_scale.cutoff_percentile)) if not isinstance(ids, collections.Iterable): ids = (ids, ) data_list = [] keys_list = [] # Define samplefolder if samplefolder == None: check_experiment_is_initialized() samplefolder = qc.DataSet.location_provider.fmt.format(counter='') # Load all datasets into list for id in ids: path = samplefolder + '{0:03d}'.format(id) data = qc.load_data(path) data_list.append(data) # find datanames to be plotted if do_plots: if useQT and len(ids) is not 1: raise ValueError( 'qcodes.QtPlot does not support multigraph plotting. Set useQT=False to plot multiple datasets.' ) if dataname is not None: if dataname not in [ key for key in data.arrays.keys() if "_set" not in key ]: raise RuntimeError('Dataname not in dataset. Input dataname was: \'{}\''.format(dataname), \ 'while dataname(s) in dataset are: \'{}\'.'.format('\', \''.join(data.arrays.keys()))) keys = [dataname] else: keys = [key for key in data.arrays.keys() if "_set" not in key] keys_list.append(keys) if do_plots: unique_keys = list( set([item for sublist in keys_list for item in sublist])) plots = [] num = '' l = len(unique_keys) for j, key in enumerate(unique_keys): array_list = [] xlims = [[], []] ylims = [[], []] clims = [[], []] # Find datasets containing data with dataname == key for data, keys in zip(data_list, keys_list): if key in keys: arrays = getattr(data, key) if transpose and len(arrays.set_arrays) == 2: if useQT: raise AttributeError( 'Transpose only works for qc.MatPlot.') if dataname is None and l != 1: raise ValueError( 'Dataname has to be provided to plot data transposed for dataset with more ' 'than 1 measurement. Datanames in dataset are: \'{}\'.' .format('\', \''.join(unique_keys))) arrays.ndarray = arrays.ndarray.T set0_temp = arrays.set_arrays[0] set1_temp = arrays.set_arrays[1] set0_temp.ndarray = set0_temp.ndarray.T set1_temp.ndarray = set1_temp.ndarray.T arrays.set_arrays = ( set1_temp, set0_temp, ) if avg_sub == 'row': for i in range(np.shape(arrays.ndarray)[0]): arrays.ndarray[i, :] -= np.nanmean( arrays.ndarray[i, :]) if avg_sub == 'col': for i in range(np.shape(arrays.ndarray)[1]): arrays.ndarray[:, i] -= np.nanmean(arrays.ndarray[:, i]) array_list.append(arrays) # Find axis limits for dataset if len(arrays.set_arrays) == 2: xlims[0].append(np.nanmin(arrays.set_arrays[1])) xlims[1].append(np.nanmax(arrays.set_arrays[1])) ylims[0].append(np.nanmin(arrays.set_arrays[0])) ylims[1].append(np.nanmax(arrays.set_arrays[0])) if auto_color_scale: vlims = auto_range_iqr(arrays.ndarray) else: vlims = (np.nanmin(arrays.ndarray), np.nanmax(arrays.ndarray)) clims[0].append(vlims[0]) clims[1].append(vlims[1]) else: xlims[0].append(np.nanmin(arrays.set_arrays[0])) xlims[1].append(np.nanmax(arrays.set_arrays[0])) ylims[0].append(np.nanmin(arrays.ndarray)) ylims[1].append(np.nanmax(arrays.ndarray)) if useQT: plot = QtPlot( array_list[0], fig_x_position=CURRENT_EXPERIMENT['plot_x_position'], **kwargs) title = "{} #{}".format(CURRENT_EXPERIMENT["sample_name"], '{}'.format(ids[0])) plot.subplots[0].setTitle(title) plot.subplots[0].showGrid(True, True) if savepng: print('Save plot only working for matplotlib figure.', \ 'Set useQT=False to save png.') else: plot = MatPlot(array_list, **kwargs) plot.rescale_axis() plot.fig.tight_layout(pad=3) plot.fig.set_size_inches(fig_size) # Set axis limits if xlim is None: plot[0].axes.set_xlim( [np.nanmin(xlims[0]), np.nanmax(xlims[1])]) else: plot[0].axes.set_xlim(xlim) if ylim is None: plot[0].axes.set_ylim( [np.nanmin(ylims[0]), np.nanmax(ylims[1])]) else: plot[0].axes.set_ylim(ylim) if len(arrays.set_arrays) == 2: total_clim = [None, None] for i in range(len(array_list)): # TODO(DV): get colorbar from plot children (should be ax.qcodes_colorbar) if clim is None: internal_clim = np.nanmin(clims[0]), np.nanmax( clims[1]) else: internal_clim = clim if total_clim[0] is None or internal_clim[ 0] < total_clim[0]: total_clim[0] = internal_clim[0] if total_clim[1] is None or internal_clim[ 1] > total_clim[1]: total_clim[1] = internal_clim[1] colorbar = plot[0].qcodes_colorbar apply_color_scale_limits(colorbar, new_lim=tuple(total_clim)) # Set figure titles plot.fig.suptitle(samplefolder) if len(ids) < 6: plot.subplots[0].set_title(', '.join(map(str, ids))) else: plot.subplots[0].set_title(' - '.join( map(str, [ids[0], ids[-1]]))) plt.draw() # Save figure if savepng: if len(ids) == 1: title_png = samplefolder + CURRENT_EXPERIMENT[ 'png_subfolder'] + sep + '{}'.format(ids[0]) else: title_png = samplefolder + CURRENT_EXPERIMENT[ 'png_subfolder'] + sep + '{}-{}'.format( ids[0], ids[-1]) if l > 1: num = '{}'.format(j + 1) plt.savefig(title_png + '_{}_{}.png'.format(num, avg_sub), dpi=500) plots.append(plot) else: plots = None return data_list, plots
def fit(self, id, fitclass, do_plots=True, dataname=None, samplefolder=None, p0=None, **kwargs): str_id = '{0:03d}'.format(id) if samplefolder == None: check_experiment_is_initialized() path = qc.DataSet.location_provider.fmt.format(counter=str_id) data = qc.load_data(path) else: path = '{}{}{}'.format(samplefolder, sep, str_id) data = qc.load_data(path) # Get dataname to be fitted if dataname == None: # Function only made for a single dataname. Check there isnt more datanames in the dataset. key_data = [ key for key in data.arrays.keys() if "_set" not in key[-4:] ] if len(key_data) != 1: raise RuntimeError( 'Dataset has more than one dataname. Choose dataname to be fitted from: {}.' .format(', '.join(key_data))) else: # Check input dataname is in dataset. if dataname not in [ key for key in data.arrays.keys() if "_set" not in key[-4:] ]: raise RuntimeError( 'Dataname not in dataset. Input dataname was: \'{}\' while dataname(s) in dataset are: {}.' .format(dataname, ', '.join(key_data))) key_data = [dataname] # Do fits separately for 1D and 2D datasets keys_set = [key for key in data.arrays.keys() if "_set" in key[-4:]] # Do fit and plot for 1D data if len(keys_set) == 1: qcxdata = getattr(data, keys_set[0]) qcydata = getattr(data, key_data[0]) # Get initial guess on parameter is guess function is defined if (p0 == None and hasattr(fitclass, 'guess')): p0 = getattr(fitclass, 'guess')(qcxdata.ndarray, qcydata.ndarray) popt, pcov = curve_fit(fitclass.fun, qcxdata.ndarray, qcydata.ndarray, p0=p0, **kwargs) if do_plots: plot = self.plot_1D(qcxdata, qcydata, fitclass, popt) title_list = plot.get_default_title().split(sep) title_list.insert(-1, 'analysis') title = sep.join(title_list) plt.savefig("{}_{}.png".format(title, fitclass.name), dpi=500) return popt, pcov, plot else: return popt, pcov if len(keys_set) == 2: print('Fitting for 2D datasets not implemented.')
from qtt.parameterviewer import createParameterWidget if __name__ == '__main__' and not remote: p = createParameterWidget([ gates, ]) if __name__ == '__main__' and remote: p = createParameterWidgetRemote([ gates, ]) #%% load data and plot results if __name__ == '__main__' and 1: olddatadir = r'K:\ns\qt\spin-qubits\data\b057_data\2016 3dot experiment\data\2016-11-11\18-20-44_P2_P3' dataset_old = load_data(location=olddatadir) # qcodes.plots.pyqtgraph.QtPlot(dataset_old.measured, interval=0) plotje = qcodes.plots.qcmatplotlib.MatPlot(dataset_old.measured, interval=0) #%% delta x = 2 gates.P1.set(gates.P1.get() + Dot_delta['P1'] * x) gates.P2.set(gates.P2.get() + Dot_delta['P2'] * x) gates.P3.set(gates.P3.get() + Dot_delta['P3'] * x) #%% epsilon x = 2 gates.P1.set(gates.P1.get() + Dot_epsilon['P1'] * x) gates.P2.set(gates.P2.get() + Dot_epsilon['P2'] * x) gates.P3.set(gates.P3.get() + Dot_epsilon['P3'] * x)
def show_num(ids, samplefolder=None,useQT=False,avg_sub='',do_plots=True,savepng=True, fig_size=[6,4],clim=None,dataname=None,xlim=None,ylim=None,**kwargs): """ Show and return plot and data. Args: ids (number, list): id or list of ids of dataset(s) samplefolder (str): Sample folder if loading data from different sample than the initialized. useQT (boolean): If true plots with QTplot instead of matplotlib avg_sub (str: 'col' or 'row'): Subtracts average from either each collumn ('col') or each row ('row') do_plots: (boolean): if false no plots are produced. dataname (str): If given only plots dataset with that name savepng (boolean): If true saves matplotlib figure as png fig_size [6,4]: Figure size in inches clim [cmin,cmax]: Set min and max of colorbar to cmin and cmax respectrively xlim [xmin,xmax]: Set limits on x axis ylim [ymin,ymax]: set limits on y axis **kwargs: Are passed to plot function Returns: data, plots : returns the plots and the datasets """ if not isinstance(ids, collections.Iterable): ids = (ids,) data_list = [] keys_list = [] # Define samplefolder if samplefolder==None: check_experiment_is_initialized() samplefolder = qc.DataSet.location_provider.fmt.format(counter='') # Load all datasets into list for id in ids: path = samplefolder + '{0:03d}'.format(id) data = qc.load_data(path) data_list.append(data) # find datanames to be plotted if do_plots: if useQT and len(ids) is not 1: raise ValueError('qcodes.QtPlot does not support multigraph plotting. Set useQT=False to plot multiple datasets.') if dataname is not None: if dataname not in [key for key in data.arrays.keys() if "_set" not in key]: raise RuntimeError('Dataname not in dataset. Input dataname was: \'{}\'', \ 'while dataname(s) in dataset are: {}.'.format(dataname,', '.join(data.arrays.keys()))) keys = [dataname] else: keys = [key for key in data.arrays.keys() if "_set" not in key] keys_list.append(keys) if do_plots: unique_keys = list(set([item for sublist in keys_list for item in sublist])) plots = [] num = '' l = len(unique_keys) for j, key in enumerate(unique_keys): array_list = [] xlims = [[],[]] ylims = [[],[]] clims = [[],[]] # Find datasets containing data with dataname == key for data, keys in zip(data_list,keys_list): if key in keys: arrays = getattr(data, key) if avg_sub == 'row': for i in range(np.shape(arrays)[0]): arrays[i,:] -= np.nanmean(arrays[i,:]) if avg_sub == 'col': for i in range(np.shape(arrays)[1]): arrays[:,i] -= np.nanmean(arrays[:,i]) array_list.append(arrays) # Find axis limits for dataset if len(arrays.set_arrays)==2: xlims[0].append(np.nanmin(arrays.set_arrays[1])) xlims[1].append(np.nanmax(arrays.set_arrays[1])) ylims[0].append(np.nanmin(arrays.set_arrays[0])) ylims[1].append(np.nanmax(arrays.set_arrays[0])) clims[0].append(np.nanmin(arrays.ndarray)) clims[1].append(np.nanmax(arrays.ndarray)) else: xlims[0].append(np.nanmin(arrays.set_arrays[0])) xlims[1].append(np.nanmax(arrays.set_arrays[0])) ylims[0].append(np.nanmin(arrays.ndarray)) ylims[1].append(np.nanmax(arrays.ndarray)) if useQT: plot = QtPlot(array_list[0], fig_x_position=CURRENT_EXPERIMENT['plot_x_position'], **kwargs) title = "{} #{}".format(CURRENT_EXPERIMENT["sample_name"], '{}'.format(ids[0])) plot.subplots[0].setTitle(title) plot.subplots[0].showGrid(True, True) if savepng: print('Save plot only working for matplotlib figure.', \ 'Set useQT=False to save png.') else: plot = MatPlot(array_list, **kwargs) plot.rescale_axis() plot.fig.tight_layout(pad=3) plot.fig.set_size_inches(fig_size) # Set axis limits if xlim is None: plot[0].axes.set_xlim([np.nanmin(xlims[0]),np.nanmax(xlims[1])]) else: plot[0].axes.set_xlim(xlim) if ylim is None: plot[0].axes.set_ylim([np.nanmin(ylims[0]),np.nanmax(ylims[1])]) else: plot[0].axes.set_ylim(ylim) if len(arrays.set_arrays)==2: for i in range(len(array_list)): if clim is None: plot[0].get_children()[i].set_clim(np.nanmin(clims[0]),np.nanmax(clims[1])) else: plot[0].get_children()[i].set_clim(clim) # Set figure titles plot.fig.suptitle(samplefolder) if len(ids)<6: plot.subplots[0].set_title(', '.join(map(str,ids))) else: plot.subplots[0].set_title(' - '.join(map(str,[ids[0],ids[-1]]))) plt.draw() # Save figure if savepng: if len(ids) == 1: title_png = samplefolder+CURRENT_EXPERIMENT['png_subfolder']+sep+'{}'.format(ids[0]) else: title_png = samplefolder+CURRENT_EXPERIMENT['png_subfolder']+sep+'{}-{}'.format(ids[0],ids[-1]) if l>1: num = '{}'.format(j+1) plt.savefig(title_png+'_{}_{}.png'.format(num,avg_sub),dpi=500) plots.append(plot) else: plots = None return data_list, plots
def show_num(id, samplefolder=None, useQT=False, ave_col=False, ave_row=False, do_plots=True, savepng=False, clim=None, **kwargs): """ Show and return plot and data for id in current instrument. Args: id(number): id of instrument useQT (boolean): If true plots with QTplot instead of matplotlib ave_col (boolean): If true subtracts average from each collumn ave_row (boolean): If true subtracts average from each row do_plots: Default False: if false no plots are produced. savepng (boolean): If true saves matplotlib figure as png clim [cmin,cmax]: Set min and max of colorbar to cmin and cmax respectrively **kwargs: Are passed to plot function Returns: data, plots : returns the plot and the dataset """ str_id = '{0:03d}'.format(id) if samplefolder==None: check_experiment_is_initialized() path = qc.DataSet.location_provider.fmt.format(counter=str_id) data = qc.load_data(path) else: path = '{}{}{}'.format(samplefolder,sep,str_id) data = qc.load_data(path) keys = [key for key in data.arrays.keys() if "_set" not in key[-4:]] if do_plots: plots = [] avestr = '' num = '' l = len(keys) for j, value in enumerate(keys): arrays = getattr(data, value) if ave_row: for i in range(np.shape(arrays)[0]): arrays[i,:] -= arrays[i,:].mean() avestr = '_row' if ave_col: for i in range(np.shape(arrays)[1]): arrays[:,i] -= arrays[:,i].mean() avestr = '_col' if useQT: plot = QtPlot( getattr(data, value), fig_x_position=CURRENT_EXPERIMENT['plot_x_position'], **kwargs) title = "{} #{}".format(CURRENT_EXPERIMENT["sample_name"], str_id) plot.subplots[0].setTitle(title) plot.subplots[0].showGrid(True, True) if savepng: print('Save plot only working for matplotlib figure. Set useQT=False to save png.') else: plot = MatPlot(getattr(data, value), **kwargs) plot.rescale_axis() plot.fig.tight_layout(pad=3) if clim is not None: mesh = plot[0].get_children()[0] mesh.set_clim(clim[0],clim[1]) plt.draw() if savepng: title_list_png = plot.get_default_title().split(sep) title_list_png.insert(-1, CURRENT_EXPERIMENT['png_subfolder']) title_png = sep.join(title_list_png) if l>1: num = '_{}'.format(j+1) plt.savefig("{}{}{}.png".format(title_png,num,avestr),dpi=500) plots.append(plot) else: plots = None return data, plots