예제 #1
0
 def sum_scans(self):
     if not self.scan_files:
         raise NeXusError("No files selected")
     server = NXServer()
     scan_filter = ';;'.join(("NeXus Files (*.nxs)", "Any Files (*.* *)"))
     scan_prefix = self.scan_files[0][:self.scan_files[0].rindex('_')]
     preferred_name = os.path.join(self.sample_directory,
                                   scan_prefix + '_sum.nxs')
     scan_file = getSaveFileName(self, 'Choose Summed File Name',
                                 preferred_name, scan_filter)
     if not scan_file:
         return
     prefix = self.sample + '_'
     if not os.path.basename(scan_file).startswith(prefix):
         raise NeXusError(f"Summed file name must start with '{prefix}'")
     self.scan_label = self.get_label(scan_file)
     scan_dir = os.path.join(self.sample_directory, self.scan_label)
     reduce = NXMultiReduce(directory=scan_dir, overwrite=True)
     reduce.nxsum(self.scan_list)
     self.treeview.tree.load(scan_file, 'rw')
     command = f'nxsum -d {scan_dir}'
     if self.update:
         command += ' -u'
     if self.overwrite:
         command += ' -o'
     for entry in reduce.entries:
         server.add_task('{} -e {} -s {}'.format(command, entry,
                                                 ' '.join(self.scan_list)))
예제 #2
0
 def initialize_data(self, data):
     if isinstance(data, NXdata):
         if len(data.nxsignal.shape) > 1:
             raise NeXusError(
                 "Fitting only possible on one-dimensional arrays")
         fit_data = NXdata(data.nxsignal, data.nxaxes, title=data.nxtitle)
         if data.nxerrors:
             fit_data.errors = data.nxerrors
         return fit_data
     else:
         raise NeXusError("Must be an NXdata group")
예제 #3
0
 def __setitem__(self, key, value):
     if isinstance(value, NXroot):
         if key not in self._entries:
             value._group = self
             value._name = key
             self._entries[key] = value
             self._shell[key] = self._entries[key]
             value.set_changed()
         else:
             raise NeXusError("'" + key + "' already in the tree")
     else:
         raise NeXusError("Value must be an NXroot group")
예제 #4
0
 def accept(self):
     if not self._selected:
         raise NeXusError("Need to select output file before saving")
     elif self.root.nxfilemode == 'r':
         raise NeXusError("NeXus file is locked")
     if self.overwrite:
         for entry in [e for e in self.root if e != 'entry']:
             reduce = NXReduce(self.root[entry], copy=True, overwrite=True)
             reduce.nxcopy()
     else:
         raise NeXusError("Much check 'overwrite' to save parameters")
     super(CopyDialog, self).accept()
예제 #5
0
    def get_data(self):
        self.import_file = self.get_filename()
        if not self.import_file:
            raise NeXusError("No file specified")
        try:
            import tifffile as TIFF
        except ImportError:
            raise NeXusError("Please install the 'tifffile' module")
        im = TIFF.imread(self.import_file)
        z = NXfield(im, name='z')
        y = NXfield(np.arange(z.shape[0], dtype=float), name='y')
        x = NXfield(np.arange(z.shape[1], dtype=float), name='x')

        return NXentry(NXdata(z, (y, x)))
예제 #6
0
 def reload(self, name):
     if name in self:
         if isinstance(self[name], NXroot):
             self[name].reload()
         return self[name]
     else:
         raise NeXusError('%s not in the tree')
예제 #7
0
 def initialize(self, directory, server_type, nodes):
     self.server_settings = NXSettings(directory=directory)
     self.directory = self.server_settings.directory
     if server_type:
         self.server_type = server_type
         self.server_settings.set('setup', 'type', server_type)
     elif self.server_settings.has_option('setup', 'type'):
         self.server_type = self.server_settings.get('setup', 'type')
     else:
         raise NeXusError('Server type not specified')
     if self.server_type == 'multinode':
         if 'nodes' not in self.server_settings.sections():
             self.server_settings.add_section('nodes')
         if nodes:
             self.write_nodes(nodes)
         self.cpus = self.read_nodes()
     else:
         if self.server_settings.has_option('setup', 'cores'):
             cpu_count = int(self.server_settings.get('setup', 'cores'))
             if cpu_count > psutil.cpu_count():
                 cpu_count = psutil.cpu_count()
         else:
             cpu_count = psutil.cpu_count()
         self.cpus = ['cpu' + str(cpu) for cpu in range(1, cpu_count + 1)]
     self.server_settings.save()
     self.log_file = os.path.join(self.directory, 'nxserver.log')
     self.pid_file = os.path.join(self.directory, 'nxserver.pid')
     self.queue_directory = os.path.join(self.directory, 'task_list')
     self.task_queue = NXFileQueue(self.queue_directory)
