Exemple #1
0
def symmetrize_data(symm_function, data_type, data_file, data_path):
    nxsetlock(60)
    data_root = nxload(data_file, 'r')
    if data_type == 'signal':
        data = data_root[data_path].nxvalue
    else:
        signal = data_root[data_path].nxvalue
        data = np.zeros(signal.shape, signal.dtype)
        data[np.where(signal > 0)] = 1
    result = symm_function(data)
    root = nxload(tempfile.mkstemp(suffix='.nxs')[1], mode='w')
    root['data'] = result
    return data_type, root.nxfilename
Exemple #2
0
def load_nxfile(filename):
    """Load nxs data from Soleil. Each file may
    contain more than 1 scan.
    
    Parameters
    -----------
    nxs file : string
        Contains multiple 2D images

    Returns
    -----------
    image : np.array
        Sum 2D images from nxs file
    """
    fload = nxload(filename)

    for key in fload:
        if 'scan' in key:
            lb = key

    data = fload[lb].scan_data.data_01.nxdata

    image = 0
    for i in range(len(data)):
        if type(image) is int:
            image = data[i]
        else:
            image += data[i]

    return image
Exemple #3
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
Exemple #4
0
    def read(self, f, frame=None):
        nxroot = nx.nxload(f)
        # print nxroot.tree
        if hasattr(nxroot, 'entry'):
            if hasattr(nxroot.entry, 'data'):
                if hasattr(nxroot.entry.data, 'data'):
                    self.data = nxroot.entry.data.data

        return self
Exemple #5
0
 def save_fit(self):
     """Saves fit results to an NXentry"""
     self.read_parameters()
     group = NXprocess()
     group['data'] = self.data
     for f in self.functions:
         group[f.name] = self.get_model(f)
         parameters = NXparameters()
         for p in f.parameters:
             parameters[p.name] = NXfield(p.value,
                                          error=p.stderr,
                                          initial_value=p.init_value,
                                          min=str(p.min),
                                          max=str(p.max))
         group[f.name].insert(parameters)
     if self.fit is not None:
         group['program'] = 'lmfit'
         group['version'] = lmfit.__version__
         group['title'] = 'Fit Results'
         group['fit'] = self.get_model()
         fit = NXparameters()
         fit.nfev = self.fit.result.nfev
         fit.ier = self.fit.result.ier
         fit.chisq = self.fit.result.chisqr
         fit.redchi = self.fit.result.redchi
         fit.message = self.fit.result.message
         fit.lmdif_message = self.fit.result.lmdif_message
         group['statistics'] = fit
         group.note = NXnote(
             self.fit.result.message,
             ('%s\n' % self.fit.result.lmdif_message +
              'scipy.optimize.leastsq error value = %s\n' %
              self.fit.result.ier +
              'Chi^2 = %s\n' % self.fit.result.chisqr +
              'Reduced Chi^2 = %s\n' % self.fit.result.redchi +
              'No. of Function Evaluations = %s\n' % self.fit.result.nfev +
              'No. of Variables = %s\n' % self.fit.result.nvarys +
              'No. of Data Points = %s\n' % self.fit.result.ndata +
              'No. of Degrees of Freedom = %s\n' % self.fit.result.nfree +
              '%s' % self.fit.fit_report()))
     else:
         group['title'] = 'Fit Model'
         group['model'] = self.get_model()
     if 'w0' not in self.tree:
         self.tree['w0'] = nxload(self.mainwindow.scratch_file, 'rw')
     ind = []
     for key in self.tree['w0']:
         try:
             if key.startswith('f'):
                 ind.append(int(key[1:]))
         except ValueError:
             pass
     if not ind:
         ind = [0]
     name = 'f' + str(sorted(ind)[-1] + 1)
     self.tree['w0'][name] = group
 def initfile(self, name):
     msg("Initializing NXFileService: " + name)
     try:
         msgv("opening", name)
         self.root[name] = nxload(name)
     except Exception as e:
         m = "Caught exception while opening: " + name + "\n" + \
             "Exception msg: " + str(e)
         msg(m)
         return m
     return True
 def initfile(self, name):
     msg("Initializing NXFileService: " + name)
     try:
         msgv("opening", name)
         self.root[name] = nxload(name)
     except Exception as e:
         m = "Caught exception while opening: " + name + "\n" + \
             "Exception msg: " + str(e)
         msg(m)
         return m
     return True
Exemple #8
0
    def save_fit(self):
        """Saves fit results to an NXentry"""
        self.read_parameters()
        group = NXprocess()
        group['data'] = self.data
        for f in self.functions:
            group[f.name] = self.get_model(f)
            parameters = NXparameters()
            for p in f.parameters:
                parameters[p.name] = NXfield(p.value, error=p.stderr, 
                                             initial_value=p.init_value,
                                             min=str(p.min), max=str(p.max))
            group[f.name].insert(parameters)
        if self.fit is not None:
            group['program'] = 'lmfit'
            group['version'] = lmfit.__version__
            group['title'] = 'Fit Results'
            group['fit'] = self.get_model()
            fit = NXparameters()
            fit.nfev = self.fit.result.nfev
            fit.ier = self.fit.result.ier 
            fit.chisq = self.fit.result.chisqr
            fit.redchi = self.fit.result.redchi
            fit.message = self.fit.result.message
            fit.lmdif_message = self.fit.result.lmdif_message
            group['statistics'] = fit
            group.note = NXnote(self.fit.result.message,
                ('%s\n' % self.fit.result.lmdif_message +
                 'scipy.optimize.leastsq error value = %s\n' 
                 % self.fit.result.ier +
                 'Chi^2 = %s\n' % self.fit.result.chisqr +
                 'Reduced Chi^2 = %s\n' % self.fit.result.redchi +

                 'No. of Function Evaluations = %s\n' % self.fit.result.nfev +
                 'No. of Variables = %s\n' % self.fit.result.nvarys +
                 'No. of Data Points = %s\n' % self.fit.result.ndata +
                 'No. of Degrees of Freedom = %s\n' % self.fit.result.nfree +
                 '%s' % self.fit.fit_report()))
        else:
            group['title'] = 'Fit Model'
            group['model'] = self.get_model()
        if 'w0' not in self.tree:
            self.tree['w0'] = nxload(self.mainwindow.scratch_file, 'rw')
        ind = []
        for key in self.tree['w0']:
            try:
                if key.startswith('f'): 
                    ind.append(int(key[1:]))
            except ValueError:
                pass
        if not ind:
            ind = [0]
        name = 'f'+str(sorted(ind)[-1]+1)
        self.tree['w0'][name] = group
Exemple #9
0
def get_hdf5_tree(hdf5_file_path, text_file_output_name=None):
    """ Returns entire hdf5 file tree to the screen """
    import nexusformat.nexus as nx
    f = nx.nxload(hdf5_file_path)
    out = f.tree
    f.close()

    if type(text_file_output_name) == str:
        str_to_text_file(out, text_file_output_name)
        print('wrote data to : %s' % text_file_output_name)

    return out
