def __init__(self, timestamp, caching=False, set_label=None, temp_label='Sample temperature', recalculate=False): """Save all data in 'DATA' dict object""" # Various attributes self.pickle_format = 'tpd__{}__{}.pickle' self.exps = {} #self.caching = True if CACHING is True or caching is True else False self.caching = caching if CACHING is not None: self.caching = CACHING self.timestamp = {} self.timestamp['date'] = timestamp self.timestamp['unix'] = time.mktime(datetime.datetime.strptime( timestamp, "%Y-%m-%d %H:%M:%S").timetuple()) # Try to load from file cache_file = [] load_from_pickle = False if self.caching and not recalculate: list_of_files = path.os.listdir(CACHE_DIR) for name in list_of_files: if name.startswith('tpd__{}__'.format(timestamp)): cache_file.append(name) if len(cache_file) == 1: with open(path.join(CACHE_DIR, cache_file[0]), 'rb') as f: copy = pickle.load(f) self.name = copy.name self.labels = copy.labels self.data = copy.data self.exps = copy.exps load_from_pickle = True print(' *** Loaded TPD data from pickle: {} ***'.format(self.name)) elif len(cache_file) > 1: print('Found multiple pickles matching timestamp:\n{}'.format(cache_file)) # Get data from database if load_from_pickle is False: # Connect to database db = Cinfdata('omicron', use_caching=caching, grouping_column='time', ) # Get data in unit seconds group_data = db.get_data_group(timestamp, scaling_factors=(1E-3, None)) group_meta = db.get_metadata_group(timestamp) # Get a list of labels and group data print(group_meta) self.name = group_meta[list(group_meta.keys())[0]]['Comment'].replace('/', '') self.labels = [group_meta[key]['mass_label'] for key in group_data.keys()] self.data = {group_meta[key]['mass_label']: group_data[key] for key in group_data.keys()} print('Loaded data from Experiment: "{}"'.format(self.name)) self.isolate_experiments(set_label=set_label, temp_label=temp_label)
def __init__(self, timestamp, caching=False): """Save all data in 'DATA' dict object""" # Connect to database db = Cinfdata('omicron', use_caching=caching, grouping_column='time', ) # Get data in unit seconds group_data = db.get_data_group(timestamp, scaling_factors=(1E-3, None)) group_meta = db.get_metadata_group(timestamp) # Get a list of labels and group data self.name = group_meta[list(group_meta.keys())[0]]['Comment'] self.labels = [group_meta[key]['mass_label'] for key in group_data.keys()] self.data = {group_meta[key]['mass_label']: group_data[key] for key in group_data.keys()} print('Loaded data from Experiment: "{}"'.format(self.name))
def __init__(self, timestamp, caching=True): """Save all data in 'DATA' dict object""" # Connect to database db = Cinfdata('omicron', use_caching=caching, grouping_column='time', ) # Get data in unit seconds group_data = db.get_data_group(timestamp, scaling_factors=(1E-3, None)) group_meta = db.get_metadata_group(timestamp) # Get a list of labels and group data self.name = group_meta[list(group_meta.keys())[0]]['Comment'] self.labels = [group_meta[key]['mass_label'] for key in group_data.keys()] self.data = {group_meta[key]['mass_label']: group_data[key] for key in group_data.keys()} print('Loaded data from Experiment: "{}"'.format(self.name))
def download_cinfdata_set( setup="sniffer", group_id=None, grouping_column=None, **kwargs ): if grouping_column is None: grouping_column, group_id = kwargs.popitem() from .Combining import synchronize try: from cinfdata import Cinfdata except ImportError: print( "the cinfdata module must be on your python path. It's here: \n" + "https://github.com/CINF/cinf_database/blob/master/cinfdata.py" ) try: cinfd = Cinfdata( setup, grouping_column=grouping_column, allow_wildcards=True, label_column="mass_label", ) except: raise # untill I know exactly which error I'm trying to catch. print("couldn't connect. You should run gstm") # os.system('gstm') raise RuntimeError("Couldn't connect to cinfdata!") # obj = cinfd.get_metadata_group('2018-03-30 14:13:17') # all_datasets = cinfd.get_metadata_group('%') # the_list = [(ID, d['time'], d['comment']) for ID, d in all_datasets.items()] # print(the_list) obj = cinfd.get_metadata_group(group_id) # print(str(obj)) # idlists = {} # keys will be time as string. values will be corresponding id's for key, value in obj.items(): # label = value['mass_label'] # print(label) timestamp = str(value["time"]) if timestamp not in idlists: idlists[timestamp] = [] idlists[timestamp] += [value["id"]] datasets = {} for timestamp, idlist in idlists.items(): if len(idlist) == 0: print("No data associated with timestamp '" + timestamp + "'.") continue dataset = {"title": timestamp, "data_type": "MS"} metadatas = dict([(i, cinfd.get_metadata(i)) for i in idlist]) unixtimes = [metadatas[i]["unixtime"] for i in idlist] if len(set(unixtimes)) > 1: msg = "unix times don't match for timestamp '" + timestamp + "'!" raise ValueError(msg) dataset["tstamp"] = unixtimes[0] dataset["timestamp"] = metadatas[idlist[0]]["time"].strftime("%H:%M:%S") labels = [metadatas[i]["mass_label"] for i in idlist] if "Mass Scan" in labels: dataset["scan_type"] = "mass" else: dataset["scan_type"] = "time" dataset["data_cols"] = set() dataset["timecols"] = {} for i in idlist: # avoiding id since it's got a builtin meaning data = cinfd.get_data(i) label = metadatas[i]["mass_label"] if len(data.shape) == 1: dataset[label] = data dataset["data_cols"].add(label) elif data.shape[1] == 2: x = data[:, 0] y = data[:, 1] x_label = label + "-x" y_label = label + "-y" dataset["timecols"][y_label] = x_label # Fixed 20B26!!! dataset[x_label] = x * 1e-3 # cinfdata saves time in ms!!! dataset[y_label] = y dataset["data_cols"].add(x_label) dataset["data_cols"].add(y_label) datasets[timestamp] = dataset timescans = [ dataset for dataset in datasets.values() if dataset["scan_type"] == "time" ] combined = synchronize(timescans, t_zero="first") return combined
def download_cinfdata_set(setup='sniffer', group_id=None, grouping_column=None, **kwargs): if grouping_column is None: grouping_column, group_id = kwargs.popitem() from .Combining import synchronize try: from cinfdata import Cinfdata except ImportError: print( 'the cinfdata module must be on your python path. It\'s here: \n' + 'https://github.com/CINF/cinf_database/blob/master/cinfdata.py') try: cinfd = Cinfdata(setup, grouping_column=grouping_column, allow_wildcards=True, label_column='mass_label') except: raise #untill I know exactly which error I'm trying to catch. print('couldn\'t connect. You should run gstm') #os.system('gstm') raise RuntimeError('Couldn\'t connect to cinfdata!') #obj = cinfd.get_metadata_group('2018-03-30 14:13:17') #all_datasets = cinfd.get_metadata_group('%') #the_list = [(ID, d['time'], d['comment']) for ID, d in all_datasets.items()] #print(the_list) obj = cinfd.get_metadata_group(group_id) #print(str(obj)) # idlists = { } # keys will be time as string. values will be corresponding id's for key, value in obj.items(): #label = value['mass_label'] #print(label) timestamp = str(value['time']) if timestamp not in idlists: idlists[timestamp] = [] idlists[timestamp] += [value['id']] datasets = {} for timestamp, idlist in idlists.items(): if len(idlist) == 0: print('No data associated with timestamp \'' + timestamp + '\'.') continue dataset = {'title': timestamp, 'data_type': 'MS'} metadatas = dict([(i, cinfd.get_metadata(i)) for i in idlist]) unixtimes = [metadatas[i]['unixtime'] for i in idlist] if len(set(unixtimes)) > 1: msg = 'unix times don\'t match for timestamp \'' + timestamp + '\'!' raise ValueError(msg) dataset['tstamp'] = unixtimes[0] dataset['timestamp'] = metadatas[idlist[0]]['time'].strftime( '%H:%M:%S') labels = [metadatas[i]['mass_label'] for i in idlist] if 'Mass Scan' in labels: dataset['scan_type'] = 'mass' else: dataset['scan_type'] = 'time' dataset['data_cols'] = set() dataset['timecols'] = {} for i in idlist: #avoiding id since it's got a builtin meaning data = cinfd.get_data(i) label = metadatas[i]['mass_label'] if len(data.shape) == 1: dataset[label] = data dataset['data_cols'].add(label) elif data.shape[1] == 2: x = data[:, 0] y = data[:, 1] x_label = label + '-x' y_label = label + '-y' dataset['timecols'][x_label] = y_label dataset[x_label] = x * 1e-3 # cinfdata saves time in ms!!! dataset[y_label] = y dataset['data_cols'].add(x_label) dataset['data_cols'].add(y_label) datasets[timestamp] = dataset timescans = [ dataset for dataset in datasets.values() if dataset['scan_type'] == 'time' ] combined = synchronize(timescans, t_zero='first') return combined