예제 #8
0
 def setup_secondary_grid(self):
     ps_angle = self.refine.angle_peaks(self.primary, self.secondary)
     n_phkl = len(self.ring_list[self.refine.rp[self.primary]])
     self.hkl_parameters = [GridParameters() for i in range(n_phkl)]
     min_diff = self.get_peak_tolerance()
     min_p = None
     min_hkl = None
     for i in range(n_phkl):
         phkl = eval(self.peak_parameters['primary_hkl'].box.items()[i])
         for hkls in self.rings[self.refine.rp[self.secondary]][1]:
             for hkl in hkls:
                 hkl_angle = self.refine.angle_hkls(phkl, hkl)
                 diff = abs(ps_angle - hkl_angle)
                 if diff < self.get_peak_tolerance():
                     self.hkl_parameters[i].add(str(hkl),
                                                hkl_angle,
                                                str(hkl),
                                                vary=False,
                                                readonly=True)
                     if diff < min_diff:
                         min_diff = diff
                         min_p = i
                         min_hkl = str(hkl)
         self.orient_box.insert_layout(
             i + 1, self.hkl_parameters[i].grid(
                 header=['HKL', 'Angle (deg)', 'Select'], spacing=5))
     if min_hkl is None:
         raise NeXusError("No matching peaks found")
     self.peak_parameters['primary_hkl'].box.setCurrentIndex(min_p)
     self.hkl_parameters[min_p][min_hkl].vary = True
     self.choose_secondary_grid()
예제 #9
0
    def __init__(self, parent=None):

        super().__init__(parent=parent)

        try:
            from globusonline.catalog.client.examples.catalog_wrapper import \
                CatalogWrapper
        except ImportError:
            raise NeXusError("Cannot import globusonline package")
        token_file = os.path.join(os.path.expanduser('~'), '.nexpy',
                                  'globusonline', 'gotoken.txt')
        self.wrap = CatalogWrapper(token='file', token_file=token_file)
        _, self.catalogs = self.wrap.catalogClient.get_catalogs()
        catalog_layout = QtWidgets.QHBoxLayout()
        self.catalog_box = QtWidgets.QComboBox()
        for catalog in self.catalogs:
            try:
                self.catalog_box.addItem(catalog['config']['name'])
            except Exception:
                pass
        self.catalog_box.setSizeAdjustPolicy(
            QtWidgets.QComboBox.AdjustToContents)
        catalog_button = QtWidgets.QPushButton("Choose Catalog")
        catalog_button.clicked.connect(self.get_catalog)
        catalog_layout.addWidget(self.catalog_box)
        catalog_layout.addWidget(catalog_button)
        self.layout = QtWidgets.QVBoxLayout()
        self.layout.addLayout(catalog_layout)
        self.layout.addWidget(self.close_buttons())
        self.setLayout(self.layout)

        self.setWindowTitle("Import " + str(filetype))
예제 #10
0
    def get_file(self, filename):
        """Return the File object (and associated tasks) matching filename.

        Parameters
        ----------
        filename : str
            Path of wrapper file.

        Returns
        -------
        File
            File object.
        """
        self.check_tasks()
        f = self.query(filename)

        if f is None:
            filename = os.path.realpath(filename)
            if not os.path.exists(filename):
                raise NeXusError(f"'{filename}' does not exist")
            self.session.add(File(filename=self.get_filename(filename)))
            self.session.commit()
            f = self.sync_file(filename)
        else:
            if (f.entries is None or f.entries == ''
                    or isinstance(f.entries, int)):
                root = nxload(filename)
                f.set_entries([e for e in root.entries if e != 'entry'])
            f = self.sync_data(filename)
        return f
예제 #11
0
 def read_image(self, filename):
     try:
         import fabio
     except ImportError:
         raise NeXusError("Please install the 'fabio' module")
     im = fabio.open(filename)
     return im
예제 #12
0
 def choose_entry(self):
     if 'calibration' not in self.entry['instrument']:
         raise NeXusError('Please load calibration data to this entry')
     self.data = self.entry['instrument/calibration']
     self.counts = self.data.nxsignal.nxvalue
     self.mask = self.entry['instrument/detector/pixel_mask'].nxvalue
     self.plot_data()
     shape = self.data.nxsignal.shape
예제 #13
0
    def get_data(self):
        prefix = self.get_prefix()
        if prefix:
            self.import_file = prefix
        else:
            self.import_file = self.get_directory()
        filenames = self.get_files()
        try:
            import fabio
        except ImportError:
            raise NeXusError("Please install the 'fabio' module")
        im = self.read_image(filenames[0])
        v0 = im.data
        x = NXfield(range(v0.shape[1]), dtype=np.uint16, name='x')
        y = NXfield(range(v0.shape[0]), dtype=np.uint16, name='y')
        z = NXfield(range(1, len(filenames)+1), dtype=np.uint16, name='z')
        v = NXfield(shape=(len(filenames), v0.shape[0], v0.shape[1]),
                    dtype=v0.dtype, name='v')
        v[0] = v0
        if v._memfile:
            chunk_size = v._memfile['data'].chunks[0]
        else:
            chunk_size = v.shape[0]/10
        self.progress_bar.setVisible(True)
        self.progress_bar.setRange(0, len(filenames))
        for i in range(0, len(filenames), chunk_size):
            try:
                files = []
                for j in range(i, i+chunk_size):
                    files.append(filenames[j])
                    self.progress_bar.setValue(j)
                self.update_progress()
                v[i:i+chunk_size, :, :] = self.read_images(files)
            except IndexError as error:
                pass
        global maximum
        v.maximum = maximum

        if im.getclassname() == 'CbfImage':
            note = NXnote(type='text/plain', file_name=self.import_file)
            note.data = im.header.pop('_array_data.header_contents', '')
            note.description = im.header.pop(
                '_array_data.header_convention', '')
        else:
            note = None

        header = NXcollection()
        for key, value in im.header.items():
            header[key] = value

        if note:
            return NXentry(
                NXdata(
                    v, (z, y, x),
                    CBF_header=note, header=header))
        else:
            return NXentry(NXdata(v, (z, y, x), header=header))