Exemple #10
0
 def make_scans(self):
     scans = [scan.label.text() for scan in self.scan_list]
     scan_command = self.textbox['Scan Command'].text()
     scan_parameters = ['#command path filename temperature detx dety ' +
                        'phi_start phi_step phi_end chi omega frame_rate']
     for scan in self.scan_list:
         nexus_file = scan.label.text()
         root = nxload(os.path.join(self.sample_directory, nexus_file))
         temperature = root.entry.sample.temperature
         base_name = os.path.basename(os.path.splitext(nexus_file)[0])
         scan_dir = base_name.replace(self.sample+'_', '')
         for entry in [root[e] for e in root if e != 'entry']:
             if 'phi_set' in entry['instrument/goniometer']:
                 phi_start = entry['instrument/goniometer/phi_set']
             else:
                 phi_start = entry['instrument/goniometer/phi']
             phi_step = entry['instrument/goniometer/phi'].attrs['step']
             phi_end = entry['instrument/goniometer/phi'].attrs['end']
             if 'chi_set' in entry['instrument/goniometer']:
                 chi = entry['instrument/goniometer/chi_set']
             else:
                 chi = entry['instrument/goniometer/chi']
             if 'omega_set' in entry['instrument/goniometer']:
                 omega = entry['instrument/goniometer/omega_set']
             else:
                 omega = entry['instrument/goniometer/omega']
             dx = entry['instrument/detector/translation_x']
             dy = entry['instrument/detector/translation_y']
             if ('frame_time' in entry['instrument/detector'] and
                     entry['instrument/detector/frame_time'] > 0.0):
                 frame_rate = 1.0 / entry['instrument/detector/frame_time']
             else:
                 frame_rate = 10.0
             scan_file = entry.nxname
             if scan_command == 'Pil2Mscan':
                 scan_parameters.append(
                     f'{scan_command} '
                     f'{os.path.join(self.scan_path, scan_dir)} '
                     f'{scan_file} {temperature:.6g} {dx:.6g} {dy:.6g} '
                     f'{phi_start:.6g} {phi_step:.6g} {phi_end:.6g} '
                     f'{chi:.6g} {omega:.6g} {frame_rate:.6g}')
         if scan_command == 'Pil2Mstring':
             scan_parameters.append(f'Pil2Mstring("{scan_dir}")')
         elif scan_command != 'Pil2Mscan':
             scan_parameters.append(f'{scan_command} {temperature}')
     if not os.path.exists(self.macro_directory):
         os.mkdir(os.path.join(self.experiment_directory, 'macros'))
     macro_filter = ';;'.join(("SPEC Macro (*.mac)", "Any Files (*.* *)"))
     macro = getSaveFileName(self, 'Open Macro', self.macro_directory,
                             macro_filter)
     if macro:
         with open(macro, 'w') as f:
             f.write('\n'.join(scan_parameters))
Exemple #11
0
def peak_search(data_file, data_path, i, j, k, threshold):
    """Identify peaks in the slab of raw data

    Parameters
    ----------
    data_file : str
        File path to the raw data file
    data_path : str
        Internal path to the raw data
    i : int
        Index of first z-value of output peaks
    j : int
        Index of first z-value of processed slab
    k : int
        Index of last z-value of processed slab
    threshold : float
        Peak threshold

    Returns
    -------
    list of NXBlobs
        Peak locations and intensities stored in NXBlob instances
    """
    global saved_blobs
    nxsetlock(600)

    def save_blobs(lio, blobs):
        for b in blobs:
            if b[0] < 0.1:
                continue
            lio.spot3d_id += 1
            blob = NXBlob(b)
            if blob.np > 0.0:
                saved_blobs.append(blob)
        if lio.onfirst > 0:
            lio.onfirst = 0

    data_root = nxload(data_file, 'r')
    with data_root.nxfile:
        data = data_root[data_path][j:k].nxvalue

    labelimage.outputpeaks = save_blobs
    lio = labelimage(data.shape[-2:], flipper=flip1, fileout=os.devnull)
    nframes = data.shape[0]
    saved_blobs = []
    for z in range(nframes):
        lio.peaksearch(data[z], threshold, z)
        lio.mergelast()
    lio.finalise()
    for blob in saved_blobs:
        blob.z += j
    return i, saved_blobs
Exemple #12
0
def sync_file(filename):
    """ Return the File object (and associated tasks) matching filename

        filename: string, path of wrapper file relative to GUP directory
     """
    f = get_file(filename)
    sample_dir = os.path.dirname(filename)
    if f:
        try:
            scan_files = os.listdir(get_directory(filename))
        except OSError:
            scan_files = []

        tasks = { t: 0 for t in task_names }
        with Lock(filename):
            root = nxload(filename)
            entries = (e for e in root.entries if e != 'entry')
            for e in entries:
                nxentry = root[e]
                if e in root and 'data' in nxentry and 'instrument' in nxentry:
                    if e+'.h5' in scan_files or e+'.nxs' in scan_files:
                        tasks['data'] += 1
                    if 'nxlink' in nxentry:
                        tasks['nxlink'] += 1
                    if 'nxmax' in nxentry:
                        tasks['nxmax'] += 1
                    if 'nxfind' in nxentry:
                        tasks['nxfind'] += 1
                    if 'nxcopy' in nxentry or is_parent(filename, sample_dir):
                        tasks['nxcopy'] += 1
                    if 'nxrefine' in nxentry:
                        tasks['nxrefine'] += 1
                    if 'nxtransform' in nxentry:
                        tasks['nxtransform'] += 1
                    if 'nxmasked_transform' in nxentry or 'nxmask' in nxentry:
                        tasks['nxmasked_transform'] += 1
                    if 'nxcombine' in root['entry']:
                        tasks['nxcombine'] += 1
                    if 'nxmasked_combine' in root['entry']:
                        tasks['nxmasked_combine'] += 1
                    if 'nxpdf' in root['entry']:
                        tasks['nxpdf'] += 1
        for task, val in tasks.items():
            if val == 0:
                setattr(f, task, NOT_STARTED)
            elif val == NUM_ENTRIES:
                setattr(f, task, DONE)
            else:
                setattr(f, task, IN_PROGRESS)
        commit(session)
    return f
Exemple #13
0
def main():

    parser = argparse.ArgumentParser(description="Perform workflow for scan")
    parser.add_argument('-d', '--directory', default='', help='scan directory')
    parser.add_argument('-e',
                        '--entries',
                        default=['f1', 'f2', 'f3'],
                        nargs='+',
                        help='names of entries to be processed')

    args = parser.parse_args()

    directory = args.directory.rstrip('/')

    print("Processing directory '%s'" % directory)

    wrapper_files = sorted([
        os.path.join(directory, filename)
        for filename in os.listdir(directory) if filename.endswith('.nxs')
    ],
                           key=natural_sort)
    summary = []
    for wrapper_file in wrapper_files:
        print("Processing %s" % wrapper_file)
        root = nxload(wrapper_file)
        for e in args.entries:
            status = '%s[%s]:' % (wrapper_file, e)
            if e in root and 'data' in root[e] and 'instrument' in root[e]:
                if 'nxlink' in root[e] or 'logs' in root[e]['instrument']:
                    status = status + ' nxlink'
                if 'nxmax' in root[e] or 'maximum' in root[e]['data'].attrs:
                    status = status + ' nxmax'
                if 'nxfind' in root[e] or 'peaks' in root[e]:
                    status = status + ' nxfind'
                if 'nxcopy' in root[e]:
                    status = status + ' nxcopy'
                if ('nxrefine' in root[e]
                        or ('detector' in root[e] and 'orientation_matrix'
                            in root[e]['instrument/detector'])):
                    status = status + ' nxrefine'
                if 'nxtransform' in root[e] or 'transform' in root[e]:
                    status = status + ' nxtransform'
            else:
                status = status + ' file incomplete'
            print(status)
            summary.append(status)

    summary_file = os.path.join(directory, 'nxsummary.log')
    with open(summary_file, 'w') as f:
        f.write('\n'.join(summary))
    print("Results summarized in '%s'" % summary_file)
Exemple #14
0
def symmetrize_entries(symm_function, data_type, data_file, data_path):
    nxsetlock(60)
    data_root = nxload(data_file, 'r')
    data_path = os.path.basename(data_path)
    for i, entry in enumerate([e for e in data_root if e != 'entry']):
        if i == 0:
            if data_type == 'signal':
                data = data_root[entry][data_path].nxsignal.nxvalue
            elif data_root[entry][data_path].nxweights:
                data = data_root[entry][data_path].nxweights.nxvalue
            else:
                signal = data_root[entry][data_path].nxsignal.nxvalue
                data = np.zeros(signal.shape, dtype=signal.dtype)
                data[np.where(signal > 0)] = 1
        else:
            if data_type == 'signal':
                data += data_root[entry][data_path].nxsignal.nxvalue
            elif data_root[entry][data_path].nxweights:
                data += data_root[entry][data_path].nxweights.nxvalue
    result = symm_function(data)
    root = nxload(tempfile.mkstemp(suffix='.nxs')[1], mode='w')
    root['data'] = result
    return data_type, root.nxfilename
