DataManager.py 7.55 KB
Newer Older
Nick Sauerwein's avatar
Nick Sauerwein committed
1 2 3 4 5
import numpy as np
import datetime
import glob


6
import Globals as g
Nick Sauerwein's avatar
Nick Sauerwein committed
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36

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):
37 38
        print_dict(data, plot = plot)
        
39
def plot_measure(data, devices, plot_config = {}, sleep = 0):
40 41 42 43 44 45
    if plot_config == {}:
        
        measure, config = (data['measure'], data['config'])
        for device in devices:
            device_name = type(device).__name__
            if hasattr(device, 'plot_measure'):
46
                device.plot_measure(measure[device_name], config = config[device_name])
47 48 49 50 51 52 53 54 55 56 57 58
        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__
Sauerwein Nick Jacob's avatar
Sauerwein Nick Jacob committed
59
            try:
60
                g.plot_hist[device_name]
Sauerwein Nick Jacob's avatar
Sauerwein Nick Jacob committed
61
            except:
62 63 64 65 66
                g.plot_hist[device_name] = {}

            for item in plot_config[device_name].items():
                if item[0] == 'plot_measure' :
                    if item[1]:
67
                        device.plot_measure(measure[device_name], config = config[device_name])
68
                else:
69 70
                    if not item[1]:
                        continue
71 72 73 74 75 76
                    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()
77 78 79 80 81
                    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))
82
    plt.pause(sleep)
83 84
                    
    #important: sort by time and plot sepecific elements
85
    
Nick Sauerwein's avatar
Nick Sauerwein committed
86 87 88 89 90 91 92 93 94 95 96 97 98

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__
Hermann Benedikt's avatar
Hermann Benedikt committed
99 100 101 102
        try:
            device.set_config(config[device_name])
        except:
            print ('Device '+device_name+' is not in this config')
Nick Sauerwein's avatar
Nick Sauerwein committed
103 104 105 106 107 108
        
def get_config(devices):        
    time = str(datetime.datetime.now()).replace(':','-').replace(' ','_')
    config = {'time': time}
        
    for device in devices:
109 110
        import time
        t = time.time()
Nick Sauerwein's avatar
Nick Sauerwein committed
111 112
        device_name = type(device).__name__ 
        config[device_name] = device.get_config()
113
        print (device_name,' config time: ',time.time() - t)
Nick Sauerwein's avatar
Nick Sauerwein committed
114 115
    return config

116
def measure(devices, devices2measure = 'all'):
Nick Sauerwein's avatar
Nick Sauerwein committed
117
    time = str(datetime.datetime.now()).replace(':','-').replace(' ','_')
Sauerwein Nick Jacob's avatar
Sauerwein Nick Jacob committed
118 119
    
    data = {'time': time}    
Nick Sauerwein's avatar
Nick Sauerwein committed
120

Sauerwein Nick Jacob's avatar
Sauerwein Nick Jacob committed
121 122
    data['config'] = get_config(devices)
    data['measure'] = {}
123 124 125 126 127
    
    if devices2measure == 'all':
        devices2measure = devices
    
    for device in devices2measure:
Nick Sauerwein's avatar
Nick Sauerwein committed
128 129
        device_name = type(device).__name__
        if hasattr(device, 'measure'):
130 131
            import time
            t = time.time()
Sauerwein Nick Jacob's avatar
Sauerwein Nick Jacob committed
132
            data['measure'][device_name] = device.measure()
Hermann Benedikt's avatar
Hermann Benedikt committed
133
            print (device_name,' measured')
134
            print (device_name,' measure time: ',time.time() - t)
Nick Sauerwein's avatar
Nick Sauerwein committed
135
    
Sauerwein Nick Jacob's avatar
Sauerwein Nick Jacob committed
136
    return data     
Nick Sauerwein's avatar
Nick Sauerwein committed
137 138 139 140 141
    
class IO:
    
    def __init__(self, path):
        self.path = path
Sauerwein Nick Jacob's avatar
Sauerwein Nick Jacob committed
142 143 144 145 146 147 148 149 150 151
        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 = ''):
Nick Sauerwein's avatar
Nick Sauerwein committed
152 153 154 155 156 157 158 159
        
        data['comment'] = comment

        if name == None:
            name = kind+data['time']

        np.save(self.path+name+'.npy',data)
        print (kind+' saved as '+name+'.npy')
160 161 162 163 164 165 166 167 168 169 170 171
        
        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)
172
            import time
173 174 175 176
            self.logbook.post('Configuration with Comment: '+comment,
                         attributes={'Titel':'Configuration of LWFA',
                                     'Autor':'Sauerwein Nick Jacob (SQ84)',
                                     'Projekt': 'LPWA',
177 178
                                     'Dates': int(time.time())}, 
                                     attachments=[obj])
Nick Sauerwein's avatar
Nick Sauerwein committed
179 180
        return data
    
181 182 183
    def load(self, name_id = -1, kind = 'config', gui = False, elog = False):
        if elog:
            import requests
Sauerwein Nick Jacob's avatar
Sauerwein Nick Jacob committed
184 185 186
            response = requests.get(self.logbook.read(name_id)[-1][0],
                                    verify=False, 
                                    auth=('sauerwein_n', 'Dark374Fields!'))
187
            return eval(response.content.decode())
188
        if gui:
189
            from tkinter import Tk
190
            from tkinter.filedialog import askopenfilename
191 192 193 194 195
            root = Tk()
            root.withdraw()
            root.update()
            filename = askopenfilename(initialdir=self.path)
            root.distroy()
196
            return np.load(filename).item()
Nick Sauerwein's avatar
Nick Sauerwein committed
197 198 199 200
        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()
    
201
    def search(self, key_dict = {}, kind = 'config', output = True):
Nick Sauerwein's avatar
Nick Sauerwein committed
202
        datas = []
203 204
        if output:
            print ('n \t ',[item[0] for item in key_dict.items()])
Nick Sauerwein's avatar
Nick Sauerwein committed
205 206 207 208
        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()]
209 210
            if output:
                print (n,' \t ',test)
Nick Sauerwein's avatar
Nick Sauerwein committed
211 212 213 214 215 216 217 218 219 220 221 222 223
            if np.all(test) or len(key_dict) == 0:
                datas += [data]
            n += 1
        return datas