예제 #14
0
 def set_cores(self, cpu_count):
     """Select number of cores"""
     try:
         cpu_count = int(cpu_count)
     except ValueError:
         raise NeXusError('Number of cores must be a valid integer')
     self.server_settings.set('setup', 'cores', str(cpu_count))
     self.server_settings.save()
     self.cpus = ['cpu' + str(cpu) for cpu in range(1, cpu_count + 1)]
예제 #15
0
 def accept(self):
     try:
         if self.mask is None:
             raise NeXusError("No mask has been created")
         elif self.entry.nxfilemode == 'r':
             raise NeXusError("NeXus file opened as readonly")
         self.reduce.write_mask(self.mask)
         self.reduce.record('nxprepare',
                            masked_file=self.reduce.mask_file,
                            threshold1=self.threshold1,
                            horizontal1=self.horizontal1,
                            threshold2=self.threshold2,
                            horizontal2=self.horizontal2,
                            process='nxprepare_mask')
         self.reduce.record_end('nxprepare')
         super().accept()
     except Exception as error:
         report_error("Preparing Mask", error)
예제 #16
0
파일: utils.py 프로젝트: nexpy/nexpy
def load_image(filename):
    if os.path.splitext(
            filename.lower())[1] in ['.png', '.jpg', '.jpeg', '.gif']:
        from matplotlib.image import imread
        im = imread(filename)
        z = NXfield(im, name='z')
        y = NXfield(range(z.shape[0]), name='y')
        x = NXfield(range(z.shape[1]), name='x')
        if z.ndim > 2:
            rgba = NXfield(range(z.shape[2]), name='rgba')
            if len(rgba) == 3:
                z.interpretation = 'rgb-image'
            elif len(rgba) == 4:
                z.interpretation = 'rgba-image'
            data = NXdata(z, (y, x, rgba))
        else:
            data = NXdata(z, (y, x))
    else:
        try:
            im = fabio.open(filename)
        except Exception as error:
            if fabio:
                raise NeXusError("Unable to open image")
            else:
                raise NeXusError(
                    "Unable to open image. Please install the 'fabio' module")
        z = NXfield(im.data, name='z')
        y = NXfield(range(z.shape[0]), name='y')
        x = NXfield(range(z.shape[1]), name='x')
        data = NXdata(z, (y, x))
        if im.header:
            header = NXcollection()
            for k, v in im.header.items():
                if v or v == 0:
                    header[k] = v
            data.header = header
        if im.getclassname() == 'CbfImage':
            note = NXnote(type='text/plain', file_name=filename)
            note.data = im.header.pop('_array_data.header_contents', '')
            note.description = im.header.pop('_array_data.header_convention',
                                             '')
            data.CBF_header = note
    data.title = filename
    return data
예제 #17
0
 def get_data(self):
     prefix = self.get_prefix()
     if prefix:
         self.import_file = prefix
     else:
         self.import_file = self.get_directory()
     filenames = self.get_files()
     if self.get_image_type() == 'CBF':
         try:
             import pycbf
         except ImportError:
             raise NeXusError("Please install the 'pycbf' module")
     else:
         try:
             import tifffile
         except ImportError:
             raise NeXusError("Please install the 'tifffile' module")
     v0 = self.read_image(filenames[0])
     x = NXfield(range(v0.shape[1]), dtype=np.uint16, name='x')
     y = NXfield(range(v0.shape[0]), dtype=np.uint16, name='y')
     z = NXfield(range(1, len(filenames) + 1), dtype=np.uint16, name='z')
     v = NXfield(shape=(len(filenames), v0.shape[0], v0.shape[1]),
                 dtype=v0.dtype,
                 name='v')
     v[0] = v0
     if v._memfile:
         chunk_size = v._memfile['data'].chunks[0]
     else:
         chunk_size = v.shape[0] / 10
     self.progress_bar.setVisible(True)
     self.progress_bar.setRange(0, len(filenames))
     for i in range(0, len(filenames), chunk_size):
         try:
             files = []
             for j in range(i, i + chunk_size):
                 files.append(filenames[j])
                 self.progress_bar.setValue(j)
             self.update_progress()
             v[i:i + chunk_size, :, :] = self.read_images(files)
         except IndexError as error:
             pass
     global maximum
     v.maximum = maximum
     return NXentry(NXdata(v, (z, y, x)))