Exemple #15
0
def main():

    parser = argparse.ArgumentParser(
        description="Perform workflow for scan")
    parser.add_argument('-d', '--directory', default='', help='scan directory')
    parser.add_argument('-e', '--entries', default=['f1', 'f2', 'f3'], 
        nargs='+', help='names of entries to be processed')
                        
    
    args = parser.parse_args()

    directory = args.directory.rstrip('/')

    print("Processing directory '%s'" % directory)
    
    wrapper_files = sorted([os.path.join(directory, filename) 
                            for filename in os.listdir(directory) 
                            if filename.endswith('.nxs')], key=natural_sort)
    summary = []
    for wrapper_file in wrapper_files:
        print("Processing %s" % wrapper_file)
        root = nxload(wrapper_file)
        for e in args.entries:        
            status = '%s[%s]:' % (wrapper_file, e)
            if e in root and 'data' in root[e] and 'instrument' in root[e]:
                if 'nxlink' in root[e] or 'logs' in root[e]['instrument']:
                    status = status + ' nxlink'
                if 'nxmax' in root[e] or 'maximum' in root[e]['data'].attrs:
                    status = status + ' nxmax'
                if 'nxfind' in root[e] or 'peaks' in root[e]:
                    status = status + ' nxfind'
                if 'nxcopy' in root[e]:
                    status = status + ' nxcopy'
                if ('nxrefine' in root[e] or 
                    ('detector' in root[e] and 
                     'orientation_matrix' in root[e]['instrument/detector'])):
                    status = status + ' nxrefine'
                if 'nxtransform' in root[e] or 'transform' in root[e]:
                    status = status + ' nxtransform'
            else:
                status = status + ' file incomplete'
            print(status)
            summary.append(status)            
            
    summary_file = os.path.join(directory, 'nxsummary.log')
    with open(summary_file, 'w') as f:
        f.write('\n'.join(summary))
    print("Results summarized in '%s'" % summary_file)  
 def outview(self):
     self.defaultview = self.outview
     scan = self.sample + '_' + self.scan_combo.currentText()
     entry = self.entry_combo.currentText()
     task = 'nx' + self.task_combo.currentText()
     if (task == 'nxcombine' or task == 'nxmasked_combine'
             or task == 'nxpdf'):
         entry = 'entry'
     wrapper_file = os.path.join(self.sample_directory, scan + '.nxs')
     root = nxload(wrapper_file)
     if task in root[entry]:
         text = 'Date: ' + root[entry][task]['date'].nxvalue + '\n'
         text = text + root[entry][task]['note/data'].nxvalue
         self.output_box.setPlainText(text)
     else:
         self.output_box.setPlainText(f'No output for {task}')
Exemple #17
0
    def init_shell(self, filename):
        """Initialize imports in the shell."""
        global _shell
        _shell = self.window.user_ns
        s = ("import nexusformat.nexus as nx\n"
             "from nexusformat.nexus import NXgroup, NXfield, NXattr, NXlink\n"
             "from nexusformat.nexus import *\n"
             "import nexpy\n"
             "from nexpy.gui.plotview import NXPlotView")
        six.exec_(s, self.window.user_ns)

        s = ""
        for _class in nxclasses:
            s = "%s=nx.%s\n" % (_class,_class) + s
        six.exec_(s, self.window.user_ns)

        config_file = os.path.join(self.nexpy_dir, 'config.py')
        if not os.path.exists(config_file):
            s = ["import sys\n",
                 "import os\n",
                 "import h5py as h5\n",
                 "import numpy as np\n",
                 "import numpy.ma as ma\n",
                 "import scipy as sp\n",
                 "import matplotlib as mpl\n",
                 "from matplotlib import pylab, mlab, pyplot\n",
                 "plt = pyplot\n",
                 "os.chdir(os.path.expanduser('~'))\n"]
            with open(config_file, 'w') as f:
                f.writelines(s)
        else:
            with open(config_file) as f:
                s = f.readlines()
        six.exec_('\n'.join(s), self.window.user_ns)
        if filename is not None:
            try:
                fname = os.path.expanduser(filename)
                name = self.window.treeview.tree.get_name(fname)
                self.window.treeview.tree[name] = self.window.user_ns[name] \
                                                = nxload(fname)
                self.window.treeview.select_node(
                    self.window.treeview.tree[name])
                logging.info("NeXus file '%s' opened as workspace '%s'"
                              % (fname, name))
                self.window.user_ns[name].plot()
            except Exception:
                pass
Exemple #18
0
 def get_nxspe(self):
     spe_file = nxload(self.import_file)
     entry = NXentry()
     entry.title = self.get_title()
     Ei = self.get_energy()
     if Ei and Ei > 0.0:
         entry.instrument = NXinstrument()
         entry.instrument.monochromator = NXmonochromator(
             NXfield(Ei, name="incident_energy", units="meV"))
     entry.data = spe_file.NXentry[0].data
     entry.data.nxsignal = entry.data.data
     if 'energy' in entry.data.entries:
         entry.data.energy.rename('energy_transfer')
     entry.data.nxaxes = [entry.data.polar, entry.data.energy_transfer]
     if 'error' in entry.data.entries:
         entry.data.error.rename('errors')
     return entry
Exemple #19
0
    def init_shell(self, filename):
        """Initialize imports in the shell."""
        global _shell
        _shell = self.window.user_ns
        s = ("import nexusformat.nexus as nx\n"
             "from nexusformat.nexus import NXgroup, NXfield, NXattr, NXlink\n"
             "from nexusformat.nexus import *\n"
             "import nexpy\n"
             "from nexpy.gui.plotview import NXPlotView")
        six.exec_(s, self.window.user_ns)

        s = ""
        for _class in nxclasses:
            s = "%s=nx.%s\n" % (_class,_class) + s
        six.exec_(s, self.window.user_ns)

        try:
            f = open(os.path.join(os.path.expanduser('~'), '.nexpy',
                                  'config.py'))
            s = ''.join(f.readlines())
            six.exec_(s, self.window.user_ns)
        except:
            s = ("import sys\n"
                 "import os\n"
                 "import h5py as h5\n"
                 "import numpy as np\n"
                 "import numpy.ma as ma\n"
                 "import scipy as sp\n"
                 "import matplotlib as mpl\n"
                 "from matplotlib import pylab, mlab, pyplot\n"
                 "plt = pyplot")
            six.exec_(s,  self.window.user_ns)
        if filename is not None:
            try:
                fname = os.path.expanduser(filename)
                name = self.window.treeview.tree.get_name(fname)
                self.window.treeview.tree[name] = self.window.user_ns[name] \
                                                = nxload(fname)
                self.window.treeview.select_node(
                    self.window.treeview.tree[name])
                logging.info("NeXus file '%s' opened as workspace '%s'"
                              % (fname, name))
                self.window.user_ns[name].plot()
            except Exception:
                pass
Exemple #20
0
    def init_shell(self):
        """Initialize imports in the shell."""
        global _shell
        _shell = self.window.user_ns
        s = ("import nexusformat.nexus as nx\n"
             "from nexusformat.nexus import NXgroup, NXfield, NXattr, NXlink\n"
             "from nexusformat.nexus import *\n"
             "import nexpy\n"
             "from nexpy.gui.plotview import NXPlotView")
        exec s in self.window.user_ns

        s = ""
        for _class in nxclasses:
            s = "%s=nx.%s\n" % (_class, _class) + s
        exec s in self.window.user_ns

        try:
            f = open(
                os.path.join(os.path.expanduser('~'), '.nexpy', 'config.py'))
            s = ''.join(f.readlines())
            exec s in self.window.user_ns
        except:
            s = ("import sys\n"
                 "import os\n"
                 "import h5py as h5\n"
                 "import numpy as np\n"
                 "import numpy.ma as ma\n"
                 "import scipy as sp\n"
                 "import matplotlib as mpl\n"
                 "from matplotlib import pylab, mlab, pyplot\n"
                 "plt = pyplot")
            exec s in self.window.user_ns
        try:
            print sys.argv[1]
            fname = os.path.expanduser(sys.argv[1])
            name = _mainwindow.treeview.tree.get_name(fname)
            _mainwindow.treeview.tree[name] = self.window.user_ns[
                name] = nxload(fname)
            _mainwindow.treeview.select_node(_mainwindow.treeview.tree[name])
            logging.info("NeXus file '%s' opened as workspace '%s'" %
                         (fname, name))
            self.window.user_ns[name].plot()
        except:
            pass
