Esempio n. 1
0
 def terminate_thread(self):
     raise IOTATermination('IOTA_TRACKER: Termination signal received!')
Esempio n. 2
0
    def run(self, idx, img):
        if os.path.isfile(self.term_file):
            raise IOTATermination('IOTA_TRACKER: Termination signal received!')
        else:
            # First, parse filepath to create Mosflm template
            directory = os.path.dirname(img)
            filepath = os.path.basename(img).split('.')
            fname = filepath[0]
            extension = filepath[1]
            if '_' in fname:
                suffix = fname.split('_')[-1]
            elif '-' in fname:
                suffix = fname.split('-')[-1]
            elif '.' in fname:
                suffix = fname.split('.')[-1]
            else:
                suffix = fname
            img_number = int(''.join(n if n.isdigit() else '' for n in suffix))
            prefix = fname.replace(suffix, '')
            n_suffix = ''.join("#" if c.isdigit() else c for c in suffix)
            template = '{}{}.{}'.format(prefix, n_suffix, extension)

            # Create autoindex.com w/ Mosflm script
            # Write to temporary file and change permissions to run
            autoindex = [
                '#! /bin/tcsh -fe', 'ipmosflm << eof-ipmosflm'.format(fname),
                'NEWMATRIX {0}.mat'.format(fname),
                'DIRECTORY {}'.format(directory),
                'TEMPLATE {}'.format(template),
                'AUTOINDEX DPS THRESH 0.1 IMAGE {} PHI 0 0.01'.format(
                    img_number), 'GO', 'eof-ipmosflm'
            ]
            autoindex_string = '\n'.join(autoindex)
            autoindex_filename = 'autoindex_{}.com'.format(idx)

            with open(autoindex_filename, 'w') as af:
                af.write(autoindex_string)
            os.chmod(autoindex_filename, 0o755)

            # Run Mosflm autoindexing
            command = './{}'.format(autoindex_filename)
            out = easy_run.fully_buffered(command, join_stdout_stderr=True)

            # Scrub text output
            final_spots = [
                l for l in out.stdout_lines if 'spots written for image' in l
            ]
            final_cell_line = [
                l for l in out.stdout_lines if 'Final cell' in l
            ]
            final_sg_line = [l for l in out.stdout_lines if 'space group' in l]

            if final_spots:
                spots = final_spots[0].rsplit()[0]
            else:
                spots = 0
            if final_cell_line:
                cell = final_cell_line[0].replace(
                    'Final cell (after refinement) is', '').rsplit()
            else:
                cell = None
            if final_sg_line:
                sg = final_sg_line[0].rsplit()[6]
            else:
                sg = None

            # Temp file cleanup
            try:
                os.remove('{}.mat'.format(fname))
            except Exception:
                pass
            try:
                os.remove('{}.spt'.format(prefix[:-1]))
            except Exception:
                pass
            try:
                os.remove('SUMMARY')
            except Exception:
                pass
            try:
                os.remove(autoindex_filename)
            except Exception:
                pass

            return [idx, spots, img, sg, cell]
    def process_image(self):
        if os.path.isfile(self.termfile):
            raise IOTATermination('IOTA_TRACKER: Termination signal received!')
        else:
            with Capturing() as junk_output:
                # if True:
                err = []
                start = time.time()
                fail = False
                sg = None
                uc = None
                status = None
                score = 0
                try:
                    datablock = DataBlockFactory.from_filenames([self.img])[0]
                    observed = self.processor.find_spots(datablock=datablock)
                    status = 'spots found'
                except Exception, e:
                    fail = True
                    observed = []
                    err.append('SPOTFINDING ERROR: {}'.format(e))
                    pass

                # TODO: Indexing / lattice determination very slow (how to speed up?)
                if self.run_indexing:
                    if not fail:
                        try:
                            experiments, indexed = self.processor.index(
                                datablock=datablock, reflections=observed)
                            score = len(indexed)
                        except Exception, e:
                            fail = True
                            err.append('INDEXING ERROR: {}'.format(e))
                            pass

                    if not fail:
                        try:
                            solution = self.processor.refine_bravais_settings(
                                reflections=indexed, experiments=experiments)

                            # Only reindex if higher-symmetry solution found
                            if solution is not None:
                                experiments, indexed = self.processor.reindex(
                                    reflections=indexed,
                                    experiments=experiments,
                                    solution=solution)
                            obs = experiments
                            lat = experiments[0].crystal.get_space_group(
                            ).info()
                            sg = str(lat).replace(' ', '')
                            status = 'indexed'
                        except Exception, e:
                            fail = True
                            err.append('LATTICE ERROR: {}'.format(e))
                            pass

                    if not fail:
                        unit_cell = experiments[0].crystal.get_unit_cell(
                        ).parameters()
                        uc = ' '.join(['{:.4f}'.format(i) for i in unit_cell])

                    if self.run_integration:
                        if not fail:
                            try:
                                # Run refinement
                                experiments, indexed = self.processor.refine(
                                    experiments=experiments, centroids=indexed)
                            except Exception, e:
                                fail = True
                                err.append('REFINEMENT ERROR: {}'.format(e))
                                pass

                        if not fail:
                            try:
                                print(experiments)
                                print(indexed)
                                integrated = self.processor.integrate(
                                    experiments=experiments, indexed=indexed)
                                frame = ConstructFrame(
                                    integrated, experiments[0]).make_frame()
                                status = 'integrated'
                            except Exception, e:
                                err.append('INTEGRATION ERROR: {}'.format(e))
                                pass