예제 #18
0
 def data(self):
     try:
         xmin, xmax = self.get_limits()
         axis = self._data.nxaxes[0]
         if xmin > axis.max() or xmax < axis.min():
             raise NeXusError('Invalid data range')
         else:
             return self._data[xmin:xmax]
     except NeXusError as error:
         report_error('Fitting data', error)
예제 #19
0
 def add_tasks(self):
     if self.grid is None:
         raise NeXusError('Need to update status')
     for scan in [s for s in self.enabled_scans if self.any_selected(s)]:
         for i, entry in enumerate(self.enabled_scans[scan]['entries']):
             if self.only_combined(scan):
                 if i == 0:
                     reduce = NXMultiReduce(scan)
                     reduce.regular = reduce.mask = False
                 else:
                     break
             else:
                 reduce = NXReduce(entry, scan)
                 reduce.regular = reduce.mask = False
                 if self.selected(scan, 'link'):
                     reduce.link = True
                 if self.selected(scan, 'copy'):
                     reduce.copy = True
                 if self.selected(scan, 'max'):
                     reduce.maxcount = True
                 if self.selected(scan, 'find'):
                     reduce.find = True
                 if self.selected(scan, 'refine'):
                     reduce.refine = True
                 if self.selected(scan, 'prepare'):
                     reduce.prepare = True
                 if self.selected(scan, 'transform'):
                     reduce.transform = True
                     reduce.regular = True
                 if self.selected(scan, 'masked_transform'):
                     reduce.transform = True
                     reduce.mask = True
             if self.selected(scan, 'combine'):
                 reduce.combine = True
                 reduce.regular = True
             if self.selected(scan, 'masked_combine'):
                 reduce.combine = True
                 reduce.mask = True
             if self.selected(scan, 'pdf'):
                 reduce.pdf = True
                 reduce.regular = True
             if self.selected(scan, 'masked_pdf'):
                 reduce.pdf = True
                 reduce.mask = True
             if self.selected(scan, 'overwrite'):
                 reduce.overwrite = True
             reduce.queue('nxreduce')
             time.sleep(0.5)
         for task in self.tasks:
             if self.selected(scan, task):
                 self.queued(scan, task)
     self.deselect_all()
예제 #20
0
 def save_parameters(self):
     if not self.is_calibrated:
         raise NeXusError('No refinement performed')
     elif 'calibration' in self.entry['instrument']:
         if confirm_action(
                 "Do you want to overwrite existing calibration data?"):
             del self.entry['instrument/calibration']
         else:
             return
     self.entry['instrument/calibration'] = self.data
     if 'refinement' in self.entry['instrument/calibration']:
         if confirm_action('Overwrite previous refinement?'):
             del self.entry['instrument/calibration/refinement']
         else:
             return
     self.entry['instrument/calibration/calibrant'] = (
         self.parameters['calibrant'].value)
     process = NXprocess()
     process.program = 'pyFAI'
     process.version = pyFAI.version
     process.parameters = NXcollection()
     process.parameters['Detector'] = (
         self.entry['instrument/detector/description'])
     pyFAI_parameter = self.pattern_geometry.getPyFAI()
     process.parameters['PixelSize1'] = pyFAI_parameter['pixel1']
     process.parameters['PixelSize2'] = pyFAI_parameter['pixel2']
     process.parameters['Distance'] = pyFAI_parameter['dist']
     process.parameters['Poni1'] = pyFAI_parameter['poni1']
     process.parameters['Poni2'] = pyFAI_parameter['poni2']
     process.parameters['Rot1'] = pyFAI_parameter['rot1']
     process.parameters['Rot2'] = pyFAI_parameter['rot2']
     process.parameters['Rot3'] = pyFAI_parameter['rot3']
     process.parameters['Wavelength'] = pyFAI_parameter['wavelength']
     self.entry['instrument/calibration/refinement'] = process
     self.entry['instrument/monochromator/wavelength'] = (
         self.parameters['wavelength'].value)
     self.entry['instrument/monochromator/energy'] = (
         12.398419739640717 / self.parameters['wavelength'].value)
     detector = self.entry['instrument/detector']
     detector['distance'] = self.parameters['distance'].value
     detector['yaw'] = self.parameters['yaw'].value
     detector['pitch'] = self.parameters['pitch'].value
     detector['roll'] = self.parameters['roll'].value
     detector['beam_center_x'] = self.parameters['xc'].value
     detector['beam_center_y'] = self.parameters['yc'].value
     try:
         detector['polarization'] = self.pattern_geometry.polarization(
             factor=0.99, shape=detector['mask'].shape)
     except Exception:
         pass