Exemple #21
0
 def choose_configuration(self):
     home_directory = self.get_directory()
     config_file = os.path.join(home_directory, 'configurations',
                                self.configuration)
     if os.path.exists(config_file):
         self.config_file = nxload(config_file)
         self.positions = len(self.config_file.entries) - 1
         self.scan_box.clear()
         for position in range(1, self.positions+1):
             self.scan_box.addItem(f'{position}')
         self.scan_box.setCurrentIndex(0)
         self.copy_configuration()
     self.setup_scans()
     self.read_parameters()
     self.insert_layout(3, self.scan.grid(header=False))
     self.insert_layout(4, self.scan_layout)
     for p in range(1, self.positions+1):
         self.insert_layout(p+4, self.entries[p].grid_layout)
     self.insert_layout(self.positions+5,
                        self.action_buttons(('Make Scan File',
                                             self.make_scan)))
Exemple #22
0
 def save_fit(self):
     """Saves fit results to an NXentry"""
     self.read_parameters()
     entry = NXentry()
     entry['data'] = self.data
     for f in self.functions:
         entry[f.name] = self.get_model(f)
         parameters = NXparameters()
         for p in f.parameters:
             parameters[p.name] = NXfield(p.value, error=p.stderr, 
                                          initial_value=p.init_value,
                                          min=str(p.min), max=str(p.max))
         entry[f.name].insert(parameters)
     if self.fit is not None:
         entry['title'] = 'Fit Results'
         entry['fit'] = self.get_model()
         fit = NXparameters()
         fit.nfev = self.fit.result.nfev
         fit.ier = self.fit.result.ier 
         fit.chisq = self.fit.result.chisqr
         fit.redchi = self.fit.result.redchi
         fit.message = self.fit.result.message
         fit.lmdif_message = self.fit.result.lmdif_message
         entry['statistics'] = fit
     else:
         entry['title'] = 'Fit Model'
         entry['model'] = self.get_model()
     if 'w0' not in self.tree:
         self.tree['w0'] = nxload(self.mainwindow.scratch_file, 'rw')
     ind = []
     for key in self.tree['w0']:
         try:
             if key.startswith('f'): 
                 ind.append(int(key[1:]))
         except ValueError:
             pass
     if not ind:
         ind = [0]
     name = 'f'+str(sorted(ind)[-1]+1)
     self.tree['w0'][name] = entry
Exemple #23
0
def data(filename):

    data = nxload(filename)

    signal = np.array(data.MDHistoWorkspace.data.signal.nxdata.T)
    error_sq = np.array(data.MDHistoWorkspace.data.errors_squared.nxdata.T)

    if ('Q1' in data.MDHistoWorkspace.data.keys()):
        Qh = data.MDHistoWorkspace.data['Q1']
        Qk = data.MDHistoWorkspace.data['Q2']
        Ql = data.MDHistoWorkspace.data['Q3']
    elif ('[H,0,0]' in data.MDHistoWorkspace.data.keys()):
        Qh = data.MDHistoWorkspace.data['[H,0,0]']
        Qk = data.MDHistoWorkspace.data['[0,K,0]']
        Ql = data.MDHistoWorkspace.data['[0,0,L]']

    Qh_min, Qk_min, Ql_min = Qh.min(), Qk.min(), Ql.min()
    Qh_max, Qk_max, Ql_max = Qh.max(), Qk.max(), Ql.max()

    mh, mk, ml = Qh.size, Qk.size, Ql.size

    nh = mh - 1
    nk = mk - 1
    nl = ml - 1

    step_h = (Qh_max - Qh_min) / nh
    step_k = (Qk_max - Qk_min) / nk
    step_l = (Ql_max - Ql_min) / nl

    min_h = np.round(Qh_min + step_h / 2, 4)
    min_k = np.round(Qk_min + step_k / 2, 4)
    min_l = np.round(Ql_min + step_l / 2, 4)

    max_h = np.round(Qh_max - step_h / 2, 4)
    max_k = np.round(Qk_max - step_k / 2, 4)
    max_l = np.round(Ql_max - step_l / 2, 4)

    h_range, k_range, l_range = [min_h, max_h], [min_k, max_k], [min_l, max_l]

    return signal, error_sq, h_range, k_range, l_range, nh, nk, nl
Exemple #24
0
 def symmetrize(self, entries=False):
     if entries:
         symmetrize = symmetrize_entries
     else:
         symmetrize = symmetrize_data
     with ProcessPoolExecutor(max_workers=2) as executor:
         futures = []
         for data_type in ['signal', 'weights']:
             futures.append(
                 executor.submit(symmetrize, self.symm_function, data_type,
                                 self.data_file, self.data_path))
     for future in as_completed(futures):
         data_type, result_file = future.result()
         result_root = nxload(result_file, 'r')
         if data_type == 'signal':
             signal = result_root['data'].nxvalue
         else:
             weights = result_root['data'].nxvalue
         os.remove(result_file)
     with np.errstate(divide='ignore', invalid='ignore'):
         result = np.where(weights > 0, signal / weights, 0.0)
     return result
Exemple #25
0
    def choose_file(self):
        """
        Opens a file dialog and sets the file text box to the chosen path.
        """
        dirname = self.get_default_directory(self.filename.text())
        filename = getOpenFileName(self, 'Open File', dirname)
        if os.path.exists(filename):
            dirname = os.path.dirname(filename)
            self.filename.setText(str(filename))
            self.set_default_directory(dirname)

        root, ext = os.path.splitext(filename)
        if ext == '.nxspe':
            self.file_type = 'NXSPE'
            spe_file = nxload(filename)
            try:
                energy = spe_file.NXentry[0].NXcollection[0].fixed_energy
                self.energy_box.setText(str(energy))
            except Exception:
                pass
        else:
            self.file_type = 'SPE'
Exemple #26
0
def keep_data(data):
    """Store the data in the scratch workspace.

    Parameters
    ----------
    data : NXdata
        NXdata group containing the data to be stored

    """
    from .consoleapp import _nexpy_dir, _tree
    if 'w0' not in _tree:
        _tree['w0'] = nxload(os.path.join(_nexpy_dir, 'w0.nxs'), 'rw')
    ind = []
    for key in _tree['w0']:
        try:
            if key.startswith('s'):
                ind.append(int(key[1:]))
        except ValueError:
            pass
    if ind == []:
        ind = [0]
    data.nxname = 's' + str(sorted(ind)[-1] + 1)
    _tree['w0'][data.nxname] = data
Exemple #27
0
    def read(self, f, frame=None):
        nxroot = nx.nxload(f)
        # print nxroot.tree
        if hasattr(nxroot, 'entry'):
            if hasattr(nxroot.entry, 'data'):
                if hasattr(nxroot.entry.data, 'data'):
                    self._dgroup = nxroot.entry.data.data

        self.filename = f
        if frame is None:
            frame = 0
        if self._h5 is None:
            # Check header for unique attributes
            self._h5 = h5py.File(self.filename, 'r+')
            self.rawdata = self._h5['entry']['data']['data']
            self.readheader(f)

            self.frames = range(self.rawdata.shape[0])

        dfrm = self.rawdata[self.frames[frame]]
        self.currentframe = frame
        self.data = dfrm

        return self
import matplotlib.pyplot as plt
import numpy as np
from nexusformat import nexus as nx

a = nx.nxload(
    "/SNS/CORELLI/IPTS-15526/shared/benzil_100K_normData_sym_All_noCC.nxs")
b = nx.nxload(
    "/SNS/CORELLI/IPTS-15526/shared/benzil_300K_normData_sym_All_noCC.nxs")

signal_100 = np.ma.masked_invalid(a.MDHistoWorkspace.data.signal)
signal_300 = np.ma.masked_invalid(b.MDHistoWorkspace.data.signal)

