Exemple #1
0
def main():

    parser = argparse.ArgumentParser(
        description="Restore orientation from CCTW parameter file")
    parser.add_argument('-d', '--directory', required=True, 
                        help='scan directory')
    parser.add_argument('-e', '--entries', default=['f1', 'f2', 'f3'], 
        nargs='+', help='names of entries to be processed')
    
    args = parser.parse_args()

    for entry in args.entries:
        reduce = NXReduce(entry, args.directory)
        if os.path.exists(reduce.settings_file):
            refine = NXRefine(reduce.entry)
            refine.read_settings(reduce.settings_file)
            refine.write_parameters()
            if os.path.exists(reduce.transform_file):
                refine.prepare_transform(reduce.transform_file)
            if os.path.exists(reduce.masked_transform_file):
                refine.prepare_transform(reduce.masked_transform_file, 
                                         mask=True)
        reduce = NXMultiReduce(args.directory)
        if os.path.exists(reduce.transform_file):
            reduce.prepare_combine()
        if os.path.exists(reduce.masked_transform_file):
            reduce.mask = True
            reduce.prepare_combine()
Exemple #2
0
class TransformDialog(BaseDialog):

    def __init__(self, parent=None):
        super(TransformDialog, self).__init__(parent)
        
        self.select_entry(self.initialize_grid)
        self.refine = NXRefine()

        grid = QtWidgets.QGridLayout()
        grid.setSpacing(10)
        header_font = QtGui.QFont()
        header_font.setBold(True)
        start_label = QtWidgets.QLabel('Start')
        start_label.setFont(header_font)
        grid.addWidget(start_label, 0, 1)
        step_label = QtWidgets.QLabel('Step')
        step_label.setFont(header_font)
        grid.addWidget(step_label, 0, 2)
        stop_label = QtWidgets.QLabel('Stop')
        stop_label.setFont(header_font)
        grid.addWidget(stop_label, 0, 3)
        grid.addWidget(QtWidgets.QLabel('H:'), 1, 0)
        grid.addWidget(QtWidgets.QLabel('K:'), 2, 0)
        grid.addWidget(QtWidgets.QLabel('L:'), 3, 0)
        self.start_h_box = QtWidgets.QLineEdit()
        self.step_h_box = QtWidgets.QLineEdit()
        self.stop_h_box = QtWidgets.QLineEdit()
        grid.addWidget(self.start_h_box, 1, 1)
        grid.addWidget(self.step_h_box, 1, 2)
        grid.addWidget(self.stop_h_box, 1, 3)
        self.start_k_box = QtWidgets.QLineEdit()
        self.step_k_box = QtWidgets.QLineEdit()
        self.stop_k_box = QtWidgets.QLineEdit()
        grid.addWidget(self.start_k_box, 2, 1)
        grid.addWidget(self.step_k_box, 2, 2)
        grid.addWidget(self.stop_k_box, 2, 3)
        self.start_l_box = QtWidgets.QLineEdit()
        self.step_l_box = QtWidgets.QLineEdit()
        self.stop_l_box = QtWidgets.QLineEdit()
        grid.addWidget(self.start_l_box, 3, 1)
        grid.addWidget(self.step_l_box, 3, 2)
        grid.addWidget(self.stop_l_box, 3, 3)
        self.set_layout(self.entry_layout, grid, 
            self.checkboxes(('copy', 'Copy to all entries', True),
                            ('mask', 'Create masked transform group', True),
                            ('overwrite', 'Overwrite existing transforms', False)),
            self.close_buttons(save=True))
        self.setWindowTitle('Transforming Data')
        try:
            self.initialize_grid()
        except Exception:
            pass

    def get_output_file(self, mask=False, entry=None):
        if entry is None:
            entry = self.entry            
        if mask:
            return os.path.splitext(entry.data.nxsignal.nxfilename)[0]+'_masked_transform.nxs'
        else:
            return os.path.splitext(entry.data.nxsignal.nxfilename)[0]+'_transform.nxs'

    def get_settings_file(self, entry=None):
        if entry is None:
            entry = self.entry            
        return os.path.splitext(entry.data.nxsignal.nxfilename)[0]+'_transform.pars'

    def get_h_grid(self):
        return (np.float32(self.start_h_box.text()),
                np.float32(self.step_h_box.text()),
                np.float32(self.stop_h_box.text()))

    def get_k_grid(self):
        return (np.float32(self.start_k_box.text()),
                np.float32(self.step_k_box.text()),
                np.float32(self.stop_k_box.text()))

    def get_l_grid(self):
        return (np.float32(self.start_l_box.text()),
                np.float32(self.step_l_box.text()),
                np.float32(self.stop_l_box.text()))

    def initialize_grid(self):
        self.refine = NXRefine(self.entry)
        self.refine.initialize_grid()
        self.start_h_box.setText('%g' % self.refine.h_start)
        self.step_h_box.setText('%g' % self.refine.h_step)
        self.stop_h_box.setText('%g' % self.refine.h_stop)
        self.start_k_box.setText('%g' % self.refine.k_start)
        self.step_k_box.setText('%g' % self.refine.k_step)
        self.stop_k_box.setText('%g' % self.refine.k_stop)
        self.start_l_box.setText('%g' % self.refine.l_start)
        self.step_l_box.setText('%g' % self.refine.l_step)
        self.stop_l_box.setText('%g' % self.refine.l_stop)

    def write_parameters(self, output_file, settings_file):
        self.refine.output_file = output_file
        self.refine.settings_file = settings_file
        self.refine.h_start, self.refine.h_step, self.refine.h_stop = self.get_h_grid()
        self.refine.k_start, self.refine.k_step, self.refine.k_stop = self.get_k_grid()
        self.refine.l_start, self.refine.l_step, self.refine.l_stop = self.get_l_grid()
        self.refine.define_grid()

    @property
    def copy(self):
        return self.checkbox['copy'].isChecked()

    @property
    def mask(self):
        return self.checkbox['mask'].isChecked()

    @property
    def overwrite(self):
        return self.checkbox['overwrite'].isChecked()

    def accept(self):
        try:
            if 'transform' in self.entry and not self.overwrite:
                self.display_message('Preparing Transform',
                    'Transform group already exists in %s' % self.entry.nxname)
                return
            if self.mask and 'masked_transform' in self.entry and not self.overwrite:
                self.display_message('Preparing Transform',
                    'Masked transform group already exists in %s' % self.entry.nxname)
                return
            output_file = self.get_output_file()
            settings_file = self.get_settings_file()
            self.write_parameters(output_file, settings_file)
            self.refine.prepare_transform(output_file)
            if self.mask:
                masked_output_file = self.get_output_file(mask=True)
                self.refine.prepare_transform(masked_output_file, mask=True)
            self.refine.write_settings(settings_file)
            if self.copy:
                root = self.entry.nxroot
                for entry in [e for e in root 
                              if e != 'entry' and e != self.entry.nxname]:
                    if 'transform' in root[entry] and not self.overwrite:
                        self.display_message('Preparing Transform',
                            'Transform group already exists in %s' % entry)
                        return
                    if self.mask and 'masked_transform' in root[entry] and not self.overwrite:
                        self.display_message('Preparing Transform',
                            'Masked transform group already exists in %s' % entry)
                        return
                    self.refine = NXRefine(root[entry])
                    output_file = self.get_output_file(entry=root[entry])
                    settings_file = self.get_settings_file(entry=root[entry])
                    self.write_parameters(output_file, settings_file)
                    self.refine.prepare_transform(output_file)
                    if self.mask:
                        masked_output_file = self.get_output_file(mask=True, 
                                                                  entry=root[entry])
                        self.refine.prepare_transform(masked_output_file, mask=True)
                    self.refine.write_settings(settings_file)
                    
            super(TransformDialog, self).accept()
        except NeXusError as error:
            report_error("Preparing Data Transform", error)
