Example #1
0
 def setUp(self):
     self.sim = Simulation(n=1024,
                           filters=SourceFilter([
                               RadialCTFFilter(defocus=d)
                               for d in np.linspace(1.5e4, 2.5e4, 7)
                           ],
                                                n=1024))
 def setUp(self):
     sim = Simulation(n=1024,
                      filters=SourceFilter([
                          RadialCTFFilter(defocus=d)
                          for d in np.linspace(1.5e4, 2.5e4, 7)
                      ],
                                           n=1024))
     basis = FBBasis3D((8, 8, 8))
     self.estimator = MeanEstimator(sim, basis, preconditioner='none')
     self.estimator_with_preconditioner = MeanEstimator(
         sim, basis, preconditioner='circulant')
    def testRadialCTFSourceFilter(self):
        source_filter = SourceFilter(
            [RadialCTFFilter(defocus=d) for d in np.linspace(1.5e4, 2.5e4, 7)],
            n=42
        )
        result = source_filter.evaluate_grid(8)

        self.assertEqual(result.shape, (8, 8, 7))
        # Just check the value of the last filter for now
        self.assertTrue(np.allclose(
            result[:, :, -1],
            np.array([
                [ 0.461755701877834,  -0.995184514498978,   0.063120922443392,   0.833250206225063,   0.961464660252150,   0.833250206225063,   0.063120922443392,  -0.995184514498978],
                [-0.995184514498978,   0.626977423649552,   0.799934516166400,   0.004814348317439,  -0.298096205735759,   0.004814348317439,   0.799934516166400,   0.626977423649552],
                [ 0.063120922443392,   0.799934516166400,  -0.573061561512667,  -0.999286510416273,  -0.963805291282899,  -0.999286510416273,  -0.573061561512667,   0.799934516166400],
                [ 0.833250206225063,   0.004814348317439,  -0.999286510416273,  -0.633095739808868,  -0.368890743119366,  -0.633095739808868,  -0.999286510416273,   0.004814348317439],
                [ 0.961464660252150,  -0.298096205735759,  -0.963805291282899,  -0.368890743119366,  -0.070000000000000,  -0.368890743119366,  -0.963805291282899,  -0.298096205735759],
                [ 0.833250206225063,   0.004814348317439,  -0.999286510416273,  -0.633095739808868,  -0.368890743119366,  -0.633095739808868,  -0.999286510416273,   0.004814348317439],
                [ 0.063120922443392,   0.799934516166400,  -0.573061561512667,  -0.999286510416273,  -0.963805291282899,  -0.999286510416273,  -0.573061561512667,   0.799934516166400],
                [-0.995184514498978,   0.626977423649552,   0.799934516166400,   0.004814348317439,  -0.298096205735759,   0.004814348317439,   0.799934516166400,   0.626977423649552]
            ])
        ))
Example #4
0
    def setUpClass(cls):
        cls.sim = Simulation(n=1024,
                             filters=SourceFilter([
                                 RadialCTFFilter(defocus=d)
                                 for d in np.linspace(1.5e4, 2.5e4, 7)
                             ],
                                                  n=1024))
        basis = FBBasis3D((8, 8, 8))
        cls.noise_variance = 0.0030762743633643615

        cls.mean_estimator = MeanEstimator(cls.sim, basis)
        cls.mean_est = np.load(os.path.join(DATA_DIR, 'mean_8_8_8.npy'))

        # Passing in a mean_kernel argument to the following constructor speeds up some calculations
        cls.covar_estimator = CovarianceEstimator(
            cls.sim,
            basis,
            mean_kernel=cls.mean_estimator.kernel,
            preconditioner='none')
        cls.covar_estimator_with_preconditioner = CovarianceEstimator(
            cls.sim,
            basis,
            mean_kernel=cls.mean_estimator.kernel,
            preconditioner='circulant')