x = np.linspace(-10, 10, 401)
y = np.linspace(-17.5, 17.5, 401)
X, Y = np.meshgrid(x, y)

fig, axes = plt.subplots(nrows=2, ncols=3, figsize=(14, 9))
axes[0, 0].pcolormesh(x, y, signal_100[25, :, :], vmin=0.0000025, vmax=0.00001)
axes[0, 0].set_title('100K HK0')
axes[0, 1].pcolormesh(x, y, signal_100[28, :, :], vmin=0.0000025, vmax=0.00001)
axes[0, 1].set_title('100K HK½')
axes[0, 2].pcolormesh(x, y, signal_100[30, :, :], vmin=0.0000025, vmax=0.00001)
axes[0, 2].set_title('100K HK1')
axes[1, 0].pcolormesh(x, y, signal_300[25, :, :], vmin=0.0000025, vmax=0.00001)
axes[1, 0].set_title('300K HK0')
axes[1, 1].pcolormesh(x, y, signal_300[28, :, :], vmin=0.0000025, vmax=0.00001)
axes[1, 1].set_title('300K HK½')
axes[1, 2].pcolormesh(x, y, signal_300[30, :, :], vmin=0.0000025, vmax=0.00001)
axes[1, 2].set_title('300K HK1')
plt.savefig("benzil_100K_300K_no_heading.png", bbox_inches='tight')
Exemple #29
0
def main():

    parser = argparse.ArgumentParser(
        description="Perform workflow for scan")
    parser.add_argument('-d', '--directory', default='', help='scan directory')
    parser.add_argument('-e', '--entries', default=['f1', 'f2', 'f3'], 
        nargs='+', help='names of NeXus files linked to this file')
    parser.add_argument('-t', '--threshold', type=float,
                        help='peak threshold - defaults to maximum counts/20')
    parser.add_argument('-f', '--first', default=20, type=int, 
                        help='first frame')
    parser.add_argument('-l', '--last', default=3630, type=int, 
                        help='last frame')
    parser.add_argument('-p', '--parent', help='file name of file to copy from')
    parser.add_argument('-r', '--refine', action='store_false',
                        help='refine lattice parameters')
                        
    
    args = parser.parse_args()

    directory = args.directory.rstrip('/')
    sample = os.path.basename(os.path.dirname(directory))
    label = os.path.basename(directory)

    print("Processing directory '%s'" % directory)
    
    entries = args.entries
    parent = args.parent
    threshold = args.threshold
    first = args.first
    last = args.last
    refine = args.refine

    wrapper_files = sorted([os.path.join(directory, filename) 
                            for filename in directory 
                            if filename.endswith('.nxs')], key=natural_sort)

    for wrapper_file in wrapper_files:
        print("Processing %s" % wrapper_file)
        root = nxload(wrapper_file)
        scan_label = os.path.splitext(os.path.basename(wrapper_file))[0][len(sample)+1:]
        for e in entries:
            print("Processing %s" % e)
            if 'logs' not in root[e]['instrument']:
                print("Reading in metadata in %s" % e)
                subprocess.call('nxingest -d %s -e %s' % (directory, e), shell=True)
            if 'maximum' not in root[e]['data'].attrs and not threshold:
                print("Determining maximum counts in %s" % f)
                subprocess.call('nxmax -d %s -e %s'
                                % (directory, e), shell=True)
            if 'peaks' not in root[e]:
                print("Finding peaks in %s" % e)
                if threshold:
                    subprocess.call('nxfind -d %s -e %s -t %s -f %s -l %s'
                        % (directory, e, threshold, first, last), shell=True)
                else:
                    subprocess.call('nxfind -d %s -e %s -f %s -l %s'
                                    % (directory, e, first, last), shell=True)

        if parent:
            print("Copying parameters from %s" % parent)
            subprocess.call('nxcopy -i %s -o %s' 
                            % (parent, wrapper_file), shell=True)
        if refine and 'orientation_matrix' in root[entries[0]]['instrument/detector']:
            subprocess.call('nxrefine -d %s' % directory, shell=True)
