def save_data_as(self): if self.new_fname is not None and self.new_fname != "": with self.file_lock: with catch(self.sphere.data_file, 'r') as f1: with catch(self.new_fname, 'w') as f2: for key in f1: f1.copy(key, f2) for attr in f1.attrs: f2.attrs[attr] = f1.attrs[attr] self.new_fname = None
def bai_2d_all(self): """Integrates all arches 2d. Note, does not call sphere method directly, handles same functions but broken up for updates after each image. """ self.data_2d.clear() with self.sphere.sphere_lock: if self.sphere.static: self.sphere.bai_2d = int_2d_data_static() else: self.sphere.bai_2d = int_2d_data() for arch in self.sphere.arches: if self.sphere.static: arch.static = True if self.sphere.gi: arch.gi = True arch.integrate_2d(**self.sphere.bai_2d_args) self.sphere.arches[arch.idx] = arch self.sphere._update_bai_2d(arch) self.data_2d[int(arch.idx)] = { 'map_raw': arch.map_raw, 'mask': arch.mask, 'int_2d': arch.int_2d } self.update.emit(arch.idx) with self.file_lock: with catch(self.sphere.data_file, 'a') as file: ut.dict_to_h5(self.sphere.bai_2d_args, file, 'bai_2d_args')
def load_arches_data(self, arch_ids, load_2d): """Loads data from hdf5 file and sets attributes. args: file: h5py file or group object """ # ic() with catch(self.sphere.data_file, 'r') as file: # ic(arch_ids) for idx in arch_ids: try: # ic(idx) arch = EwaldArch(idx=idx, static=True, gi=self.sphere.gi) if not load_2d: # arch.load_from_h5(file['arches'], load_2d=False) self.load_arch_data(file['arches'], arch, idx, load_2d=False) self.data_1d[int(idx)] = arch.copy(include_2d=False) # ic('loaded 1D data', self.data_1d.keys()) else: try: if len(arch.int_2d.i_qChi) == 0: pass except TypeError: arch.load_from_h5(file['arches'], load_2d=True) self.data_1d[int(idx)] = arch.copy(include_2d=False) self.data_2d[int(idx)] = { 'map_raw': arch.map_raw, 'mask': arch.mask, 'int_2d': arch.int_2d } # ic('loaded 1 and 2D data', self.data_1d.keys(), self.data_2d.keys()) # ic(idx, self.arches['add_idxs'], self.arches['sub_idxs']) if idx in self.arches['add_idxs']: self.arches['sum_int_2d'] += self.data_2d[int( idx)]['int_2d'] self.arches['sum_map_raw'] += self.data_2d[int( idx)]['map_raw'] elif idx in self.arches['sub_idxs']: self.arches['sum_int_2d'] -= self.data_2d[int( idx)]['int_2d'] self.arches['sum_map_raw'] -= self.data_2d[int( idx)]['map_raw'] except KeyError: pass
def __getitem__(self, idx): """Initializes a new EwaldArch object and loads data from file into it. """ if idx in self.index: arch = EwaldArch(idx, static=self.static, gi=self.gi) # invoke the lock to prevent conflicts with self.file_lock: # use catch to avoid oserrors which will resolve with time. with catch(self.data_file, 'r') as f: arch.load_from_h5(f['arches']) return arch else: raise KeyError(f"Arch not found with {idx} index")
def __setitem__(self, idx, arch): """Sets the arch at location idx to be the new arch. If an arch is stored with the same idx, replaces the data with the new data. """ # invoke the lock to prevent conflicts with self.file_lock: # use catch to avoid oserrors which will resolve with time. with catch(self.data_file, 'a') as f: if 'arches' not in f: f.create_group('arches') if idx != arch.idx: arch.idx = idx arch.save_to_h5(f['arches']) if arch.idx not in self.index: self.index.append(arch.idx)
def bai_2d_all(self): """Integrates all arches 2d. Note, does not call sphere method directly, handles same functions but broken up for updates after each image. """ with self.sphere.sphere_lock: self.sphere.bai_2d = int_2d_data() for arch in self.sphere.arches: arch.integrate_2d(**self.sphere.bai_2d_args, global_mask=self.sphere.global_mask) # self.sphere.arches[arch.idx] = arch self.sphere._update_bai_2d(arch) self.update.emit(arch.idx) with self.file_lock: with catch(self.sphere.data_file, 'a') as file: ut.dict_to_h5(self.sphere.bai_2d_args, file, 'bai_2d_args')
def load_arches(self): # ic() with self.file_lock: with catch(self.sphere.data_file, 'r') as file: # ic(self.arch_ids) for idx in self.arch_ids: try: # ic(idx) arch = EwaldArch(idx=idx, static=True, gi=self.sphere.gi) arch.load_from_h5(file['arches'], load_2d=self.update_2d) self.data_1d[int(idx)] = arch.copy(include_2d=False) # ic('loaded 1D data', self.data_1d.keys()) if self.update_2d: try: if len(arch.int_2d.i_qChi) == 0: pass except TypeError: arch.load_from_h5(file['arches'], load_2d=self.update_2d) self.data_2d[int(idx)] = { 'map_raw': arch.map_raw, 'mask': arch.mask, 'int_2d': arch.int_2d } if idx in self.arches['add_idxs']: self.arches['sum_int_2d'] += self.data_2d[int( idx)]['int_2d'] self.arches['sum_map_raw'] += self.data_2d[int( idx)]['map_raw'] elif idx in self.arches['sub_idxs']: self.arches['sum_int_2d'] -= self.data_2d[int( idx)]['int_2d'] self.arches['sum_map_raw'] -= self.data_2d[int( idx)]['map_raw'] except KeyError: pass # ic(self.data_1d.keys(), self.data_2d.keys(), self.arches.keys()) self.sigUpdate.emit() gc.collect()
def _set_new_mask(self, idx, mask): mask_ids = np.arange(self.mask_widget.data.size)[mask.ravel() == 1] if idx < 0: self.sphere.global_mask = mask_ids with self.sphere.file_lock: self.sphere.save_to_h5() else: if self.arch.idx is not None and self.arch.idx == idx: _arch = self.arch else: try: with self.sphere.file_lock: _arch = self.sphere.arches[idx] except KeyError: traceback.print_exc() return _arch.mask = mask_ids with self.sphere.file_lock: with catch(self.sphere.data_file, 'a') as file: arches = file['arches'] _arch.save_to_h5(arches)
def __init__(self, data_file, file_lock, arches=[], static=False, gi=False): """data_file: Path to hdf5 file for storing data. file_lock: Thread safe lock. arches: List of arches to initialize series with. """ self.data_file = data_file self.file_lock = file_lock self.index = [] self.static = static self.gi = gi if arches: for a in arches: self.__setitem__(a.idx, a) self._i = 0 # invoke the lock to prevent conflicts with self.file_lock: # use catch to avoid oserrors which will resolve with time. with catch(self.data_file, 'a') as f: if 'arches' not in f: f.create_group('arches')
def load_arch(self): with self.file_lock: with catch(self.sphere.data_file, 'r') as file: self.arch.load_from_h5(file['arches']) self.sigUpdate.emit()