Exemple #3
0
class TransformDialog(NXDialog):
    def __init__(self, parent=None):
        super().__init__(parent)

        self.select_entry(self.choose_entry)

        self.Qgrid = QtWidgets.QGridLayout()
        self.Qgrid.setSpacing(10)
        headers = ['Axis', 'Q', 'dQ', 'N', 'Max']
        width = [25, 50, 50, 25, 50]
        column = 0
        for header in headers:
            label = NXLabel(header, bold=True, align='center')
            self.Qgrid.addWidget(label, 0, column)
            self.Qgrid.setColumnMinimumWidth(column, width[column])
            column += 1
        self.Qbox = {}
        self.dQbox = {}
        self.Nbox = {}
        self.maxbox = {}
        for i, label in enumerate(['H', 'K', 'L']):
            self.Qgrid.addWidget(NXLabel(label, align='center'), i + 1, 0)
            self.Qbox[label] = NXLineEdit(slot=self.calculate,
                                          width=100,
                                          align='right')
            self.Qgrid.addWidget(self.Qbox[label], i + 1, 1)
            self.dQbox[label] = NXLineEdit(slot=self.calculate,
                                           width=100,
                                           align='right')
            self.Qgrid.addWidget(self.dQbox[label], i + 1, 2)
            self.Nbox[label] = NXLabel(align='center')
            self.Qgrid.addWidget(self.Nbox[label], i + 1, 3)
            self.maxbox[label] = NXLabel(align='center')
            self.Qgrid.addWidget(self.maxbox[label], i + 1, 4)
        self.set_layout(self.entry_layout, self.close_buttons(save=True))
        self.setWindowTitle('Transforming Data')
        try:
            self.initialize_grid()
        except Exception:
            pass

    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()

    def update_grid(self):
        self.Qbox['H'].setText(f"{self.refine.h_stop:g}")
        self.Qbox['K'].setText(f"{self.refine.k_stop:g}")
        self.Qbox['L'].setText(f"{self.refine.l_stop:g}")
        self.dQbox['H'].setText(f"{self.refine.h_step:g}")
        self.dQbox['K'].setText(f"{self.refine.k_step:g}")
        self.dQbox['L'].setText(f"{self.refine.l_step:g}")
        self.Nbox['H'].setText(f"{self.refine.h_shape:g}")
        self.Nbox['K'].setText(f"{self.refine.k_shape:g}")
        self.Nbox['L'].setText(f"{self.refine.l_shape:g}")
        self.maxbox['H'].setText(f"{self.refine.Qmax / self.refine.astar:g}")
        self.maxbox['K'].setText(f"{self.refine.Qmax / self.refine.bstar:g}")
        self.maxbox['L'].setText(f"{self.refine.Qmax / self.refine.cstar:g}")

    def calculate(self):
        for label, rlu in [('H', self.refine.astar), ('K', self.refine.bstar),
                           ('L', self.refine.cstar)]:
            self.Nbox[label].setText(
                int(
                    np.round(
                        2 * float(self.Qbox[label].text()) /
                        float(self.dQbox[label].text()), 2)) + 1)
            self.maxbox[label].setText(f"{self.refine.Qmax / rlu:g}")

    def get_output_file(self, mask=False, entry=None):
        if entry is None:
            entry = self.entry
        if mask:
            return os.path.splitext(
                entry.data.nxsignal.nxfilename)[0] + '_masked_transform.nxs'
        else:
            return os.path.splitext(
                entry.data.nxsignal.nxfilename)[0] + '_transform.nxs'

    def get_settings_file(self, entry=None):
        if entry is None:
            entry = self.entry
        return os.path.splitext(
            entry.data.nxsignal.nxfilename)[0] + '_transform.pars'

    def get_parameters(self, Q):
        stop, step = float(self.Qbox[Q].text()), float(self.dQbox[Q].text())
        return -stop, step, stop

    def write_parameters(self, output_file, settings_file):
        self.refine.output_file = output_file
        self.refine.settings_file = settings_file
        self.refine.h_start, self.refine.h_step, self.refine.h_stop = (
            self.get_parameters('H'))
        self.refine.k_start, self.refine.k_step, self.refine.k_stop = (
            self.get_parameters('K'))
        self.refine.l_start, self.refine.l_step, self.refine.l_stop = (
            self.get_parameters('L'))
        self.refine.define_grid()

    @property
    def copy(self):
        return self.checkbox['copy'].isChecked()

    @property
    def mask(self):
        return self.checkbox['mask'].isChecked()

    @property
    def overwrite(self):
        return self.checkbox['overwrite'].isChecked()

    def accept(self):
        try:
            if 'transform' in self.entry and not self.overwrite:
                self.display_message(
                    "Preparing Transform",
                    f"Transform group already exists in {self.entry.nxname}")
                return
            if (self.mask and 'masked_transform' in self.entry
                    and not self.overwrite):
                self.display_message(
                    "Preparing Transform",
                    "Masked transform group already exists in "
                    f"{self.entry.nxname}")
                return
            output_file = self.get_output_file()
            settings_file = self.get_settings_file()
            self.write_parameters(output_file, settings_file)
            self.refine.prepare_transform(output_file)
            if self.mask:
                masked_output_file = self.get_output_file(mask=True)
                self.refine.prepare_transform(masked_output_file, mask=True)
            self.refine.write_settings(settings_file)
            if self.copy:
                root = self.entry.nxroot
                for entry in [
                        e for e in root
                        if e != 'entry' and e != self.entry.nxname
                ]:
                    if 'transform' in root[entry] and not self.overwrite:
                        self.display_message(
                            "Preparing Transform",
                            f"Transform group already exists in {entry}")
                        return
                    if (self.mask and 'masked_transform' in root[entry]
                            and not self.overwrite):
                        self.display_message(
                            "Preparing Transform",
                            f"Masked transform group already exists in {entry}"
                        )
                        return
                    self.refine = NXRefine(root[entry])
                    output_file = self.get_output_file(entry=root[entry])
                    settings_file = self.get_settings_file(entry=root[entry])
                    self.write_parameters(output_file, settings_file)
                    self.refine.prepare_transform(output_file)
                    if self.mask:
                        masked_output_file = self.get_output_file(
                            mask=True, entry=root[entry])
                        self.refine.prepare_transform(masked_output_file,
                                                      mask=True)
                    self.refine.write_settings(settings_file)
            super().accept()
        except NeXusError as error:
            report_error("Preparing Data Transform", error)