예제 #21
0
 def choose_entry(self):
     try:
         refine = NXRefine(self.entry)
         if refine.xp is None:
             raise NeXusError("No peaks in entry")
     except NeXusError as error:
         report_error("Refining Lattice", error)
         return
     self.refine = refine
     if self.layout.count() == 2:
         self.insert_layout(1, self.Qgrid)
         self.insert_layout(
             2,
             self.checkboxes(('copy', 'Copy to all entries', True),
                             ('mask', 'Create masked transforms', True),
                             ('overwrite', 'Overwrite transforms', False)))
     self.refine.initialize_grid()
     self.update_grid()
예제 #22
0
 def choose_entry(self):
     try:
         refine = NXRefine(self.entry)
         if refine.xp is None:
             raise NeXusError("No peaks in entry")
     except NeXusError as error:
         report_error("Refining Lattice", error)
         return
     self.refine = refine
     self.set_title(f"Refining {self.refine.name}")
     if self.layout.count() == 2:
         self.insert_layout(1, self.parameters.grid_layout)
         self.insert_layout(2, self.refine_buttons)
         self.insert_layout(3, self.orientation_buttons)
         self.insert_layout(4, self.parameters.report_layout())
         self.insert_layout(5, self.lattice_buttons)
     self.update_parameters()
     self.update_table()
예제 #23
0
def get_file(filename):
    """ Return the File object (and associated tasks) matching filename

        filename: string, path of wrapper file relative to GUP directory
     """
    f = session.query(File) \
            .filter(File.filename == get_filename(filename)) \
            .one_or_none()

    if f is None:
        filename = os.path.realpath(filename)
        if not os.path.exists(filename):
            raise NeXusError("'%s' does not exist" % filename)
        session.add(File(filename = get_filename(filename)))        
        commit(session)
        f = sync_file(filename)
    else:
        f = sync_data(filename)
    return f
예제 #24
0
파일: nxfile.py 프로젝트: umithardal/nicos
 def __init__(self, name, mode='r', **kwds):
     """Create a Nexus File object for reading and writing."""
     self._file = None
     name = os.path.abspath(name)
     self.name = name
     if mode == 'w4' or mode == 'wx':
         raise NeXusError('Only HDF5 files supported')
     elif mode == 'w' or mode == 'w-' or mode == 'w5':
         if mode == 'w5':
             mode = 'w'
         self._mode = 'rw'
         self._file = nxs.open(name, 'w5')
     else:
         if mode == 'rw' or mode == 'r+':
             self._mode = 'rw'
             mode = 'r+'
         else:
             self._mode = 'r'
         self._file = nxs.open(name, 'w5')
     self._filename = self._file.filename
     self._path = '/'
예제 #25
0
 def add(self, node):
     if isinstance(node, NXgroup):
         shell_names = self.get_shell_names(node)
         if shell_names:
             node.nxname = shell_names[0]
         if isinstance(node, NXroot):
             self[node.nxname] = node
             self[node.nxname]._file_modified = False
         elif isinstance(node, NXentry):
             group = NXroot(node)
             name = self.get_new_name()
             self[name] = group
             print("NeXpy: '%s' added to tree in '%s'" %
                   (node.nxname, group.nxname))
         else:
             group = NXroot(NXentry(node))
             name = self.get_new_name()
             self[name] = group
             print("NeXpy: '%s' added to tree in '%s%s'" %
                   (node.nxname, group.nxname, node.nxgroup.nxpath))
     else:
         raise NeXusError("Only an NXgroup can be added to the tree")
예제 #26
0
 def plot_cake(self):
     if 'Cake Plot' in plotviews:
         plotview = plotviews['Cake Plot']
     else:
         plotview = NXPlotView('Cake Plot')
     if not self.is_calibrated:
         raise NeXusError('No refinement performed')
     res = self.cake_geometry.integrate2d(self.counts,
                                          1024, 1024,
                                          method='csr',
                                          unit='2th_deg',
                                          correctSolidAngle=True)
     self.cake_data = NXdata(res[0],
                             (NXfield(res[2], name='azimumthal_angle'),
                              NXfield(res[1], name='polar_angle')))
     self.cake_data['title'] = 'Cake Plot'
     plotview.plot(self.cake_data, log=True)
     wavelength = self.parameters['wavelength'].value
     polar_angles = [2 * np.degrees(np.arcsin(wavelength/(2*d)))
                     for d in self.calibrant.dSpacing]
     plotview.vlines([polar_angle for polar_angle in polar_angles
                      if polar_angle < plotview.xaxis.max],
                     linestyle=':', color='r')