def __create_file_writer_command(filepath):
    streams = {}

    # DENEX detector
    detector_topic = 'denex_detector'
    __add_data_stream(streams, detector_topic, 'delay_line_detector',
                      '/entry/instrument/detector_1/raw_event_data', 'ev42')
    detector_debug_topic = 'denex_debug'
    for detector_channel in range(4):
        __add_data_stream(
            streams, detector_debug_topic, f'Denex_Adc0_Ch{detector_channel}',
            f'/entry/instrument/detector_1/pulses_channel_{detector_channel}',
            'ev42')
        __add_data_stream(
            streams, detector_debug_topic,
            f'Denex_Adc0_Ch{detector_channel}_waveform',
            f'/entry/instrument/detector_1/waveforms_channel_{detector_channel}',
            'senv')

    # Detector HV supply
    hv_supply_topic = 'V20_detectorPower'
    for hv_power_supply_channel in range(4):
        __add_data_stream(
            streams, hv_supply_topic,
            f'HZB-V20:Det-PwrC-01:02:00{hv_power_supply_channel}:VMon',
            f'/entry/instrument/detector_1/hv_supply_voltage_channel_{hv_power_supply_channel + 1}',
            'f142', 'double')
        __add_data_stream(
            streams, hv_supply_topic,
            f'HZB-V20:Det-PwrC-01:02:00{hv_power_supply_channel}:IMon',
            f'/entry/instrument/detector_1/hv_supply_current_channel_{hv_power_supply_channel + 1}',
            'f142', 'double')
        __add_data_stream(
            streams, hv_supply_topic,
            f'HZB-V20:Det-PwrC-01:02:00{hv_power_supply_channel}:Pw',
            f'/entry/instrument/detector_1/hv_supply_status_channel_{hv_power_supply_channel + 1}',
            'f142', 'int32')

    # Monitors
    monitor_topic = 'monitor'
    for monitor_number in range(4):
        __add_data_stream(streams, monitor_topic,
                          f'Monitor_Adc0_Ch{monitor_number}',
                          f'/entry/monitor_{monitor_number}/events', 'ev42')
        __add_data_stream(streams, monitor_topic,
                          f'Monitor_Adc0_Ch{monitor_number}',
                          f'/entry/monitor_{monitor_number}/waveforms', 'senv')

    # Choppers
    chopper_topic = 'V20_choppers'
    __add_data_stream(streams, chopper_topic,
                      'HZB-V20:Chop-Drv-0401:TDC_array',
                      '/entry/instrument/chopper_1/top_dead_center', 'tdct')
    __add_data_stream(streams, chopper_topic,
                      'HZB-V20:Chop-Drv-0402:TDC_array',
                      '/entry/instrument/chopper_2/top_dead_center', 'tdct')
    __add_data_stream(streams, chopper_topic,
                      'HZB-V20:Chop-Drv-0101:TDC_array',
                      '/entry/instrument/chopper_3/top_dead_center', 'tdct')
    __add_data_stream(streams, chopper_topic,
                      'HZB-V20:Chop-Drv-0102:TDC_array',
                      '/entry/instrument/chopper_4/top_dead_center', 'tdct')
    __add_data_stream(streams, chopper_topic,
                      'HZB-V20:Chop-Drv-0301:TDC_array',
                      '/entry/instrument/chopper_5/top_dead_center', 'tdct')
    __add_data_stream(streams, chopper_topic,
                      'HZB-V20:Chop-Drv-0501:TDC_array',
                      '/entry/instrument/chopper_6/top_dead_center', 'tdct')
    __add_data_stream(streams, chopper_topic,
                      'HZB-V20:Chop-Drv-0502:TDC_array',
                      '/entry/instrument/chopper_7/top_dead_center', 'tdct')
    __add_data_stream(streams, chopper_topic,
                      'HZB-V20:Chop-Drv-0302:TDC_array',
                      '/entry/instrument/chopper_8/top_dead_center', 'tdct')
    __add_data_stream(streams, chopper_topic,
                      'HZB-V20:Chop-Drv-0101:Ref_Unix_asub.VALF',
                      '/entry/instrument/chopper_3/ntp_to_mrf_comparison',
                      'f142', 'int32')

    # ESS Mini-Chopper
    __add_data_stream(streams, chopper_topic,
                      'HZB-V20:Chop-Drv-0102:TDC_array',
                      '/entry/instrument/chopper_9/top_dead_center', 'tdct')
    __add_data_stream(streams, chopper_topic,
                      'HZB-V20:Chop-Drv-0103:TDC_array',
                      '/entry/instrument/chopper_9/reference_pulse', 'tdct')
    __add_data_stream(streams, chopper_topic,
                      'HZB-V20:Chop-Drv-0104:TDC_array',
                      '/entry/instrument/chopper_9/neutron_pulse_arrival',
                      'tdct')

    # Readout system timing status
    timing_status_topic = 'V20_timingStatus'
    for readout_system_number in ('1', '2'):
        group_name = f'readout_system_{readout_system_number}'
        __add_data_stream(
            streams, timing_status_topic,
            f'HZB-V20:TS-RO{readout_system_number}:TS-SDiff-RBV',
            f'/entry/instrument/detector_1/{group_name}/s_diff', 'f142',
            'double')
        __add_data_stream(
            streams, timing_status_topic,
            f'HZB-V20:TS-RO{readout_system_number}:TS-NDiff-RBV',
            f'/entry/instrument/detector_1/{group_name}/n_diff', 'f142',
            'double')
        __add_data_stream(streams, timing_status_topic,
                          f'HZB-V20:TS-RO{readout_system_number}:STATUS2-RBV',
                          f'/entry/instrument/detector_1/{group_name}/status',
                          'f142', 'int32')

    # Motion devices
    def _add_motion_dev(pv_root: str, group_names: List[str],
                        start_index: int):
        motion_topic = 'V20_motion'
        for group_number, group_name in enumerate(group_names):
            __add_data_stream(streams, motion_topic,
                              pv_root + f"{group_number + start_index}.VAL",
                              f'/entry/instrument/{group_name}/target_value',
                              'f142', 'double')
            __add_data_stream(streams, motion_topic,
                              pv_root + f"{group_number + start_index}.RBV",
                              f'/entry/instrument/{group_name}/value', 'f142',
                              'double')
            __add_data_stream(streams, motion_topic,
                              pv_root + f"{group_number + start_index}.STAT",
                              f'/entry/instrument/{group_name}/status', 'f142',
                              'int32')
            __add_data_stream(streams, motion_topic,
                              pv_root + f"{group_number + start_index}.VELO",
                              f'/entry/instrument/{group_name}/velocity',
                              'f142', 'double')

    motion_topic = 'V20_motion'
    _add_motion_dev("TUD-SMI:MC-MCU-01:m",
                    ['linear_stage', 'tilting_angle_1', 'tilting_angle_2'],
                    start_index=1)
    _add_motion_dev("HZB-V20:MC-MCU-01:m", ['Omega_1', 'Omega_2', 'Lin1'],
                    start_index=10)

    def _add_slit(slit_group_name: str, pv_names: List[str]):
        nicos_device_name = slit_group_name.lower()
        nicos_topic = "V20_nicosCacheHistory"
        for pv_name in pv_names:
            if "H-Gap" in pv_name:
                value_name = "x_gap"
                nicos_value_name = "h_gap"
            elif "V-Gap" in pv_name:
                value_name = "y_gap"
                nicos_value_name = "v_gap"
            elif "H-Center" in pv_name:
                value_name = "x_center"
                nicos_value_name = "h_center"
            elif "V-Center" in pv_name:
                value_name = "y_center"
                nicos_value_name = "v_center"
            else:
                value_name = "value"
                nicos_value_name = "value"
            __add_data_stream(
                streams, motion_topic, f"{pv_name}.VAL",
                f'/entry/instrument/{slit_group_name}/{value_name}_target',
                'f142', 'double')
            __add_data_stream(
                streams, motion_topic, f"{pv_name}.RBV",
                f'/entry/instrument/{slit_group_name}/{value_name}', 'f142',
                'double')
            __add_data_stream(
                streams, motion_topic, f"{pv_name}.STAT",
                f'/entry/instrument/{slit_group_name}/{value_name}_status',
                'f142', 'int32')
            __add_data_stream(
                streams, motion_topic, f"{pv_name}.VELO",
                f'/entry/instrument/{slit_group_name}/{value_name}_velocity',
                'f142', 'double')

            __add_data_stream(
                streams, nicos_topic,
                f"nicos/{nicos_device_name}{nicos_value_name}/value",
                f'/entry/instrument/{slit_group_name}/{value_name}_from_nicos_cache',
                'ns10')

    _add_slit("Slit3", [
        "HZB-V20:MC-SLT-01:SltH-Center", "HZB-V20:MC-SLT-01:SltH-Gap",
        "HZB-V20:MC-SLT-01:SltV-Center", "HZB-V20:MC-SLT-01:SltV-Gap"
    ])

    links = {}

    converter = NexusToDictConverter()
    nexus_file = nexus.nxload(filepath)
    tree = converter.convert(nexus_file, streams, links)
    # The Kafka broker at V20 is v20-udder1, but due to the network setup at V20 we have to use the IP: 192.168.1.80
    # Use a timestamp in the output filename, but avoid characters "-" and ":"
    iso8601_str_seconds = datetime.now().isoformat().split('.')[0]
    timestamp = iso8601_str_seconds.replace(':', '_')
    timestamp = timestamp.replace('-', '_')
    start_time = 'STARTTIME'  # NICOS replaces STARTTIME
    stop_time = None
    file_name = 'FILENAME'  # NICOS replaces FILENAME
    write_command, stop_command = create_writer_commands(
        tree,
        '/data/kafka-to-nexus/FILENAME',
        broker='192.168.1.80:9092',
        start_time=start_time,
        stop_time=stop_time)
    object_to_json_file(write_command, 'V20_file_write_start.json')
    object_to_json_file(stop_command, 'V20_file_write_stop.json')
        value_strings = f.readline().replace('\n', '').split('\t')
        decs = []
        for i in range(len(value_strings)):
            int_dec = value_strings[i].split('.')
            if len(int_dec) == 1:
                decs += [0]  #must be integer
            else:
                decs += [len(int_dec[1])]  # number of decimal places

    return (cols, header, cmd, decs)


(cols, srs_header, cmd,
 decs) = getScanOutputNamesFromDatFile('%s%i.dat' % (inpath, filenum))

n = nx.nxload('%s%i.nxs' % (inpath, filenum))

n.save('%s%i.nxs' % (outpath, filenum),
       'w')  # seems to be required in order to change read/write mode

with n.nxfile:
    n['/entry1'].attrs['default'] = 'scan'
    n['entry1/scan'] = nx.NXsubentry(definition='NXclassic_scan')
    n['/entry1/scan'].attrs['default'] = 'measurement'
    n['entry1/scan/title'] = cmd  #get command string from .dat file
    n['entry1/scan/start_time'] = nx.NXlink(n['/entry1/start_time'])
    n['entry1/scan/end_time'] = nx.NXlink(n['/entry1/end_time'])
    n['entry1/scan/scan_command'] = nx.NXlink(n['/entry1/scan_command'])
    n['entry1/scan/measurement'] = n['/entry1/measurement']
    n['entry1/scan/scan_header'] = srs_header
    n['entry1/scan/scan_fields'] = cols