Exemple #4
0
class TransformDialog(BaseDialog):
    def __init__(self, parent=None):
        super(TransformDialog, self).__init__(parent)

        self.select_entry(self.initialize_grid)
        self.refine = NXRefine()

        grid = QtWidgets.QGridLayout()
        grid.setSpacing(10)
        header_font = QtGui.QFont()
        header_font.setBold(True)
        start_label = QtWidgets.QLabel('Start')
        start_label.setFont(header_font)
        grid.addWidget(start_label, 0, 1)
        step_label = QtWidgets.QLabel('Step')
        step_label.setFont(header_font)
        grid.addWidget(step_label, 0, 2)
        stop_label = QtWidgets.QLabel('Stop')
        stop_label.setFont(header_font)
        grid.addWidget(stop_label, 0, 3)
        grid.addWidget(QtWidgets.QLabel('H:'), 1, 0)
        grid.addWidget(QtWidgets.QLabel('K:'), 2, 0)
        grid.addWidget(QtWidgets.QLabel('L:'), 3, 0)
        self.start_h_box = QtWidgets.QLineEdit()
        self.step_h_box = QtWidgets.QLineEdit()
        self.stop_h_box = QtWidgets.QLineEdit()
        grid.addWidget(self.start_h_box, 1, 1)
        grid.addWidget(self.step_h_box, 1, 2)
        grid.addWidget(self.stop_h_box, 1, 3)
        self.start_k_box = QtWidgets.QLineEdit()
        self.step_k_box = QtWidgets.QLineEdit()
        self.stop_k_box = QtWidgets.QLineEdit()
        grid.addWidget(self.start_k_box, 2, 1)
        grid.addWidget(self.step_k_box, 2, 2)
        grid.addWidget(self.stop_k_box, 2, 3)
        self.start_l_box = QtWidgets.QLineEdit()
        self.step_l_box = QtWidgets.QLineEdit()
        self.stop_l_box = QtWidgets.QLineEdit()
        grid.addWidget(self.start_l_box, 3, 1)
        grid.addWidget(self.step_l_box, 3, 2)
        grid.addWidget(self.stop_l_box, 3, 3)
        self.set_layout(
            self.entry_layout, grid,
            self.checkboxes(
                ('copy', 'Copy to all entries', True),
                ('mask', 'Create masked transform group', True),
                ('overwrite', 'Overwrite existing transforms', False)),
            self.close_buttons(save=True))
        self.setWindowTitle('Transforming Data')
        try:
            self.initialize_grid()
        except Exception:
            pass

    def get_output_file(self, mask=False):
        if mask:
            return os.path.splitext(self.entry.data.nxsignal.nxfilename
                                    )[0] + '_masked_transform.nxs'
        else:
            return os.path.splitext(
                self.entry.data.nxsignal.nxfilename)[0] + '_transform.nxs'

    def get_settings_file(self):
        return os.path.splitext(
            self.entry.data.nxsignal.nxfilename)[0] + '_transform.pars'

    def get_h_grid(self):
        return (np.float32(self.start_h_box.text()),
                np.float32(self.step_h_box.text()),
                np.float32(self.stop_h_box.text()))

    def get_k_grid(self):
        return (np.float32(self.start_k_box.text()),
                np.float32(self.step_k_box.text()),
                np.float32(self.stop_k_box.text()))

    def get_l_grid(self):
        return (np.float32(self.start_l_box.text()),
                np.float32(self.step_l_box.text()),
                np.float32(self.stop_l_box.text()))

    def initialize_grid(self):
        self.refine = NXRefine(self.entry)
        self.refine.initialize_grid()
        self.start_h_box.setText('%g' % self.refine.h_start)
        self.step_h_box.setText('%g' % self.refine.h_step)
        self.stop_h_box.setText('%g' % self.refine.h_stop)
        self.start_k_box.setText('%g' % self.refine.k_start)
        self.step_k_box.setText('%g' % self.refine.k_step)
        self.stop_k_box.setText('%g' % self.refine.k_stop)
        self.start_l_box.setText('%g' % self.refine.l_start)
        self.step_l_box.setText('%g' % self.refine.l_step)
        self.stop_l_box.setText('%g' % self.refine.l_stop)

    def write_parameters(self, output_file, settings_file):
        self.refine.output_file = output_file
        self.refine.settings_file = settings_file
        self.refine.h_start, self.refine.h_step, self.refine.h_stop = self.get_h_grid(
        )
        self.refine.k_start, self.refine.k_step, self.refine.k_stop = self.get_k_grid(
        )
        self.refine.l_start, self.refine.l_step, self.refine.l_stop = self.get_l_grid(
        )
        self.refine.define_grid()

    @property
    def copy(self):
        return self.checkbox['copy'].isChecked()

    @property
    def mask(self):
        return self.checkbox['mask'].isChecked()

    @property
    def overwrite(self):
        return self.checkbox['overwrite'].isChecked()

    def accept(self):
        try:
            if 'transform' in self.entry and not self.overwrite:
                self.display_message(
                    'Preparing Transform',
                    'Transform group already exists in %s' % self.entry.nxname)
                return
            if self.mask and 'masked_transform' in self.entry and not self.overwrite:
                self.display_message(
                    'Preparing Transform',
                    'Masked transform group already exists in %s' %
                    self.entry.nxname)
                return
            output_file = self.get_output_file()
            settings_file = self.get_settings_file()
            self.write_parameters(output_file, settings_file)
            self.refine.prepare_transform(output_file)
            if self.mask:
                masked_output_file = self.get_output_file(mask=True)
                self.refine.prepare_transform(masked_output_file, mask=True)
            self.refine.write_settings(settings_file)
            if self.copy:
                root = self.entry.nxroot
                for entry in [
                        e for e in root
                        if e != 'entry' and e != self.entry.nxname
                ]:
                    if 'transform' in root[entry] and not self.overwrite:
                        self.display_message(
                            'Preparing Transform',
                            'Transform group already exists in %s' % entry)
                        return
                    if self.mask and 'masked_transform' in root[
                            entry] and not self.overwrite:
                        self.display_message(
                            'Preparing Transform',
                            'Masked transform group already exists in %s' %
                            entry)
                        return
                    self.refine = NXRefine(root[entry])
                    output = output_file.replace(self.entry.nxname, entry)
                    settings = settings_file.replace(self.entry.nxname, entry)
                    self.write_parameters(output, settings)
                    self.refine.prepare_transform(output)
                    if self.mask:
                        output = masked_output_file.replace(
                            self.entry.nxname, entry)
                        self.refine.prepare_transform(output, mask=True)
                    self.refine.write_settings(settings)

            super(TransformDialog, self).accept()
        except NeXusError as error:
            report_error("Preparing Data Transform", error)