예제 #27
0
 def view_logs(self):
     if self.grid is None:
         raise NeXusError('Need to update status')
     dialog = NXDialog(parent=self)
     dialog.setMinimumWidth(800)
     dialog.setMinimumHeight(600)
     scans = [os.path.basename(scan) for scan in self.scans]
     self.scan_combo = dialog.select_box(scans, slot=self.choose_scan)
     self.entry_combo = dialog.select_box(self.entries,
                                          slot=self.refreshview)
     self.task_combo = dialog.select_box(self.tasks, slot=self.refreshview)
     self.defaultview = None
     self.output_box = NXPlainTextEdit(wrap=False)
     cpu_process_button = NXPushButton('View CPU Processes', self.procview)
     cpu_log_button = NXPushButton('View CPU Log', self.cpuview)
     self.cpu_combo = dialog.select_box(['nxserver'] + self.server.cpus,
                                        slot=self.cpuview)
     close_layout = self.make_layout(cpu_process_button,
                                     cpu_log_button,
                                     self.cpu_combo,
                                     'stretch',
                                     dialog.close_buttons(close=True),
                                     align='justified')
     dialog.set_layout(
         dialog.make_layout(self.scan_combo, self.entry_combo,
                            self.task_combo), self.output_box,
         dialog.action_buttons(('View Data Directory', self.dataview),
                               ('View Server Logs', self.serverview),
                               ('View Workflow Logs', self.logview),
                               ('View Workflow Output', self.outview),
                               ('View Database', self.databaseview)),
         close_layout)
     scans = os.path.join(self.label, self.sample)
     dialog.setWindowTitle(f"'{scans}' Logs")
     self.view_dialog = dialog
     self.view_dialog.show()
예제 #28
0
 def get_directory(self, server_directory=None):
     self.home_settings = ConfigParser()
     home_directory = os.path.join(os.path.abspath(os.path.expanduser('~')),
                                   '.nxserver')
     if not os.path.exists(home_directory):
         os.mkdir(home_directory)
     self.home_file = os.path.join(home_directory, 'settings.ini')
     self.home_settings.read(self.home_file)
     if 'setup' not in self.home_settings.sections():
         self.home_settings.add_section('setup')
     if server_directory:
         self.home_settings.set('setup', 'directory', server_directory)
         with open(self.home_file, 'w') as f:
             self.home_settings.write(f)
     elif self.home_settings.has_option('setup', 'directory'):
         server_directory = self.home_settings.get('setup', 'directory')
     else:
         raise NeXusError(
             "Please define settings directory - type 'nxsettings -h'")
     if os.path.basename(server_directory) != 'nxserver':
         server_directory = os.path.join(server_directory, 'nxserver')
     if not os.path.exists(server_directory):
         os.mkdir(server_directory)
     return server_directory