Exemple #32
0
def main():

    parser = argparse.ArgumentParser(description="Perform workflow for scan")
    parser.add_argument('-d', '--directory', default='', help='scan directory')
    parser.add_argument('-e',
                        '--entries',
                        default=['f1', 'f2', 'f3'],
                        nargs='+',
                        help='names of NeXus files linked to this file')
    parser.add_argument('-f',
                        '--first',
                        default=20,
                        type=int,
                        help='first frame')
    parser.add_argument('-l',
                        '--last',
                        default=3630,
                        type=int,
                        help='last frame')
    parser.add_argument('-p',
                        '--parent',
                        help='file name of file to copy from')
    parser.add_argument('-r',
                        '--refine',
                        action='store_true',
                        help='refine lattice parameters')
    parser.add_argument('-t',
                        '--transform',
                        action='store_true',
                        help='perform CCTW transforms')

    args = parser.parse_args()

    directory = args.directory.rstrip('/')
    sample = os.path.basename(os.path.dirname(os.path.dirname(directory)))
    label = os.path.basename(os.path.dirname(directory))
    scan = os.path.basename(directory)
    wrapper_file = os.path.join(sample, label, '%s_%s.nxs' % (sample, scan))
    entries = args.entries
    parent = args.parent
    first = args.first
    last = args.last
    refine = args.refine
    transform = args.transform

    if not os.path.exists(wrapper_file):
        print("'%s' does not exist" % wrapper_file)
        sys.exit(1)
    else:
        root = nxload(wrapper_file, 'rw')

    if parent == wrapper_file:
        parent = None
    elif parent:
        if not os.path.exists(parent):
            print("'%s' does not exist" % parent)
            sys.exit(1)

    print('Performing workflow on', wrapper_file)

    for entry in entries:
        print("Processing", entry)
        subprocess.call('nxlink -d %s -e %s' % (directory, entry), shell=True)
        subprocess.call('nxmax -d %s -e %s' % (directory, entry), shell=True)
        subprocess.call('nxfind -d %s -e %s -f %s -l %s' %
                        (directory, entry, first, last),
                        shell=True)

    if parent:
        subprocess.call('nxcopy -i %s -o %s' % (parent, wrapper_file),
                        shell=True)
    if refine and 'orientation_matrix' in root[
            entries[0]]['instrument/detector']:
        subprocess.call('nxrefine -d %s' % directory, shell=True)
    if transform:
        if parent:
            subprocess.call('nxtransform -d %s -p %s' % (directory, parent),
                            shell=True)
        else:
            subprocess.call('nxtransform -d %s' % directory, shell=True)
        subprocess.call('nxcombine -d %s' % directory, shell=True)
Exemple #33
0
    def __init__(self, database=None, visit=None, nexus_file=None):
        ###import config file
        self.pypline_dir = os.path.dirname(os.path.realpath(__file__))
        with open(self.pypline_dir+'/config.yaml', 'r') as ymlfile:
            self.myconfig = yaml.load(ymlfile)

        ###connect to the logger
        self.logger = myLogger(os.path.basename(__file__))

        ###set some parameters
        try:
            if visit.type == 'visit':
                self.visit = visit
            else:
                raise TypeError('ParseNXS needs a visit instance')
            if database.type == 'database':
                self.database = database
            else:
                raise TypeError('ParseNXS needs a database instance')
        except Exception as ex:
            template = "An exception of type {0} occured. {1!r}"
            message = template.format(type(ex).__name__, ex.args)
            self.logger.error(message)
            
        self.type = 'nxs'
        self.success = False
        self.hplc_pattern = re.compile('\ASECSAXS ')
        self.hplc_sample_pattern = re.compile(' Peak [0-9]+\Z')
        self.robot_sample_pattern_start = re.compile('\ASample: ')
        self.robot_sample_pattern_end = re.compile(' \(Location {1,3}I{1,3} [A-H]  ?[1-9]?[0-2]?\)\Z')
        self.robot_buffer_pattern = re.compile('Buffer for next and preceding sample measurement')

        self.nexus_file = str(nexus_file)
        self.scan_command = ''
        self.descriptive_title = 'Instrument Scan'
        self.scan_type = 'Instrument Scan'
        self.outfile_prefix = 'Instrument_Scan'
        self.file_code = 0
        self.dat_dir = ''
        self.number_of_exposures = 1
        self.exposure_time = 1.0
        self.file_time = ''
        self.visit_id = ''

        self.dat_data = []

        try:
            if not os.path.isfile(self.nexus_file) and self.nexus_file[-4:] == '.nxs':
                raise IOError(str(self.nexus_file)+' does not exist or is not of type .nxs')
            try:
                mynxs = nxload(self.nexus_file)
                    
                self.scan_command = str(mynxs.entry1.scan_command.nxdata)
                self.file_code = int(mynxs.entry1.entry_identifier.nxdata)
                self.dat_dir = os.path.dirname(self.nexus_file)+'/'+self.myconfig['settings']['existing_dat_file_directory']+'/'
                self.file_time = datetime.fromtimestamp(os.path.getmtime(nexus_file)).strftime('%Y/%m/%d_%H:%M:%S')

                self.visit_id = str(mynxs.entry1.experiment_identifier.nxdata)
                if self.scan_command == 'static readout':
                    m = re.match(r"(Sample: )(.*)( \(Location.*\))", str(mynxs.entry1.title.nxdata))
                    if m:
                        descriptive_title = m.group(2)
                    else:
                        descriptive_title = str(mynxs.entry1.title.nxdata)
                         
                    self.descriptive_title = self.MakeSafeFilename(descriptive_title)
                    if str(mynxs.entry1.environment.type) == 'BSSC' and str(mynxs.entry1.sample.type) == 'buffer':
                        self.scan_type = 'Robot Buffer'
                    elif str(mynxs.entry1.environment.type) == 'BSSC' and str(mynxs.entry1.sample.type) == 'sample+buffer':
                        self.scan_type = 'Robot Sample'
                    elif str(mynxs.entry1.environment.type) == 'HPLC' and str(mynxs.entry1.sample.type) == 'buffer':
                        self.scan_type = 'SEC Buffer'
                    elif str(mynxs.entry1.environment.type) == 'HPLC' and str(mynxs.entry1.sample.type) == 'sample+buffer':
                        self.scan_type = 'SEC Sample'
                    else:
                        self.scan_type = 'Manual'


                    self.outfile_prefix = self.MakeSafeFilename(self.descriptive_title)
                    self.number_of_exposures = int(mynxs.entry1.instrument.detector.count_time.size) 
                    self.exposure_time = float(mynxs.entry1.instrument.detector.count_time[0][0])
                    self.success = True
            except:
                self.logger.error('nxs file: '+str(self.nexus_file)+' could not be parsed')
                self.descriptive_title = 'ERROR IN NXS FILE'
                self.scan_type = 'Error'
                self.file_time = datetime.strftime(datetime.now(), '%Y/%m/%d_%H:%M:%S')
                self.visit_id = self.visit.ReturnVisitID()
                self.success = False
        except Exception as ex:
            template = "An exception of type {0} occured. {1!r}"
            message = template.format(type(ex).__name__, ex.args)
            self.logger.error(message)
Exemple #34
0
    def sync_file(self, filename):
        """Synchronize the NeXus file contents to the database.

        Parameters
        ----------
        filename : str
            Path of wrapper file relative to GUP directory.

        Returns
        -------
        File
            Updated File object.
        """
        f = self.get_file(filename)
        sample_dir = os.path.dirname(filename)
        if f:
            try:
                scan_files = os.listdir(get_directory(filename))
            except OSError:
                scan_files = []
            root = nxload(filename)
            entries = [e for e in root.entries if e != 'entry']
            tasks = {t: 0 for t in self.task_names}
            for e in entries:
                nxentry = root[e]
                if e in root and 'data' in nxentry and 'instrument' in nxentry:
                    if e + '.h5' in scan_files or e + '.nxs' in scan_files:
                        tasks['data'] += 1
                    if 'nxlink' in nxentry:
                        tasks['nxlink'] += 1
                    if 'nxmax' in nxentry:
                        tasks['nxmax'] += 1
                    if 'nxfind' in nxentry:
                        tasks['nxfind'] += 1
                    if 'nxcopy' in nxentry or is_parent(filename, sample_dir):
                        tasks['nxcopy'] += 1
                    if 'nxrefine' in nxentry:
                        tasks['nxrefine'] += 1
                    if 'nxprepare_mask' in nxentry:
                        tasks['nxprepare'] += 1
                    if 'nxtransform' in nxentry:
                        tasks['nxtransform'] += 1
                    if 'nxmasked_transform' in nxentry or 'nxmask' in nxentry:
                        tasks['nxmasked_transform'] += 1
            if 'nxcombine' in root['entry']:
                tasks['nxcombine'] = len(entries)
            if 'nxmasked_combine' in root['entry']:
                tasks['nxmasked_combine'] = len(entries)
            if 'nxpdf' in root['entry']:
                tasks['nxpdf'] = len(entries)
            if 'nxmasked_pdf' in root['entry']:
                tasks['nxmasked_pdf'] = len(entries)
            for task, value in tasks.items():
                if value == 0:
                    setattr(f, task, NOT_STARTED)
                elif value == len(entries):
                    setattr(f, task, DONE)
                else:
                    setattr(f, task, IN_PROGRESS)
            f.set_entries(entries)
            self.session.commit()
        return f
