DataManager.py 7.33 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 109 110 111 112
        
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

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

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

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

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