예제 #29
0
    def update(self):
        if not self.sample_directory:
            raise NeXusError("No sample directory declared")

        if self.grid:
            self.delete_grid(self.grid)
            del self.grid_widget

        if self.scroll_area:
            self.scroll_area.close()
            self.scroll_area.deleteLater()

        # Map from wrapper files to scan directories
        wrapper_files = {
            w: self.get_scan(w)
            for w in sorted([
                os.path.join(self.sample_directory, filename)
                for filename in os.listdir(self.sample_directory)
                if self.is_valid(filename)
            ],
                            key=natural_sort)
        }
        self.grid = QtWidgets.QGridLayout()
        self.grid_widget = NXWidget()
        self.grid_widget.set_layout(self.grid, 'stretch')
        self.scroll_area = NXScrollArea(self.grid_widget)
        self.scroll_area.setMinimumSize(1250, 300)
        self.insert_layout(3, self.scroll_area)
        self.grid.setSpacing(1)

        self.scans = {}
        self.scans_backup = {}

        row = 0
        # Create (unchecked) checkboxes
        for wrapper_file, scan in wrapper_files.items():
            scan_label = os.path.basename(scan)
            status = {}
            status['scan'] = NXLabel(scan_label)
            if self.parent_file == wrapper_file:
                status['scan'].setStyleSheet('font-weight:bold')
            status['entries'] = []
            status['data'] = self.new_checkbox()
            status['link'] = self.new_checkbox()
            status['copy'] = self.new_checkbox()
            status['max'] = self.new_checkbox()
            status['find'] = self.new_checkbox()
            status['refine'] = self.new_checkbox()
            status['prepare'] = self.new_checkbox()
            status['transform'] = self.new_checkbox()
            status['masked_transform'] = self.new_checkbox()
            status['combine'] = self.new_checkbox()
            status['masked_combine'] = self.new_checkbox()
            status['pdf'] = self.new_checkbox()
            status['masked_pdf'] = self.new_checkbox()
            status['overwrite'] = self.new_checkbox(self.select_scans)
            status['sync'] = self.new_checkbox()
            self.grid.addWidget(status['scan'], row, 0, QtCore.Qt.AlignCenter)
            self.grid.addWidget(status['data'], row, 1, QtCore.Qt.AlignCenter)
            self.grid.addWidget(status['link'], row, 2, QtCore.Qt.AlignCenter)
            self.grid.addWidget(status['copy'], row, 3, QtCore.Qt.AlignCenter)
            self.grid.addWidget(status['max'], row, 4, QtCore.Qt.AlignCenter)
            self.grid.addWidget(status['find'], row, 5, QtCore.Qt.AlignCenter)
            self.grid.addWidget(status['refine'], row, 6,
                                QtCore.Qt.AlignCenter)
            self.grid.addWidget(status['prepare'], row, 7,
                                QtCore.Qt.AlignCenter)
            self.grid.addWidget(status['transform'], row, 8,
                                QtCore.Qt.AlignCenter)
            self.grid.addWidget(status['masked_transform'], row, 9,
                                QtCore.Qt.AlignCenter)
            self.grid.addWidget(status['combine'], row, 10,
                                QtCore.Qt.AlignCenter)
            self.grid.addWidget(status['masked_combine'], row, 11,
                                QtCore.Qt.AlignCenter)
            self.grid.addWidget(status['pdf'], row, 12, QtCore.Qt.AlignCenter)
            self.grid.addWidget(status['masked_pdf'], row, 13,
                                QtCore.Qt.AlignCenter)
            self.grid.addWidget(status['overwrite'], row, 14,
                                QtCore.Qt.AlignCenter)
            self.grid.addWidget(status['sync'], row, 15, QtCore.Qt.AlignCenter)
            self.scans[scan] = status
            row += 1
        self.grid.addWidget(NXLabel('All'), row, 0, QtCore.Qt.AlignCenter)
        all_boxes = {}
        all_boxes['link'] = self.new_checkbox(
            lambda: self.select_status('link'))
        all_boxes['copy'] = self.new_checkbox(
            lambda: self.select_status('copy'))
        all_boxes['max'] = self.new_checkbox(lambda: self.select_status('max'))
        all_boxes['find'] = self.new_checkbox(
            lambda: self.select_status('find'))
        all_boxes['refine'] = self.new_checkbox(
            lambda: self.select_status('refine'))
        all_boxes['prepare'] = self.new_checkbox(
            lambda: self.select_status('prepare'))
        all_boxes['transform'] = self.new_checkbox(
            lambda: self.select_status('transform'))
        all_boxes['masked_transform'] = self.new_checkbox(
            lambda: self.select_status('masked_transform'))
        all_boxes['combine'] = self.new_checkbox(
            lambda: self.select_status('combine'))
        all_boxes['masked_combine'] = self.new_checkbox(
            lambda: self.select_status('masked_combine'))
        all_boxes['pdf'] = self.new_checkbox(lambda: self.select_status('pdf'))
        all_boxes['masked_pdf'] = self.new_checkbox(
            lambda: self.select_status('masked_pdf'))
        all_boxes['overwrite'] = self.new_checkbox(self.select_all)
        all_boxes['sync'] = self.new_checkbox(self.select_all)
        self.grid.addWidget(all_boxes['link'], row, 2, QtCore.Qt.AlignCenter)
        self.grid.addWidget(all_boxes['copy'], row, 3, QtCore.Qt.AlignCenter)
        self.grid.addWidget(all_boxes['max'], row, 4, QtCore.Qt.AlignCenter)
        self.grid.addWidget(all_boxes['find'], row, 5, QtCore.Qt.AlignCenter)
        self.grid.addWidget(all_boxes['refine'], row, 6, QtCore.Qt.AlignCenter)
        self.grid.addWidget(all_boxes['prepare'], row, 7,
                            QtCore.Qt.AlignCenter)
        self.grid.addWidget(all_boxes['transform'], row, 8,
                            QtCore.Qt.AlignCenter)
        self.grid.addWidget(all_boxes['masked_transform'], row, 9,
                            QtCore.Qt.AlignCenter)
        self.grid.addWidget(all_boxes['combine'], row, 10,
                            QtCore.Qt.AlignCenter)
        self.grid.addWidget(all_boxes['masked_combine'], row, 11,
                            QtCore.Qt.AlignCenter)
        self.grid.addWidget(all_boxes['pdf'], row, 12, QtCore.Qt.AlignCenter)
        self.grid.addWidget(all_boxes['masked_pdf'], row, 13,
                            QtCore.Qt.AlignCenter)
        self.grid.addWidget(all_boxes['overwrite'], row, 14,
                            QtCore.Qt.AlignCenter)
        self.grid.addWidget(all_boxes['sync'], row, 15, QtCore.Qt.AlignCenter)
        self.all_scans = all_boxes
        self.start_progress((0, len(wrapper_files)))

        # Populate the checkboxes based on the entries in self.db.File
        for i, (wrapper, scan) in enumerate(wrapper_files.items()):
            status = self.scans[scan]
            status['data'].setEnabled(False)
            f = self.db.get_file(wrapper)
            status['entries'] = f.get_entries()
            for task_name in self.db.task_names:
                # Database columns use nx* names while columns don't
                if task_name.startswith('nx'):
                    col_name = task_name[2:]
                else:
                    col_name = task_name
                checkbox = status[col_name]
                file_status = getattr(f, task_name)
                if file_status == self.db.DONE:
                    checkbox.setCheckState(QtCore.Qt.Checked)
                    checkbox.setEnabled(False)
                elif file_status == self.db.IN_PROGRESS:
                    checkbox.setCheckState(QtCore.Qt.PartiallyChecked)
                    checkbox.setEnabled(True)
                    checkbox.setStyleSheet("color: green")
                elif file_status == self.db.QUEUED:
                    checkbox.setCheckState(QtCore.Qt.PartiallyChecked)
                    checkbox.setEnabled(True)
                    checkbox.setStyleSheet("color: blue")
                elif file_status == self.db.FAILED:
                    checkbox.setCheckState(QtCore.Qt.PartiallyChecked)
                    checkbox.setEnabled(True)
                    checkbox.setStyleSheet("color: red")
            if status['data'].checkState() == QtCore.Qt.Unchecked:
                for task in [
                        'link', 'max', 'find', 'prepare', 'transform',
                        'masked_transform'
                ]:
                    status[task].setEnabled(False)
            self.update_progress(i)

        self.stop_progress()
        self.backup_scans()
        return self.grid
