import numpy as np import datetime import glob import Globals as g import matplotlib.pyplot as plt def print_dict(dictionary, ident = '', braces=1, plot = True): """ Recursively prints nested dictionaries.""" for key, value in dictionary.items(): if isinstance(value, dict): print ('%s%s%s%s' %(ident,braces*'[',key,braces*']')) print_dict(value, ident+' ', braces+1) else: if type(value).__name__ == 'ndarray' and plot == True: print (ident+key+' (see plot below)' ) if value.ndim == 1: plt.figure() plt.plot(value) plt.title(key+' level = '+str(braces)) if value.ndim == 2: from matplotlib import cm plt.figure() plt.imshow(value, cmap='gray') plt.title(key+' level = '+str(braces)) plt.colorbar() else: print (ident+'%s = %s' %(key, value)) def print_data(data, plot = True): print_dict(data, plot = plot) def plot_measure(data, devices, plot_config = {}, sleep = 0): if plot_config == {}: measure, config = (data['measure'], data['config']) for device in devices: device_name = type(device).__name__ if hasattr(device, 'plot_measure'): device.plot_measure(measure[device_name], config = config[device_name]) return else: try: g.plot_hist except: g.plot_hist = {} measure, config = (data['measure'], data['config']) for device in devices: device_name = type(device).__name__ try: g.plot_hist[device_name] except: g.plot_hist[device_name] = {} for item in plot_config[device_name].items(): if item[0] == 'plot_measure' : if item[1]: device.plot_measure(measure[device_name], config = config[device_name]) else: if not item[1]: continue try: g.plot_hist[device_name][item[0]] =g.plot_hist[device_name][item[0]] + [measure[device_name][item[0]]] except: g.plot_hist[device_name][item[0]] = [measure[device_name][item[0]]] plt.figure('history: '+device_name+' '+item[0]) plt.clf() xx = range(-len(g.plot_hist[device_name][item[0]])+1,1) plt.plot(xx,g.plot_hist[device_name][item[0]]) plt.xlabel('last measurements') plt.ylabel('value') plt.hlines([min(g.plot_hist[device_name][item[0]]), max(g.plot_hist[device_name][item[0]])],min(xx), max(xx)) plt.pause(sleep) #important: sort by time and plot sepecific elements def dict_contains(d, item): for it in d.items(): if isinstance(it[1], dict): if dict_contains(it[1], item): return True if it == item: return True return False def set_config(devices, config): for device in devices: device_name = type(device).__name__ try: device.set_config(config[device_name]) except: print ('Device '+device_name+' is not in this config') def get_config(devices): time = str(datetime.datetime.now()).replace(':','-').replace(' ','_') config = {'time': time} for device in devices: device_name = type(device).__name__ config[device_name] = device.get_config() return config def measure(devices, devices2measure = 'all'): time = str(datetime.datetime.now()).replace(':','-').replace(' ','_') data = {'time': time} data['config'] = get_config(devices) data['measure'] = {} if devices2measure == 'all': devices2measure = devices for device in devices2measure: device_name = type(device).__name__ if hasattr(device, 'measure'): data['measure'][device_name] = device.measure() print (device_name,' measured') return data class IO: def __init__(self, path): self.path = path try: import elog as el self.logbook = el.open('https://elog-gfa.psi.ch', 'AMAS', user='sauerwein_n', password='Dark374Fields!', use_ssl=True, subdir='', encrypt_pwd=True) except: print ('This computer does not support elog') def save(self, data, name = None, elog = False, kind = 'config', comment = ''): data['comment'] = comment if name == None: name = kind+data['time'] np.save(self.path+name+'.npy',data) print (kind+' saved as '+name+'.npy') if elog: class elogobj: def __init__(self,dic): self.name = 'config.txt' self.dic = dic def read(self): return str(self.dic) obj = elogobj(data) import time self.logbook.post('Configuration with Comment: '+comment, attributes={'Titel':'Configuration of LWFA', 'Autor':'Sauerwein Nick Jacob (SQ84)', 'Projekt': 'LPWA', 'Dates': int(time.time())}, attachments=[obj]) return data def load(self, name_id = -1, kind = 'config', gui = False, elog = False): if elog: import requests response = requests.get(self.logbook.read(name_id)[-1][0], verify=False, auth=('sauerwein_n', 'Dark374Fields!')) return eval(response.content.decode()) if gui: from tkinter import Tk from tkinter.filedialog import askopenfilename root = Tk() root.withdraw() root.update() filename = askopenfilename(initialdir=self.path) root.distroy() return np.load(filename).item() if type(name_id) == int: return np.load([filen for filen in glob.iglob(self.path+kind+'*.npy')][name_id]).item() return np.load(self.path+str(name_id)+'.npy').item() def search(self, key_dict = {}, kind = 'config', output = True): datas = [] if output: print ('n \t ',[item[0] for item in key_dict.items()]) n = 0 for filename in glob.iglob(self.path+kind+'*.npy'): data = np.load(filename).item() test = [dict_contains(data,item) for item in key_dict.items()] if output: print (n,' \t ',test) if np.all(test) or len(key_dict) == 0: datas += [data] n += 1 return datas