Пример #1
0
    def save_metadata(self,
                      starfile_filepath,
                      new_mrcs=True,
                      batch_size=512,
                      save_mode=None):
        """
        Save updated metadata to a STAR file

        :param starfile_filepath: Path to STAR file where we want to
            save image_source
        :param new_mrcs: Whether to save all images to new MRCS files or not.
            If True, new file names and pathes need to be created.
        :param batch_size: Batch size of images to query from the
            `ImageSource` object. Every `batch_size` rows, entries are
            written to STAR file.
        :param save_mode: Whether to save all images in a single or
            multiple files in batch size.
        :return: None
        """

        df = self._metadata.copy()
        # Drop any column that doesn't start with a *single* underscore
        df = df.drop(
            [
                str(col) for col in df.columns
                if not col.startswith("_") or col.startswith("__")
            ],
            axis=1,
        )

        with open(starfile_filepath, "w") as f:
            if new_mrcs:
                # Create a new column that we will be populating in the loop below
                # For
                df["_rlnImageName"] = ""

                if save_mode == "single":
                    # Save all images into one single mrc file
                    fname = os.path.basename(starfile_filepath)
                    fstem = os.path.splitext(fname)[0]
                    mrcs_filename = f"{fstem}_{0}_{self.n-1}.mrcs"

                    # Then set name in dataframe for the StarFile
                    # Note, here the row_indexer is :, representing all rows in this data frame.
                    #   df.loc will be reponsible for dereferencing and assigning values to df.
                    #   Pandas will assert df.shape[0] == self.n
                    df.loc[:, "_rlnImageName"] = [
                        f"{j + 1:06}@{mrcs_filename}" for j in range(self.n)
                    ]
                else:
                    # save all images into multiple mrc files in batch size
                    for i_start in np.arange(0, self.n, batch_size):
                        i_end = min(self.n, i_start + batch_size)
                        num = i_end - i_start
                        mrcs_filename = (os.path.splitext(
                            os.path.basename(starfile_filepath))[0] +
                                         f"_{i_start}_{i_end-1}.mrcs")

                        # Note, here the row_indexer is a slice.
                        #   df.loc will be reponsible for dereferencing and assigning values to df.
                        #   Pandas will assert the lnegth of row_indexer equals num.
                        row_indexer = df[i_start:i_end].index
                        df.loc[row_indexer, "_rlnImageName"] = [
                            "{0:06}@{1}".format(j + 1, mrcs_filename)
                            for j in range(num)
                        ]

            filename_indices = df._rlnImageName.str.split(
                pat="@", expand=True)[1].tolist()

            # initial the star file object and save it
            starfile = StarFile(blocks=[StarFileBlock(loops=[df])])
            starfile.save(f)

        return filename_indices