예제 #30
0
    def plot(self, data_group, fmt, xmin, xmax, ymin, ymax, zmin, zmax,
             **opts):
        """
        Plot the data entry.

        Raises NeXusError if the data cannot be plotted.
        """
        try:
            import matplotlib.pyplot as plt
        except ImportError:
            raise NeXusError(
                "Default plotting package (matplotlib) not available.")

        over = opts.pop("over", False)
        image = opts.pop("image", False)
        log = opts.pop("log", False)
        logx = opts.pop("logx", False)
        logy = opts.pop("logy", False)

        if fmt == '':
            fmt = 'o'

        if over:
            plt.autoscale(enable=False)
        else:
            plt.autoscale(enable=True)
            plt.clf()

        signal = data_group.nxsignal
        errors = data_group.nxerrors
        title = data_group.nxtitle

        # Provide a new view of the data if there is a dimension of length 1
        data, axes = (signal.nxdata.reshape(data_group.plot_shape),
                      data_group.plot_axes)

        #One-dimensional Plot
        if len(data.shape) == 1:
            if hasattr(signal, 'units'):
                if not errors and signal.units == 'counts':
                    errors = NXfield(np.sqrt(data))
            if errors:
                ebars = errors.nxdata
                plt.errorbar(centers(axes[0], data.shape[0]),
                             data,
                             ebars,
                             fmt=fmt,
                             **opts)
            else:
                plt.plot(centers(axes[0], data.shape[0]), data, fmt, **opts)
            if not over:
                ax = plt.gca()
                xlo, xhi = ax.set_xlim(auto=True)
                ylo, yhi = ax.set_ylim(auto=True)
                if xmin: xlo = xmin
                if xmax: xhi = xmax
                ax.set_xlim(xlo, xhi)
                if ymin: ylo = ymin
                if ymax: yhi = ymax
                ax.set_ylim(ylo, yhi)
                if logx: ax.set_xscale('symlog')
                if log or logy: ax.set_yscale('symlog')
                plt.xlabel(label(axes[0]))
                plt.ylabel(label(signal))
                plt.title(title)

        #Two dimensional plot
        else:
            from matplotlib.image import NonUniformImage
            from matplotlib.colors import LogNorm, Normalize

            if len(data.shape) > 2:
                slab = []
                if image:
                    for _dim in data.shape[:-3]:
                        slab.append(0)
                    slab.extend([slice(None), slice(None), slice(None)])
                else:
                    for _dim in data.shape[:-2]:
                        slab.append(0)
                    slab.extend([slice(None), slice(None)])
                data = data[slab]
                if 0 in slab:
                    print "Warning: Only the top 2D slice of the data is plotted"

            if image:
                x = boundaries(axes[-2], data.shape[-2])
                y = boundaries(axes[-3], data.shape[-3])
                xlabel, ylabel = label(axes[-2]), label(axes[-3])
            else:
                x = boundaries(axes[-1], data.shape[-1])
                y = boundaries(axes[-2], data.shape[-2])
                xlabel, ylabel = label(axes[-1]), label(axes[-2])

            if not zmin:
                zmin = np.nanmin(data[data > -np.inf])
            if not zmax:
                zmax = np.nanmax(data[data < np.inf])

            if not image:
                if log:
                    zmin = max(zmin, 0.01)
                    zmax = max(zmax, 0.01)
                    opts["norm"] = LogNorm(zmin, zmax)
                else:
                    opts["norm"] = Normalize(zmin, zmax)

            ax = plt.gca()
            if image:
                im = ax.imshow(data, **opts)
                ax.set_aspect('equal')
            else:
                im = ax.pcolormesh(x, y, data, **opts)
                im.get_cmap().set_bad('k', 1.0)
                ax.set_xlim(x[0], x[-1])
                ax.set_ylim(y[0], y[-1])
                ax.set_aspect('auto')
            if not image:
                plt.colorbar(im)

            if 'origin' in opts and opts['origin'] == 'lower':
                image = False
            if xmin:
                ax.set_xlim(left=xmin)
            if xmax:
                ax.set_xlim(right=xmax)
            if ymin:
                if image:
                    ax.set_ylim(top=ymin)
                else:
                    ax.set_ylim(bottom=ymin)
            if ymax:
                if image:
                    ax.set_ylim(bottom=ymax)
                else:
                    ax.set_ylim(top=ymax)

            plt.xlabel(xlabel)
            plt.ylabel(ylabel)
            plt.title(title)

        plt.gcf().canvas.draw_idle()
        plt.ion()
        plt.show()