Example #5
0
    parser.add_argument('--num_volumes', default=2, type=int)
    parser.add_argument('--image_size', default=8, type=int)
    parser.add_argument('--num_images', default=1024, type=int)
    parser.add_argument('--num_eigs', default=16, type=int)

    with parser.parse_args() as args:

        C = args.num_volumes
        L = args.image_size
        n = args.num_images

        sim = Simulation(
            n=n,
            C=C,
            filters=SourceFilter(
                [RadialCTFFilter(defocus=d) for d in np.linspace(1.5e4, 2.5e4, 7)],
                n=n
            )
        )
        basis = FBBasis3D((L, L, L))

        noise_estimator = WhiteNoiseEstimator(sim, batchSize=500)
        # Estimate the noise variance. This is needed for the covariance estimation step below.
        noise_variance = noise_estimator.estimate()
        print(f'Noise Variance = {noise_variance}')

        """
        Estimate the mean. This uses conjugate gradient on the normal equations for the least-squares estimator of the mean
        volume. The mean volume is represented internally using the basis object, but the output is in the form of an
        L-by-L-by-L array.
        """
        mean_estimator = MeanEstimator(sim, basis)
Example #6
0
    def __init__(self,
                 filepath,
                 pixel_size=1,
                 B=0,
                 ignore_missing_files=False,
                 max_rows=None):
        """
        Load starfile at given filepath
        :param filepath: Absolute or relative path to .star file
        :param pixel_size: the pixel size of the images in angstroms (Default 1)
        :param B: the envelope decay of the CTF in inverse square angstrom (Default 0)
        :param ignore_missing_files: Whether to ignore missing MRC files or not (Default False)
        :param max_rows: Maximum no. of rows in .star file to read. If None (default), all rows are read.
            Note that this refers to the max no. of images to load, not the max. number of .mrcs files (which may be
            equal to or less than the no. of images).
            If ignore_missing_files is False, the first max_rows rows read from the .star file are considered.
            If ignore_missing_files is True, then the first max_rows *available* rows from the .star file are
            considered.
        """
        logger.debug(f'Loading starfile at path {filepath}')

        self.df = Starfile.star2df(filepath)

        # Handle missing files
        missing = self.df['_mrc_found'] == False
        n_missing = sum(missing)
        if ignore_missing_files:
            if n_missing > 0:
                logger.info(
                    f'Dropping {n_missing} rows with missing mrc files')
                self.df = self.df[~missing]
                self.df.reset_index(inplace=True)
        else:
            ensure(n_missing == 0, f'{n_missing} mrc files missing')

        if max_rows is not None:
            self.df = self.df.iloc[:max_rows]

        n = len(self.df)

        self.pixel_size = pixel_size
        self.B = B

        # Peek into the first image and populate some attributes
        first_mrc_filepath = self.df.iloc[0]._mrc_filepath
        mrc = mrcfile.open(first_mrc_filepath)

        # Get the 'mode' (data type) - TODO: There's probably a more direct way to do this.
        mode = int(mrc.header.mode)
        dtypes = {0: 'int8', 1: 'int16', 2: 'float32', 6: 'uint16'}
        ensure(
            mode in dtypes,
            f'Only modes={list(dtypes.keys())} in MRC files are supported for now.'
        )
        dtype = dtypes[mode]

        shape = mrc.data.shape
        ensure(shape[1] == shape[2], "Only square images are supported")
        L = shape[1]

        # Save original image resolution
        self._L = L
        logger.debug(f'Image size = {L}x{L}')

        rots = angles_to_rots(self.df[[
            '_rlnAngleRot_radians', '_rlnAngleTilt_radians',
            '_rlnAnglePsi_radians'
        ]].values.T)

        filter_params, filter_indices = np.unique(self.df[[
            'rlnVoltage', 'rlnDefocusU', 'rlnDefocusV',
            '_rlnDefocusAngle_radians', 'rlnSphericalAberration',
            'rlnAmplitudeContrast'
        ]].values,
                                                  return_inverse=True,
                                                  axis=0)

        filters = []
        for row in filter_params:
            filters.append(
                CTFFilter(pixel_size=self.pixel_size,
                          voltage=row[0],
                          defocus_u=row[1],
                          defocus_v=row[2],
                          defocus_ang=row[3],
                          Cs=row[4],
                          alpha=row[5],
                          B=self.B))
        filters = SourceFilter(filters, indices=filter_indices)

        offsets = self.df[['rlnOriginX', 'rlnOriginY']].values.T
        amplitudes = np.ones(n)
        states = self.df['rlnClassNumber'].values

        super().__init__(L=L,
                         n=n,
                         states=states,
                         filters=filters,
                         offsets=offsets,
                         amplitudes=amplitudes,
                         rots=rots,
                         dtype=dtype)