def import_data(ID, setup='omicron'): db = Cinfdata(setup, use_caching=True) data = db.get_data(ID) try: data[:,0] = data[:,0]/1000 except: meta = db.get_metadata(ID) print('"{}" data for ID {} is empty and set to "None"'.format(meta['mass_label'], ID)) data = None return data
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
import datetime import numpy as np from cinfdata import Cinfdata DATA_CHECKSUM = 12525.0000008 METADATA = { 'comment': '', 'tof_liner_voltage': None, 'tof_iterations': None, 'timestep': 0.1, 'id': 5417L, 'preamp_range': -7, 'tof_pulse_width': None, 'pre_wait_time': None, 'tof_ion_energy': None, 'tof_R2_voltage': None, 'mass_label': None, 'tof_R1_voltage': None, 'sem_voltage': 1798.83, u'unixtime': 1464261944L, 'type': 4L, 'pass_energy': None, 'time': datetime.datetime(2016, 5, 26, 13, 25, 44), 'sample_temperature': None, 'tof_focus_voltage': None, 'tof_pulse_voltage': None, 'tof_emission_current': None, 'tof_deflection_voltage': None, 'tof_p1_2': None, 'tof_p1_0': None, 'tof_p1_1': None, 'tof_lens_E': None, 'tof_lens_D': None, 'tof_lens_A': None, 'tof_lens_C': None, 'tof_lens_B': None } cinfdata = Cinfdata('tof', use_caching=True) # Get from database assert np.isclose(cinfdata.get_data(5417).sum(), DATA_CHECKSUM) assert cinfdata.get_metadata(5417) == METADATA print('Test database OK') # And now fetch from cache assert np.isclose(cinfdata.get_data(5417).sum(), DATA_CHECKSUM) assert cinfdata.get_metadata(5417) == METADATA print('Test cache OK')
from cinfdata import Cinfdata db = Cinfdata('stm312', use_caching=True) spectrum = db.get_data(6688) metadata = db.get_metadata(6688)
'tof_R1_voltage': None, 'sem_voltage': 1798.83, u'unixtime': 1464261944L, 'type': 4L, 'pass_energy': None, 'time': datetime.datetime(2016, 5, 26, 13, 25, 44), 'sample_temperature': None, 'tof_focus_voltage': None, 'tof_pulse_voltage': None, 'tof_emission_current': None, 'tof_deflection_voltage': None, 'tof_p1_2': None, 'tof_p1_0': None, 'tof_p1_1': None, 'tof_lens_E': None, 'tof_lens_D': None, 'tof_lens_A': None, 'tof_lens_C': None, 'tof_lens_B': None } cinfdata = Cinfdata('tof', use_caching=True) # Get from database assert np.isclose(cinfdata.get_data(5417).sum(), DATA_CHECKSUM) assert cinfdata.get_metadata(5417) == METADATA print('Test database OK') # And now fetch from cache assert np.isclose(cinfdata.get_data(5417).sum(), DATA_CHECKSUM) assert cinfdata.get_metadata(5417) == METADATA print('Test cache OK')
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
from cinfdata import Cinfdata from matplotlib import pyplot as plt db = Cinfdata('stm312') spectrum = db.get_data(6688) metadata = db.get_metadata(6688) plt.plot(spectrum[:, 0], spectrum[:, 1]) plt.title(metadata['Comment']) plt.show()