Esempio n. 4
0
    def run(self, idx, img):
        if self.meta_parent.terminated:
            raise IOTATermination(
                'IOTA_TRACKER: SPF Termination signal received!')
        else:
            with Capturing() as junk_output:
                fail = False
                sg = None
                uc = None
                try:
                    experiments = ExperimentListFactory.from_filenames([img
                                                                        ])[0]
                    observed = self.processor.find_spots(
                        experiments=experiments)
                except Exception:
                    fail = True
                    observed = []
                    pass

                # TODO: Indexing / lattice determination very slow (how to speed up?)
                if self.run_indexing:
                    if not fail:
                        try:
                            experiments, indexed = self.processor.index(
                                experiments=experiments, reflections=observed)
                        except Exception:
                            fail = True
                            pass

                    if not fail:
                        try:
                            solution = self.processor.refine_bravais_settings(
                                reflections=indexed, experiments=experiments)

                            # Only reindex if higher-symmetry solution found
                            if solution is not None:
                                experiments, indexed = self.processor.reindex(
                                    reflections=indexed,
                                    experiments=experiments,
                                    solution=solution)
                            lat = experiments[0].crystal.get_space_group(
                            ).info()
                            sg = str(lat).replace(' ', '')
                        except Exception:
                            fail = True
                            pass

                    if not fail:
                        unit_cell = experiments[0].crystal.get_unit_cell(
                        ).parameters()
                        uc = ' '.join(['{:.4f}'.format(i) for i in unit_cell])

                    if self.run_integration:
                        if not fail:
                            try:
                                # Run refinement
                                experiments, indexed = self.processor.refine(
                                    experiments=experiments, centroids=indexed)
                            except Exception:
                                fail = True
                                pass

                        if not fail:
                            try:
                                print(experiments)
                                print(indexed)
                                integrated = self.processor.integrate(
                                    experiments=experiments, indexed=indexed)
                            except Exception:
                                pass

            return [idx, int(len(observed)), img, sg, uc]
Esempio n. 5
0
    def run(self, idx, img):
        if os.path.isfile(self.term_file):
            raise IOTATermination("IOTA_TRACKER: Termination signal received!")
        else:
            # First, parse filepath to create Mosflm template
            directory = os.path.dirname(img)
            filepath = os.path.basename(img).split(".")
            fname = filepath[0]
            extension = filepath[1]
            if "_" in fname:
                suffix = fname.split("_")[-1]
            elif "-" in fname:
                suffix = fname.split("-")[-1]
            elif "." in fname:
                suffix = fname.split(".")[-1]
            else:
                suffix = fname
            img_number = int("".join(n if n.isdigit() else "" for n in suffix))
            prefix = fname.replace(suffix, "")
            n_suffix = "".join("#" if c.isdigit() else c for c in suffix)
            template = "{}{}.{}".format(prefix, n_suffix, extension)

            # Create autoindex.com w/ Mosflm script
            # Write to temporary file and change permissions to run
            autoindex = [
                "#! /bin/tcsh -fe",
                "ipmosflm << eof-ipmosflm".format(fname),
                "NEWMATRIX {0}.mat".format(fname),
                "DIRECTORY {}".format(directory),
                "TEMPLATE {}".format(template),
                "AUTOINDEX DPS THRESH 0.1 IMAGE {} PHI 0 0.01".format(
                    img_number),
                "GO",
                "eof-ipmosflm",
            ]
            autoindex_string = "\n".join(autoindex)
            autoindex_filename = "autoindex_{}.com".format(idx)

            with open(autoindex_filename, "w") as af:
                af.write(autoindex_string)
            os.chmod(autoindex_filename, 0o755)

            # Run Mosflm autoindexing
            command = "./{}".format(autoindex_filename)
            out = easy_run.fully_buffered(command, join_stdout_stderr=True)

            # Scrub text output
            final_spots = [
                l for l in out.stdout_lines if "spots written for image" in l
            ]
            final_cell_line = [
                l for l in out.stdout_lines if "Final cell" in l
            ]
            final_sg_line = [l for l in out.stdout_lines if "space group" in l]

            if final_spots:
                spots = final_spots[0].rsplit()[0]
            else:
                spots = 0
            if final_cell_line:
                cell = (final_cell_line[0].replace(
                    "Final cell (after refinement) is", "").rsplit())
            else:
                cell = None
            if final_sg_line:
                sg = final_sg_line[0].rsplit()[6]
            else:
                sg = None

            # Temp file cleanup
            try:
                os.remove("{}.mat".format(fname))
            except Exception:
                pass
            try:
                os.remove("{}.spt".format(prefix[:-1]))
            except Exception:
                pass
            try:
                os.remove("SUMMARY")
            except Exception:
                pass
            try:
                os.remove(autoindex_filename)
            except Exception:
                pass

            return [idx, spots, img, sg, cell]