def export_data(self): try: item = self.ui.h5file_tree.ui.Tree.currentItem() self.current_node_path = item.text(2) node = self.h5file.get_node(item.text(2)) if 'ARRAY' in node._v_attrs['CLASS']: data = node.read() if isinstance(data, np.ndarray): file = select_file(save=True, ext='txt') if file != '': np.savetxt(file, data, '%.6e', '\t') elif 'GROUP' in node._v_attrs['CLASS']: children_names = list(node._v_children) data = [] header = [] for subnode_name in node._v_children: subnode = node._f_get_child(subnode_name) if 'ARRAY' in subnode._v_attrs['CLASS']: if len(subnode.shape) == 1: data.append(subnode.read()) header.append(subnode_name) file = select_file(save=True, ext='txt') if file != '': np.savetxt(file, np.array(data).T, '%.6e', '\t', header='\t'.join(header)) except Exception as e: self.status_signal.emit(getLineInfo() + str(e))
def load_file(self, base_path=None, file_path=None): """Opens a file dialog to select a h5file saved on disk to be used Parameters ---------- base_path file_path See Also -------- :py:meth:`init_file` :py:meth:`pymodaq.daq_utils.daq_utils.select_file` """ if base_path is None: base_path = self.settings.child('base_path').value() if not os.path.isdir(base_path): base_path = None if file_path is None: file_path = utils.select_file(base_path, save=False, ext='h5') if not isinstance(file_path, Path): file_path = Path(file_path) if 'h5' not in file_path.suffix: raise IOError('Invalid file type, should be a h5 file') self.init_file(addhoc_file_path=file_path)
def __init__(self, msgbox=False): self.preset_params = None self.pid_type = False if msgbox: msgBox = QtWidgets.QMessageBox() msgBox.setText("Preset Manager?") msgBox.setInformativeText("What do you want to do?") cancel_button = msgBox.addButton(QtWidgets.QMessageBox.Cancel) new_button = msgBox.addButton("New", QtWidgets.QMessageBox.ActionRole) modify_button = msgBox.addButton('Modify', QtWidgets.QMessageBox.AcceptRole) msgBox.setDefaultButton(QtWidgets.QMessageBox.Cancel) ret = msgBox.exec() if msgBox.clickedButton() == new_button: self.set_new_preset() elif msgBox.clickedButton() == modify_button: path = select_file(start_path=preset_path, save=False, ext='xml') if path != '': self.set_file_preset(str(path)) else: #cancel pass
def load_data(self): self.h5file_path = str( utils.select_file(start_path=None, save=False, ext='h5')) if self.h5file_path != '': self.settings.child('settings', 'filepath').setValue(self.h5file_path) if self.h5file is not None: self.h5file.close() self.h5file = tables.open_file(self.h5file_path, 'a') self.list_2Dscans()
def load_file(self, h5file=None): if h5file is None: h5file = str(select_file(start_path=None, save=False, ext='h5')) if isinstance(h5file, str) or isinstance(h5file, Path): h5file = tables.open_file(str(h5file), 'a') elif isinstance(h5file, tables.File): pass else: raise Exception('not a valid h5 file or path to a h5 file') self.h5file = h5file self.populate_tree()
def save_ROI(self): try: data = custom_tree.parameter_to_xml_string( self.settings.child(('ROIs'))) path = select_file(ext='roi') if path != '': with open(path, 'wb') as f: f.write(data) except Exception as e: print(e)
def export_data(self): try: item = self.ui.h5file_tree.ui.Tree.currentItem() self.current_node_path = item.text(2) node = self.h5file.get_node(item.text(2)) if 'ARRAY' in node._v_attrs['CLASS']: data = node.read() if isinstance(data, np.ndarray): file = select_file(save=True, ext='txt') if file != '': np.savetxt(file, data, '%.6e', '\t') except Exception as e: self.status_signal.emit(getLineInfo() + str(e))
def modify_shortcuts(self): try: path = select_file(start_path=shortcut_path, save=False, ext='xml') if path != '': self.shortcut_manager.set_file_preset(str(path)) mssg = QtWidgets.QMessageBox() mssg.setText( f'You have to restart the application to take the modifications into account! ' f'Quitting the application...') mssg.exec() self.quit_fun() else: # cancel pass except Exception as e: self.update_status(getLineInfo() + str(e))
def load_file(self, base_path=None, file_path=None): if base_path is None: base_path = self.settings.child('base_path').value() if not os.path.isdir(base_path): base_path = None if file_path is None: file_path = utils.select_file(base_path, save=False, ext='h5') if not isinstance(file_path, Path): file_path = Path(file_path) if not file_path.suffix == 'h5': raise IOError('Invalid file type, should be a h5 file') self.init_file(addhoc_file_path=file_path)
def save_layout_state(self, file=None): """ Save the current layout state in the select_file obtained pathname file. Once done dump the pickle. See Also -------- utils.select_file """ try: dockstate = self.dockarea.saveState() if file is None: file = select_file(start_path=None, save=True, ext='dock') if file is not None: with open(str(file), 'wb') as f: pickle.dump(dockstate, f, pickle.HIGHEST_PROTOCOL) except: pass
def save_data(self): try: path = utils.select_file(start_path=self.settings.child( 'main_settings', 'base_path').value(), save=True, ext='h5') if path is not None: #init the file object with an addhoc name given by the user h5saver = H5Saver(save_type='custom') h5saver.init_file(update_h5=True, addhoc_file_path=path) #save all metadata settings_str = custom_tree.parameter_to_xml_string( self.settings) settings_str = b'<All_settings>' + settings_str settings_str += custom_tree.parameter_to_xml_string(self.detector.settings) + \ custom_tree.parameter_to_xml_string(h5saver.settings) + \ b'</All_settings>' data_group = h5saver.add_data_group( h5saver.raw_group, group_data_type='data0D', title='data from custom app', settings_as_xml=settings_str) for dat in self.raw_data: channel = h5saver.add_CH_group(data_group) data_dict = dict(data=np.array(dat), x_axis=dict(data=np.linspace( 0, len(dat) - 1, len(dat)), units='pxl')) h5saver.add_data(channel, data_dict=data_dict, scan_type='') st = 'file {:s} has been saved'.format(str(path)) self.add_log(st) self.settings.child('main_settings', 'info').setValue(st) h5saver.close_file() except Exception as e: self.add_log(getLineInfo() + str(e))
def load_ROI(self, path=None): try: if path is None: path = select_file(save=False, ext='roi') if path != '': for roi in self.ROIs.values(): index = roi.index self.viewer_widget.plotitem.removeItem(roi) #self.settings.sigTreeStateChanged.disconnect() self.settings.child(*('ROIs', 'ROI_%02.0d' % index)).remove() #self.settings.sigTreeStateChanged.connect(self.roi_tree_changed) self.ROIs = OrderedDict([]) params = custom_tree.XML_file_to_parameter(path) self.settings.child(('ROIs')).addChildren(params) except Exception as e: pass
def load_layout_state(self, file=None): """ Load and restore a layout state from the select_file obtained pathname file. See Also -------- utils.select_file """ try: if file is None: file = select_file(save=False, ext='dock') if file is not None: with open(str(file), 'rb') as f: dockstate = pickle.load(f) self.dockarea.restoreState(dockstate) file = os.path.split(file)[1] self.settings.child('loaded_files', 'layout_file').setValue(file) except: pass
def load_h5_file(self, path=None): """ | Load the specific h5 file calling the open_h5_file procedure ================ ============ ======================================= **Parameters** **Type** **Description** *path* string the current path to the file to load ================ ============ ======================================= See Also -------- open_h5_file, update_status, daq_utils.select_file """ try: filename = select_file(start_path=path, save=False, ext='h5') if filename is not "": self.open_h5_file(filename) except Exception as e: self.update_status(str(e), wait_time=self.wait_time)
def save_data(self): try: fname = utils.select_file(start_path=self.save_file_pathname, save=True, ext='h5') if not (not (fname)): with tables.open_file( str(fname), mode='w', title='an example h5 file name') as h5file: data_to_save = np.squeeze(np.array(self.raw_data)) #save metadata h5file.root._v_attrs[ 'settings'] = custom_tree.parameter_to_xml_string( self.settings) for ind in range(data_to_save.shape[1]): arr = h5file.create_array('/', 'data_{:d}'.format(ind), data_to_save[:, ind]) arr._v_attrs['shape'] = data_to_save.shape[0] arr._v_attrs['type'] = 'data1D' arr = h5file.create_array('/', 'data_2D', data_to_save) arr._v_attrs['shape'] = data_to_save.shape arr._v_attrs['type'] = 'data2D' logger = "logging" text_atom = tables.atom.ObjectAtom() logger_array = h5file.create_vlarray('/', logger, atom=text_atom) logger_array._v_attrs['type'] = 'list' for ind_log in range(self.logger_list.count()): txt = self.logger_list.item(ind_log).text() logger_array.append(txt) st = 'file {:s} has been saved'.format(fname) self.add_log(st) self.settings.child('min_settings', 'info').setValue(st) except Exception as e: self.add_log(str(e))
def __init__(self, parent, h5file=None): QLocale.setDefault(QLocale(QLocale.English, QLocale.UnitedStates)) super(H5Browser, self).__init__() if type(parent) is not QtWidgets.QWidget: raise Exception('no valid parent container, expected a QWidget') self.parent = parent if h5file is None: h5file = str(select_file(start_path=None, save=False, ext='h5')) if type(h5file) == str: h5file = tables.open_file(h5file, 'a') elif type(h5file) == tables.File: pass else: raise Exception('not a valid h5 file or path to a h5 file') self.h5file = h5file self.ui = QObject() #the user interface self.set_GUI() self.populate_tree() self.ui.h5file_tree.ui.Open_Tree.click()
def init_file(self, update_h5=False, custom_naming=False, addhoc_file_path=None): """ init a new h5 file. Could be a file with a given name (addhoc_file_path) or following the template for scans (datasets) or for detectors or a box to set a custom name (custom naming) Parameters ---------- update_h5: (bool) create a new h5 file with name specified by other parameters if false try to open an existing file or create it if it doesn't exists custom_naming: (bool) if True, a selection file dialog opens to set a new file name addhoc_file_path: (Path) supplied name for the file Returns ------- bool: True if new file has been created, False otherwise """ date = datetime.datetime.now() if addhoc_file_path is None: if not os.path.isdir(self.settings.child(('base_path')).value()): os.mkdir(self.settings.child(('base_path')).value()) # set the filename and path base_name = self.settings.child(('base_name')).value() if not custom_naming: custom_naming = self.settings.child(('custom_name')).value() if not custom_naming: scan_type = self.settings.child( ('save_type')).value() == 'scan' scan_path, current_scan_name, save_path = self.update_file_paths( update_h5) self.current_scan_name = current_scan_name self.settings.child( ('current_scan_name')).setValue(current_scan_name) self.settings.child( ('current_scan_path')).setValue(str(scan_path)) if not scan_type: self.h5_file_path = save_path #will remove the dataset part used for DAQ_scan datas self.h5_file_name = base_name + date.strftime( '_%Y%m%d_%H_%M_%S.h5') else: self.h5_file_path = save_path self.h5_file_name = save_path.name + ".h5" else: self.h5_file_name = utils.select_file(start_path=base_name, save=True, ext='h5') self.h5_file_path = self.h5_file_name.parent else: if isinstance(addhoc_file_path, str): addhoc_file_path = Path(addhoc_file_path) self.h5_file_path = addhoc_file_path.parent self.h5_file_name = addhoc_file_path.name fullpathname = str(self.h5_file_path.joinpath(self.h5_file_name)) self.settings.child(('current_h5_file')).setValue(fullpathname) if update_h5: self.current_scan_group = None scan_group = None if self.current_scan_group is not None: scan_group = self.current_scan_group._v_name if update_h5: self.close_file() self.h5_file = tables.open_file(fullpathname, 'w', title='PyMoDAQ file') self.h5_file.root._v_attrs['pymodaq_version'] = get_version() else: self.close_file() self.h5_file = tables.open_file(fullpathname, 'a', title='PyMoDAQ file') self.raw_group = self.get_set_group(self.h5_file.root, 'Raw_datas', title='Data from PyMoDAQ modules') self.get_set_logger(self.raw_group) if scan_group is not None: self.current_scan_group = self.get_set_group( self.raw_group, scan_group) else: self.current_scan_group = self.get_last_scan() self.raw_group._v_attrs['type'] = self.settings.child( ('save_type')).value() self.h5_file.root._v_attrs['file'] = date.strftime(self.h5_file_name) if update_h5: self.h5_file.root._v_attrs['date'] = date.strftime('%Y%m%d') self.h5_file.root._v_attrs['time'] = date.strftime('%H:%M:%S') return update_h5
def save_file(self): filename = select_file(None, save=True, ext='h5') self.h5file.copy_file(str(filename))
def init_file(self, update_h5=False, custom_naming=False, addhoc_file_path=None): """Initializes a new h5 file. Could set the h5_file attributes as: * a file with a name following a template if ``custom_naming`` is ``False`` and ``addhoc_file_path`` is ``None`` * a file within a name set using a file dialog popup if ``custom_naming`` is ``True`` * a file with a custom name if ``addhoc_file_path`` is a ``Path`` object or a path string Parameters ---------- update_h5: bool create a new h5 file with name specified by other parameters if false try to open an existing file and will append new data to it custom_naming: bool if True, a selection file dialog opens to set a new file name addhoc_file_path: Path or str supplied name by the user for the new file Returns ------- update_h5: bool True if new file has been created, False otherwise """ date = datetime.datetime.now() if addhoc_file_path is None: if not os.path.isdir(self.settings.child(('base_path')).value()): os.mkdir(self.settings.child(('base_path')).value()) # set the filename and path base_name = self.settings.child(('base_name')).value() if not custom_naming: custom_naming = self.settings.child(('custom_name')).value() if not custom_naming: scan_type = self.settings.child( ('save_type')).value() == 'scan' scan_path, current_scan_name, save_path = self.update_file_paths( update_h5) self.current_scan_name = current_scan_name self.settings.child( ('current_scan_name')).setValue(current_scan_name) self.settings.child( ('current_scan_path')).setValue(str(scan_path)) if not scan_type: self.h5_file_path = save_path #will remove the dataset part used for DAQ_scan datas self.h5_file_name = base_name + date.strftime( '_%Y%m%d_%H_%M_%S.h5') else: self.h5_file_path = save_path self.h5_file_name = save_path.name + ".h5" else: self.h5_file_name = utils.select_file(start_path=base_name, save=True, ext='h5') self.h5_file_path = self.h5_file_name.parent else: if isinstance(addhoc_file_path, str): addhoc_file_path = Path(addhoc_file_path) self.h5_file_path = addhoc_file_path.parent self.h5_file_name = addhoc_file_path.name fullpathname = str(self.h5_file_path.joinpath(self.h5_file_name)) self.settings.child(('current_h5_file')).setValue(fullpathname) if update_h5: self.current_scan_group = None scan_group = None if self.current_scan_group is not None: scan_group = self.current_scan_group._v_name if update_h5: self.close_file() self.h5_file = tables.open_file(fullpathname, 'w', title='PyMoDAQ file') self.h5_file.root._v_attrs['pymodaq_version'] = get_version() else: self.close_file() self.h5_file = tables.open_file(fullpathname, 'a', title='PyMoDAQ file') self.raw_group = self.get_set_group(self.h5_file.root, 'Raw_datas', title='Data from PyMoDAQ modules') self.get_set_logger(self.raw_group) if scan_group is not None: self.current_scan_group = self.get_set_group( self.raw_group, scan_group) else: self.current_scan_group = self.get_last_scan() self.raw_group._v_attrs['type'] = self.settings.child( ('save_type')).value() self.h5_file.root._v_attrs['file'] = date.strftime(self.h5_file_name) if update_h5: self.h5_file.root._v_attrs['date'] = date.strftime('%Y%m%d') self.h5_file.root._v_attrs['time'] = date.strftime('%H:%M:%S') return update_h5
def do(): save_file_pathname = select_file(None, save=True, ext='h5') # see daq_utils print(save_file_pathname)
def browse_data(fname=None, ret_all=False): """ | Browse data present in any h5 file, when user has selected the one, | | press OK and returns the selected data =============== ==================================== ================================================== **Parameters** **Type** **Description** *fname* str, Path object The path as a Path object or string =============== ==================================== ================================================== =============== ==================================== ================================================== **Returns** **Type** **Description** *data* ndarray Data as a numpy array =============== ==================================== ================================================== """ if fname is None: fname = str(select_file(start_path=None, save=False, ext='h5')) if type(fname) != str: try: fname = str(fname) except: raise Exception('filename in browse data is not valid') if fname != '': (root, ext) = os.path.splitext(fname) if not ('h5' in ext or 'hdf5' in ext): warnings.warn( 'This is not a PyMODAQ h5 file, there could be issues', Warning) with tables.open_file(fname) as h5file: dialog = QtWidgets.QDialog() dialog.setWindowTitle('Select a data node in the tree') vlayout = QtWidgets.QVBoxLayout() form = QtWidgets.QWidget() browser = H5Browser(form, h5file) vlayout.addWidget(form) dialog.setLayout(vlayout) buttonBox = QtWidgets.QDialogButtonBox(parent=dialog) dialog.setLayout(vlayout) buttonBox = QtWidgets.QDialogButtonBox(parent=dialog) buttonBox.addButton('OK', buttonBox.AcceptRole) buttonBox.accepted.connect(dialog.accept) buttonBox.addButton('Cancel', buttonBox.RejectRole) buttonBox.rejected.connect(dialog.reject) vlayout.addWidget(buttonBox) dialog.setWindowTitle('Select data to be loaded') res = dialog.exec() if res == dialog.Accepted: node_path = browser.current_node_path data = h5file.get_node( node_path).read() #save preset parameters in a xml file else: data = None node_path = None if ret_all: return data, fname, node_path else: return data return None, '', ''
def move_Abs(self, position): """ Make the absolute move from the given position after thread command signal was received in DAQ_Move_main. =============== ========= ======================= **Parameters** **Type** **Description** *position* float The absolute position =============== ========= ======================= See Also -------- DAQ_Move_base.set_position_with_scaling, DAQ_Move_base.poll_moving """ try: position = self.check_bound(position) #position=self.set_position_with_scaling(position) #print(position) self.target_position = position if self.settings.child(('shaping_type')).value() == 'FullScreen': self.controller.showBlankscreen(grayValue=position) elif self.settings.child( ('shaping_type')).value() == 'SplitScreen': if self.settings.child('splitting', 'split_control').value( ) == 'Screen spliting': #,'GreyA','GreyB'] screenDivider = position else: screenDivider = self.settings.child( 'splitting', 'split_value').value() if self.settings.child('splitting', 'split_control').value() == 'GreyA': a_gray_value = int(position) else: a_gray_value = self.settings.child('splitting', 'greyA_value').value() if self.settings.child('splitting', 'split_control').value() == 'GreyB': b_gray_value = int(position) else: b_gray_value = self.settings.child('splitting', 'greyB_value').value() flipped = self.settings.child('splitting', 'split_flip').value() if self.settings.child('splitting', 'split_dir').value() == 'Vertical': self.controller.showDividedScreenVertical( a_gray_value, b_gray_value, screenDivider, flipped) else: self.controller.showDividedScreenHorizontal( a_gray_value, b_gray_value, screenDivider, flipped) elif self.settings.child(('shaping_type')).value() == 'File': fname = str(select_file(start_path=None, save=False, ext='h5')) data = h5browser.browse_data(fname) if self.settings.child('calibration', 'calib_apply').value( ) and self.calibration is not None: data = np.reshape( np.interp(data.reshape(np.prod(data.shape)), self.calibration, np.linspace(0, 255, 256)).astype('uint8'), data.shape) dataWidth = self.controller.width_px dataHeight = self.controller.height_px # Calculate the data: for indy in range(dataHeight): for indx in range(dataWidth): self.data_uchar[indy, indx] = data[indy, indx] if data is None: raise Exception('No data has been selected') else: self.controller.showData(self.data_uchar) self.current_position = position #+np.random.rand()-0.5 self.poll_moving() except Exception as e: self.emit_status( ThreadCommand('Update_Status', [getLineInfo() + str(e), 'log']))