def setAngleFilename_fromLineEdit(self):
     filename = self.ui.lineEdit_angleFile.text()
     if os.path.isfile(toString(filename)):
         self.calculationParameters.angleFilename = filename
         self.calculationParameters.angleFileProvided = True
         self.ui.lineEdit_angleFile.setText(toQString(filename))
         self.disableAngleWidgets()
 def loadModel(self, filename):
     self.calculationParameters.oversamplingRatio, ok = QtWidgets.QInputDialog.getInt(
         self,
         "Select Oversampling Ratio",
         "Oversampling Ratio = ",
         value=3,
         min=1,
         max=10,
         step=1)
     from threading import Thread
     t = Thread(target=lambda: print("Loading Model..."))
     t.start()
     self.ui.btn_go.setEnabled(True)
     self.calculationParameters.model = genfire.fileio.readVolume(
         toString(filename))
     self.calculationParameters.dims = np.shape(
         self.calculationParameters.model)
     self.calculationParameters.paddedDim = self.calculationParameters.dims[
         0] * self.calculationParameters.oversamplingRatio
     padding = int((self.calculationParameters.paddedDim -
                    self.calculationParameters.dims[0]) / 2)
     self.calculationParameters.model = np.pad(
         self.calculationParameters.model,
         ((padding, padding), (padding, padding), (padding, padding)),
         'constant')
     self.calculationParameters.model = fftn_fftshift(
         self.calculationParameters.model)
     self.calculationParameters.ncOut = int(
         np.shape(self.calculationParameters.model)[0] // 2)
     self.calculationParameters.interpolator = genfire.utility.getProjectionInterpolator(
         self.calculationParameters.model)
     t.join()
Esempio n. 3
0
 def launchVolumeSlicer(self):
     import genfire.fileio
     filename, _ = QtWidgets.QFileDialog.getOpenFileName(
         QtWidgets.QFileDialog(),
         "Select Volume",
         filter="Volume files (*.mat *.mrc *.npy);;All Files (*)")
     if filename:
         filename = toString(filename)
         volume = genfire.fileio.readVolume(filename)
         self.VolumeSlicer = VolumeSlicer.VolumeSlicer(volume)
         self.VolumeSlicer.show()
 def selectModelFile(self):
     filename, _ = QtWidgets.QFileDialog.getOpenFileName(
         QtWidgets.QFileDialog(),
         "Select File Containing Model",
         filter=
         "Volume files (*.mrc *.mat *.npy);;MATLAB files (*.mat);;TIFF images (*.tif *.tiff);;MRC (*.mrc);;numpy (*.npy);;All Files (*)"
     )
     print(filename)
     if os.path.isfile(toString(filename)):
         self.ui.btn_go.setEnabled(True)
         self.setModelFilename(filename)
Esempio n. 5
0
    def updateFilenames(self):
        import os
        base, file = os.path.split(
            toString(self.GENFIRE_ProjectionCalculator.calculationParameters.
                     outputFilename))

        self.ui.lineEdit_angle.setText(
            toQString(
                str(self.GENFIRE_ProjectionCalculator.calculationParameters.
                    outputAngleFilename)))
        self.ui.lineEdit_angle.textChanged.emit(self.ui.lineEdit_angle.text())
        self.ui.lineEdit_pj.setText(
            toQString(
                str(self.GENFIRE_ProjectionCalculator.calculationParameters.
                    outputFilename)))
        self.ui.lineEdit_pj.textChanged.emit(self.ui.lineEdit_pj.text())
        self.ui.lineEdit_results.setText(toQString(str(base + "/results.mrc")))
        self.ui.lineEdit_results.textChanged.emit(
            self.ui.lineEdit_results.text())
Esempio n. 6
0
    def displayResults(self):
        outputfilename, _ = QtWidgets.QFileDialog.getOpenFileName(
            QtWidgets.QFileDialog(),
            "Select Reconstruction",
            filter=
            "Volume files (*.mrc *.mat *.npy)  ;; MATLAB files (*.mat);;text files (*.txt *.tiff);;MRC (*.mrc);;All Files (*)"
        )
        outputfilename = toString(outputfilename)
        if outputfilename:
            import numpy as np
            import os
            import genfire.fileio
            import matplotlib.pyplot as plt
            plt.close('all')

            initialObject = genfire.fileio.readVolume(outputfilename)
            dims = np.shape(initialObject)
            n_half_x = int(dims[0] / 2)  #this assumes even-sized arrays
            n_half_y = int(dims[1] / 2)
            n_half_z = int(dims[2] / 2)
            reconstructionDisplayWindowSize = dims[0]  # array should be cubic
            half_window_x = reconstructionDisplayWindowSize // 2
            half_window_y = reconstructionDisplayWindowSize // 2
            half_window_z = reconstructionDisplayWindowSize // 2
            plt.figure()
            plt.subplot(233)
            plt.imshow(
                np.squeeze(initialObject[n_half_x,
                                         n_half_y - half_window_y:n_half_y +
                                         half_window_y,
                                         n_half_z - half_window_z:n_half_z +
                                         half_window_z]))
            plt.title("central YZ slice")

            plt.subplot(232)
            plt.imshow(
                np.squeeze(initialObject[n_half_x - half_window_x:n_half_x +
                                         half_window_x, n_half_y,
                                         n_half_z - half_window_z:n_half_z +
                                         half_window_z]))
            plt.title("central XZ slice")

            plt.subplot(231)
            plt.title("central XY slice")
            plt.imshow(
                np.squeeze(initialObject[n_half_x - half_window_x:n_half_x +
                                         half_window_x,
                                         n_half_y - half_window_y:n_half_y +
                                         half_window_y, n_half_z]))

            plt.subplot(236)
            plt.title("YZ projection")
            plt.imshow(
                np.squeeze(
                    np.sum(initialObject[n_half_x - half_window_x:n_half_x +
                                         half_window_x,
                                         n_half_y - half_window_y:n_half_y +
                                         half_window_y,
                                         n_half_z - half_window_z:n_half_z +
                                         half_window_z],
                           axis=0)))

            plt.subplot(235)
            plt.title("XZ projection")
            plt.imshow(
                np.squeeze(
                    np.sum(initialObject[n_half_x - half_window_x:n_half_x +
                                         half_window_x,
                                         n_half_y - half_window_y:n_half_y +
                                         half_window_y,
                                         n_half_z - half_window_z:n_half_z +
                                         half_window_z],
                           axis=1)))

            plt.subplot(234)
            plt.title("XY projection")
            plt.imshow(
                np.squeeze(
                    np.sum(initialObject[n_half_x - half_window_x:n_half_x +
                                         half_window_x,
                                         n_half_y - half_window_y:n_half_y +
                                         half_window_y,
                                         n_half_z - half_window_z:n_half_z +
                                         half_window_z],
                           axis=2)))
            plt.get_current_fig_manager().window.setGeometry(25, 25, 600, 1200)

            # now load error curves if they exist
            output_base, output_ext = os.path.splitext(outputfilename)
            errK_filename = output_base + '_errK.txt'
            if os.path.isfile(errK_filename):
                errK = np.loadtxt(errK_filename)
                numIterations = np.shape(errK)[0]
                plt.figure()
                plt.plot(range(0, numIterations), errK)

                mngr = plt.get_current_fig_manager()
                mngr.window.setGeometry(700, 25, 550, 250)
                plt.title("Reciprocal Error vs Iteration Number")
                plt.xlabel("Iteration Num")
                plt.ylabel("Reciprocal Error")
                # plt.setp(plt.gcf(),)
            Rfree_total_filename = output_base + '_Rfree_total.txt'
            Rfree_bybin_filename = output_base + '_Rfree_bybin.txt'
            if os.path.isfile(Rfree_total_filename):
                Rfree_total = np.loadtxt(Rfree_total_filename)
                numIterations = np.shape(Rfree_total)[0]
                plt.figure()
                plt.title("R-free vs Iteration Number")
                mngr = plt.get_current_fig_manager()
                mngr.window.setGeometry(700, 350, 550, 250)
                plt.plot(range(0, numIterations), Rfree_total)
                plt.title("Mean R-free Value vs Iteration Number")
                plt.xlabel("Iteration Num")
                plt.ylabel('Mean R-free')

            if os.path.isfile(Rfree_bybin_filename):
                Rfree_bybin = np.loadtxt(Rfree_bybin_filename)
                plt.figure()
                mngr = plt.get_current_fig_manager()
                mngr.window.setGeometry(700, 650, 550, 250)
                X = np.linspace(0, 1, np.shape(Rfree_bybin)[0])
                plt.plot(X, Rfree_bybin[:, -1])
                plt.title("Final Rfree Value vs Spatial Frequency")
                plt.xlabel("Spatial Frequency (% of Nyquist)")
                plt.ylabel('Rfree')

            plt.draw()
            plt.pause(1e-30)  # slight pause forces redraw
 def setModelFilename_fromLineEdit(self):
     filename = self.ui.lineEdit_modelFile.text()
     if os.path.isfile(
             toString(filename)
     ) and filename != self.calculationParameters.modelFilename:
         self.setModelFilename(toString(filename))
    def calculateProjections(self):
        if self.calculateProjections_Dialog.status:
            self.calculationParameters.modelFilename = toString(
                self.calculationParameters.modelFilename)
            if not self.calculationParameters.modelLoadedFlag:

                self.calculationParameters.model = genfire.fileio.readVolume(
                    self.calculationParameters.modelFilename)
                self.calculationParameters.oversamplingRatio = self.calculationParameters.oversamplingRatio
                self.calculationParameters.dims = np.shape(
                    self.calculationParameters.model)
                paddedDim = self.calculationParameters.dims[
                    0] * self.calculationParameters.oversamplingRatio
                padding = int(
                    (paddedDim - self.calculationParameters.dims[0]) / 2)
                self.calculationParameters.model = np.pad(
                    self.calculationParameters.model,
                    ((padding, padding), (padding, padding),
                     (padding, padding)), 'constant')
                self.calculationParameters.model = fftn_fftshift(
                    self.calculationParameters.model)
                self.calculationParameters.interpolator = genfire.utility.getProjectionInterpolator(
                    self.calculationParameters.model)
            self.calculationParameters.ncOut = np.shape(
                self.calculationParameters.model)[0] // 2

            if self.calculationParameters.angleFileProvided:
                self.calculationParameters.angleFilename = toString(
                    self.calculationParameters.angleFilename)
                self.calculationParameters.outputFilename = toString(
                    self.calculationParameters.outputFilename)
                angles = np.loadtxt(
                    toString(self.calculationParameters.angleFilename))
                phi = angles[:, 0]
                theta = angles[:, 1]
                psi = angles[:, 2]
                filename = self.calculationParameters.outputFilename + '.npy'
                projections = np.zeros(
                    (self.calculationParameters.dims[0],
                     self.calculationParameters.dims[1], np.size(phi)),
                    dtype=float)
                if self.calculationParameters.interpolator is None:
                    self.calculationParameters.interpolator = genfire.utility.getProjectionInterpolator(
                        self.calculationParameters.model)
                for projNum in range(0, np.size(phi)):
                    pj = genfire.utility.calculateProjection_interp_fromInterpolator(
                        self.calculationParameters.interpolator, phi[projNum],
                        theta[projNum], psi[projNum],
                        np.shape(self.calculationParameters.model))
                    projections[:, :, projNum] = pj[
                        self.calculationParameters.ncOut -
                        int(self.calculationParameters.dims[0] /
                            2):self.calculationParameters.ncOut +
                        int(self.calculationParameters.dims[0] / 2),
                        self.calculationParameters.ncOut -
                        int(self.calculationParameters.dims[1] /
                            2):self.calculationParameters.ncOut +
                        int(self.calculationParameters.dims[1] / 2)]

                genfire.fileio.writeVolume(filename, projections)
            else:
                if self.calculationParameters.interpolator is None:
                    self.calculationParameters.interpolator = genfire.utility.getProjectionInterpolator(
                        self.calculationParameters.model)
                phi = self.calculationParameters.phi
                psi = self.calculationParameters.psi
                theta = np.arange(self.calculationParameters.thetaStart, \
                                  self.calculationParameters.thetaStop + 1e-10, \
                                  self.calculationParameters.thetaStep)
                projections = np.zeros(
                    (self.calculationParameters.dims[0],
                     self.calculationParameters.dims[1], np.size(theta)),
                    dtype=float)
                for i, current_theta in enumerate(theta):

                    pj = genfire.utility.calculateProjection_interp_fromInterpolator(
                        self.calculationParameters.interpolator, phi,
                        current_theta, psi,
                        np.shape(self.calculationParameters.model))
                    projections[:, :, i] = pj[
                        self.calculationParameters.ncOut -
                        int(self.calculationParameters.dims[0] /
                            2):self.calculationParameters.ncOut +
                        int(self.calculationParameters.dims[0] / 2),
                        self.calculationParameters.ncOut -
                        int(self.calculationParameters.dims[1] /
                            2):self.calculationParameters.ncOut +
                        int(self.calculationParameters.dims[1] / 2)]
                filename = self.calculationParameters.outputFilename
                filename = toString(filename)
                # projections[ projections<0 ] = 0
                genfire.fileio.writeVolume(filename, projections)
                if self.calculationParameters.writeAnglesFlag:
                    output_filename_base, ext = os.path.splitext(
                        toString(self.calculationParameters.outputFilename))
                    output_angle_filename = output_filename_base + "_euler_angles.txt"
                    if os.path.isfile(output_angle_filename):
                        print(
                            "WARNING! Filename {} already exists and will be overwritten."
                            .format(output_angle_filename))
                    self.calculationParameters.outputAngleFilename = output_angle_filename
                    num_projections = np.size(theta)
                    phi = np.repeat(phi, num_projections)
                    psi = np.repeat(psi, num_projections)
                    with open(output_angle_filename, 'w') as fid:
                        for euler in zip(phi, theta, psi):
                            string = str(euler[0]) + ' ' + str(
                                euler[1]) + ' ' + str(euler[2]) + '\n'
                            fid.write(string)
                    print("Successfully calculated {}.".format(
                        output_angle_filename))

            print("Successfully calculated {}.".format(filename))