2022-07-30 21:50:53 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
"""
|
|
|
|
Created on Mon Mar 15 15:08:46 2021
|
|
|
|
|
|
|
|
@author: Nick Sauerwein
|
|
|
|
"""
|
|
|
|
|
|
|
|
import lyse
|
|
|
|
import numpy as np
|
|
|
|
|
|
|
|
import os.path
|
|
|
|
import time
|
|
|
|
|
|
|
|
import h5py
|
|
|
|
|
|
|
|
|
|
|
|
def get_mtime(filename):
|
|
|
|
return time.ctime(os.path.getmtime(filename))
|
|
|
|
|
|
|
|
|
|
|
|
class DataExtractorManager:
|
|
|
|
|
|
|
|
def __init__(self, data_path=None):
|
|
|
|
|
|
|
|
self.data_extractors = {}
|
|
|
|
self.data_path = data_path
|
|
|
|
self.local_data_changed = False
|
|
|
|
|
|
|
|
def update_local_data(self, h5_path):
|
2022-08-10 16:40:15 +02:00
|
|
|
|
|
|
|
h5_path = h5_path.replace('\n', '')
|
|
|
|
h5_path = os.path.join(h5_path)
|
|
|
|
|
2022-07-30 21:50:53 +02:00
|
|
|
with h5py.File(h5_path, 'r') as h5_file:
|
|
|
|
self.local_data_changed = False
|
|
|
|
for key in self.data_extractors:
|
|
|
|
self.data_extractors[key].update_local_data(h5_path, h5_file=h5_file)
|
|
|
|
if self.data_extractors[key].local_data_changed:
|
|
|
|
self.local_data_changed = True
|
|
|
|
|
|
|
|
def clean_memory(self, h5_paths):
|
|
|
|
for key in self.data_extractors:
|
|
|
|
self.data_extractors[key].clean_memory(h5_paths)
|
|
|
|
|
|
|
|
def __getitem__(self, key):
|
|
|
|
return self.data_extractors[key]
|
|
|
|
|
|
|
|
def __setitem__(self, key, de):
|
|
|
|
self.data_extractors[key] = de
|
|
|
|
|
|
|
|
def add_data_extractor(self, label, type, data_path):
|
|
|
|
|
|
|
|
result = DataExtractor(label, type, data_path)
|
|
|
|
self.data_extractors[label] = result
|
|
|
|
|
|
|
|
return result
|
|
|
|
|
|
|
|
def get_data(self, h5_path, h5_file=None):
|
|
|
|
result = {}
|
|
|
|
for key in self.data_extractors:
|
|
|
|
result[key] = self.data_extractors[key].get_data(h5_path, h5_file=h5_file)
|
|
|
|
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
|
|
|
class DataExtractor:
|
|
|
|
def __init__(self, label, type, data_path, load_to_ram=True):
|
|
|
|
|
|
|
|
self.load_to_ram = load_to_ram
|
|
|
|
|
|
|
|
self.local_datas = {}
|
|
|
|
self.local_mtimes = {}
|
|
|
|
|
|
|
|
if self.load_to_ram:
|
|
|
|
self.local_data_changed = True
|
|
|
|
|
|
|
|
# The path of data in hdf5 file
|
|
|
|
self.data_path = data_path
|
|
|
|
self.data_type = type
|
|
|
|
self.data_label = label
|
|
|
|
|
2022-08-10 16:40:15 +02:00
|
|
|
def extract_data(self, h5_file):
|
2022-07-30 21:50:53 +02:00
|
|
|
|
|
|
|
result = None
|
|
|
|
|
2022-08-10 16:40:15 +02:00
|
|
|
try:
|
|
|
|
result = h5_file[self.data_path][()]
|
|
|
|
result = np.array(result)
|
|
|
|
except:
|
|
|
|
result = None
|
2022-08-09 15:37:34 +02:00
|
|
|
|
2022-07-30 21:50:53 +02:00
|
|
|
return result
|
|
|
|
|
|
|
|
def update_local_data(self, h5_path, h5_file=None):
|
|
|
|
if h5_path in self.local_datas and self.local_mtimes[h5_path] == get_mtime(h5_path):
|
|
|
|
self.local_data_changed = False
|
|
|
|
elif self.load_to_ram:
|
2022-08-10 16:40:15 +02:00
|
|
|
self.local_datas[h5_path] = self.extract_data(h5_file)
|
2022-07-30 21:50:53 +02:00
|
|
|
self.local_mtimes[h5_path] = get_mtime(h5_path)
|
|
|
|
self.local_data_changed = True
|
|
|
|
|
|
|
|
def update_local_datas(self):
|
|
|
|
for key in self.local_datas:
|
|
|
|
self.update_local_data(key)
|
|
|
|
|
|
|
|
def get_data(self, h5_path, h5_file=None):
|
|
|
|
|
|
|
|
if self.load_to_ram:
|
|
|
|
|
|
|
|
self.update_local_data(h5_path)
|
|
|
|
|
|
|
|
return self.local_datas[h5_path]
|
|
|
|
else:
|
|
|
|
return self.extract_data(h5_path, h5_file=h5_file)
|
|
|
|
|
|
|
|
def clean_memory(self, h5_paths):
|
|
|
|
for key in list(self.local_datas):
|
2022-08-10 16:40:15 +02:00
|
|
|
if key not in h5_paths.tolist():
|
2022-07-30 21:50:53 +02:00
|
|
|
del self.local_datas[key]
|
|
|
|
del self.local_mtimes[key]
|
|
|
|
|
|
|
|
self.local_data_changed = True
|
|
|
|
self.update_local_datas()
|