Пример #2
0
class StarFileTestCase(TestCase):
    def setUp(self):
        with importlib_resources.path(tests.saved_test_data,
                                      "sample.star") as path:
            self.starfile = StarFile(path)

        # Independent Image object for testing Image source methods
        L = 768
        self.im = Image(misc.face(gray=True).astype("float64")[:L, :L])
        self.img_src = ArrayImageSource(self.im)

        # We also want to flex the stack logic.
        self.n = 21
        im_stack = np.broadcast_to(self.im.data, (self.n, L, L))
        # make each image methodically different
        im_stack = np.multiply(im_stack, np.arange(self.n)[:, None, None])
        self.im_stack = Image(im_stack)
        self.img_src_stack = ArrayImageSource(self.im_stack)

        # Create a tmpdir object for this test instance
        self._tmpdir = tempfile.TemporaryDirectory()
        # Get the directory from the name attribute of the instance
        self.tmpdir = self._tmpdir.name

    def tearDown(self):
        # Destroy the tmpdir instance and contents
        self._tmpdir.cleanup()

    def testLength(self):
        # StarFile is an iterable that gives us blocks.
        #   We have 2 blocks in our sample starfile.
        self.assertEqual(2, len(self.starfile))

    def testIteration(self):
        # A StarFile can be iterated over, yielding StarFileBlocks
        for block in self.starfile:
            self.assertTrue(isinstance(block, StarFileBlock))

    def testBlockByIndex(self):
        # Indexing a StarFile with a 0-based index gives us a 'block',
        block0 = self.starfile[0]
        self.assertTrue(isinstance(block0, StarFileBlock))
        # Our first block has no 'loop's.
        self.assertEqual(0, len(block0))

    def testBlockByName(self):
        # Indexing a StarFile with a string gives us a block with that name
        #   ("data_<name>" in starfile).
        # In our case the block at index 1 has name 'planetary'
        block1 = self.starfile["planetary"]
        # This block has a two 'loops'.
        self.assertEqual(2, len(block1))

    def testBlockProperties(self):
        # A StarFileBlock may have attributes that were read from the
        #   starfile key=>value pairs.
        block0 = self.starfile["general"]
        # Note that no typecasting is performed
        self.assertEqual(block0._three, "3")

    def testLoop(self):
        loop = self.starfile[1][0]
        self.assertIsInstance(loop, DataFrame)

    def testData1(self):
        df = self.starfile["planetary"][0]
        self.assertEqual(8, len(df))
        self.assertEqual(4, len(df.columns))
        # Note that no typecasting of values is performed at io.StarFile level
        self.assertEqual("1", df[df["_name"] == "Earth"].iloc[0]["_gravity"])

    def testData2(self):
        df = self.starfile["planetary"][1]
        self.assertEqual(3, len(df))
        self.assertEqual(2, len(df.columns))
        # Missing values in a loop default to ''
        self.assertEqual(
            "", df[df["_name"] == "Earth"].iloc[0]["_discovered_year"])

    def testSave(self):
        # Save the StarFile object to disk,
        #   read it back, and check for equality.
        # Note that __eq__ is supported for StarFile/StarFileBlock classes

        with open("sample_saved.star", "w") as f:
            self.starfile.save(f)
        self.starfile2 = StarFile("sample_saved.star")
        self.assertEqual(self.starfile, self.starfile2)

        os.remove("sample_saved.star")
Пример #3
0
    def save_metadata(
        self, starfile_filepath, new_mrcs=True, batch_size=512, save_mode=None
    ):
        """
        Save updated metadata to a STAR file

        :param starfile_filepath: Path to STAR file where we want to
            save image_source
        :param new_mrcs: Whether to save all images to new MRCS files or not.
            If True, new file names and pathes need to be created.
        :param batch_size: Batch size of images to query from the
            `ImageSource` object. Every `batch_size` rows, entries are
            written to STAR file.
        :param save_mode: Whether to save all images in a single or
            multiple files in batch size.
        :return: None
        """

        df = self._metadata.copy()
        # Drop any column that doesn't start with a *single* underscore
        df = df.drop(
            [
                str(col)
                for col in df.columns
                if not col.startswith("_") or col.startswith("__")
            ],
            axis=1,
        )

        filename_indices = None

        with open(starfile_filepath, "w") as f:
            if new_mrcs:
                # Create a new column that we will be populating in the loop below
                # For
                df["_rlnImageName"] = ""

                if save_mode == "single":
                    # Save all images into one single mrc file
                    fname = os.path.basename(starfile_filepath)
                    fstem = os.path.splitext(fname)[0]
                    mrcs_filename = f"{fstem}_{0}_{self.n-1}.mrcs"

                    # Then set name in dataframe for the StarFile
                    df["_rlnImageName"][0 : self.n] = pd.Series(
                        [f"{j + 1:06}@{mrcs_filename}" for j in range(self.n)]
                    )

                else:
                    # save all images into multiple mrc files in batch size
                    for i_start in np.arange(0, self.n, batch_size):
                        i_end = min(self.n, i_start + batch_size)
                        num = i_end - i_start
                        mrcs_filename = (
                            os.path.splitext(os.path.basename(starfile_filepath))[0]
                            + f"_{i_start}_{i_end-1}.mrcs"
                        )

                        df["_rlnImageName"][i_start:i_end] = pd.Series(
                            [
                                "{0:06}@{1}".format(j + 1, mrcs_filename)
                                for j in range(num)
                            ]
                        )

            filename_indices = [
                df["_rlnImageName"][i].split("@")[1] for i in range(self.n)
            ]

            # initial the star file object and save it
            starfile = StarFile(blocks=[StarFileBlock(loops=[df])])
            starfile.save(f)

        return filename_indices