Exemple #35
0
def main():

    parser = argparse.ArgumentParser(
        description="Perform workflow for scan")
    parser.add_argument('-d', '--directory', default='', help='scan directory')
    parser.add_argument('-e', '--entries', default=['f1', 'f2', 'f3'], 
        nargs='+', help='names of NeXus files linked to this file')
    parser.add_argument('-f', '--first', default=20, type=int, 
                        help='first frame')
    parser.add_argument('-l', '--last', default=3630, type=int, 
                        help='last frame')
    parser.add_argument('-p', '--parent', help='file name of file to copy from')
    parser.add_argument('-r', '--refine', action='store_true',
                        help='refine lattice parameters')
    parser.add_argument('-t', '--transform', action='store_true',
                        help='perform CCTW transforms')
   
    args = parser.parse_args()

    directory = args.directory.rstrip('/')
    sample = os.path.basename(os.path.dirname(os.path.dirname(directory)))   
    label = os.path.basename(os.path.dirname(directory))
    scan = os.path.basename(directory)
    wrapper_file = os.path.join(sample, label, '%s_%s.nxs' % (sample, scan))
    entries = args.entries
    parent = args.parent
    first = args.first
    last = args.last
    refine = args.refine
    transform = args.transform

    if not os.path.exists(wrapper_file):
        print("'%s' does not exist" % wrapper_file)
        sys.exit(1)
    else:
        root = nxload(wrapper_file, 'rw')

    if parent == wrapper_file:
        parent = None
    elif parent:
        if not os.path.exists(parent):
            print("'%s' does not exist" % parent)
            sys.exit(1)
        
    print('Performing workflow on', wrapper_file)

    for entry in entries:
        print("Processing", entry)
        subprocess.call('nxlink -d %s -e %s' % (directory, entry), shell=True)
        subprocess.call('nxmax -d %s -e %s' % (directory, entry), shell=True)
        subprocess.call('nxfind -d %s -e %s -f %s -l %s'
                        % (directory, entry, first, last), shell=True)

    if parent:
        subprocess.call('nxcopy -i %s -o %s' % (parent, wrapper_file), shell=True)
    if refine and 'orientation_matrix' in root[entries[0]]['instrument/detector']:
        subprocess.call('nxrefine -d %s' % directory, shell=True)
    if transform:
        if parent:
            subprocess.call('nxtransform -d %s -p %s' % (directory, parent), shell=True)
        else:
            subprocess.call('nxtransform -d %s' % directory, shell=True)
        subprocess.call('nxcombine -d %s' % directory, shell=True)
import matplotlib.pyplot as plt
import numpy as np
from nexusformat import nexus as nx

a=nx.nxload("/SNS/CORELLI/IPTS-15526/shared/benzil_100K_normData_sym_All_noCC.nxs")
b=nx.nxload("/SNS/CORELLI/IPTS-15526/shared/benzil_300K_normData_sym_All_noCC.nxs")

signal_100 = np.ma.masked_invalid(a.MDHistoWorkspace.data.signal)
signal_300 = np.ma.masked_invalid(b.MDHistoWorkspace.data.signal)

x = np.linspace(-10, 10, 401)
y = np.linspace(-17.5, 17.5, 401)
X, Y = np.meshgrid(x, y)

fig, axes = plt.subplots(nrows=2, ncols=3, figsize=(14, 9))
axes[0,0].pcolormesh(x,y,signal_100[25,:,:],vmin=0.0000025,vmax=0.00001)
axes[0,0].set_title('100K HK0')
axes[0,1].pcolormesh(x,y,signal_100[28,:,:],vmin=0.0000025,vmax=0.00001)
axes[0,1].set_title('100K HK½')
axes[0,2].pcolormesh(x,y,signal_100[30,:,:],vmin=0.0000025,vmax=0.00001)
axes[0,2].set_title('100K HK1')
axes[1,0].pcolormesh(x,y,signal_300[25,:,:],vmin=0.0000025,vmax=0.00001)
axes[1,0].set_title('300K HK0')
axes[1,1].pcolormesh(x,y,signal_300[28,:,:],vmin=0.0000025,vmax=0.00001)
axes[1,1].set_title('300K HK½')
axes[1,2].pcolormesh(x,y,signal_300[30,:,:],vmin=0.0000025,vmax=0.00001)
axes[1,2].set_title('300K HK1')
plt.savefig("benzil_100K_300K_no_heading.png",bbox_inches='tight')


part0=nx.nxload("/SNS/users/rwp/benzil/benzil_300K_normData_sym_part0_All_noCC.nxs")
Exemple #37
0
def main():

    parser = argparse.ArgumentParser(description="Perform workflow for scan")
    parser.add_argument('-d', '--directory', default='', help='scan directory')
    parser.add_argument('-e',
                        '--entries',
                        default=['f1', 'f2', 'f3'],
                        nargs='+',
                        help='names of NeXus files linked to this file')
    parser.add_argument('-t',
                        '--threshold',
                        type=float,
                        help='peak threshold - defaults to maximum counts/20')
    parser.add_argument('-f',
                        '--first',
                        default=20,
                        type=int,
                        help='first frame')
    parser.add_argument('-l',
                        '--last',
                        default=3630,
                        type=int,
                        help='last frame')
    parser.add_argument('-p',
                        '--parent',
                        help='file name of file to copy from')
    parser.add_argument('-r',
                        '--refine',
                        action='store_false',
                        help='refine lattice parameters')

    args = parser.parse_args()

    directory = args.directory.rstrip('/')
    sample = os.path.basename(os.path.dirname(directory))
    label = os.path.basename(directory)

    print("Processing directory '%s'" % directory)

    entries = args.entries
    parent = args.parent
    threshold = args.threshold
    first = args.first
    last = args.last
    refine = args.refine

    wrapper_files = sorted([
        os.path.join(directory, filename)
        for filename in directory if filename.endswith('.nxs')
    ],
                           key=natural_sort)

    for wrapper_file in wrapper_files:
        print("Processing %s" % wrapper_file)
        root = nxload(wrapper_file)
        scan_label = os.path.splitext(
            os.path.basename(wrapper_file))[0][len(sample) + 1:]
        for e in entries:
            print("Processing %s" % e)
            if 'logs' not in root[e]['instrument']:
                print("Reading in metadata in %s" % e)
                subprocess.call('nxingest -d %s -e %s' % (directory, e),
                                shell=True)
            if 'maximum' not in root[e]['data'].attrs and not threshold:
                print("Determining maximum counts in %s" % f)
                subprocess.call('nxmax -d %s -e %s' % (directory, e),
                                shell=True)
            if 'peaks' not in root[e]:
                print("Finding peaks in %s" % e)
                if threshold:
                    subprocess.call('nxfind -d %s -e %s -t %s -f %s -l %s' %
                                    (directory, e, threshold, first, last),
                                    shell=True)
                else:
                    subprocess.call('nxfind -d %s -e %s -f %s -l %s' %
                                    (directory, e, first, last),
                                    shell=True)

        if parent:
            print("Copying parameters from %s" % parent)
            subprocess.call('nxcopy -i %s -o %s' % (parent, wrapper_file),
                            shell=True)
        if refine and 'orientation_matrix' in root[
                entries[0]]['instrument/detector']:
            subprocess.call('nxrefine -d %s' % directory, shell=True)
Exemple #38
0
 def __init__(self, local_path: str):
     super().__init__(local_path)
     self.nxfile = nxload(local_path)
def readnxs(filename, show=False):
	nxsdata = nxs.nxload(filename)
	if show:
		print(nxsdata.tree)
	return nxsdata