Example #1
0
    def create_project_file(self, name, spacing, modality, orientation, window_width, window_level, image, affine='', folder=None):
        if folder is None:
            folder = tempfile.mkdtemp()
        if not os.path.exists(folder):
            os.mkdir(folder)
        image_file = os.path.join(folder, 'matrix.dat')
        image_mmap = imagedata_utils.array2memmap(image, image_file)
        matrix = {
            'filename': 'matrix.dat',
            'shape': image.shape,
            'dtype': str(image.dtype)
        }
        project = {
                   # Format info
                   "format_version": const.INVESALIUS_ACTUAL_FORMAT_VERSION,
                   "invesalius_version": const.INVESALIUS_VERSION,
                   "date": datetime.datetime.now().isoformat(),
                   "compress": True,

                   # case info
                   "name": name, # patient's name
                   "modality": modality, # CT, RMI, ...
                   "orientation": orientation,
                   "window_width": window_width,
                   "window_level": window_level,
                   "scalar_range": (int(image.min()), int(image.max())),
                   "spacing": spacing,
                   "affine": affine,

                   "matrix": matrix,
                  }

        path = os.path.join(folder, 'main.plist')
        with open(path, 'w+b') as f:
            plistlib.dump(project, f)
Example #2
0
    def create_project_from_matrix(self,
                                   name,
                                   matrix,
                                   orientation="AXIAL",
                                   spacing=(1.0, 1.0, 1.0),
                                   modality="CT",
                                   window_width=None,
                                   window_level=None,
                                   new_instance=False):
        """
        Creates a new project from a Numpy 3D array.

        name: Name of the project.
        matrix: A Numpy 3D array. It only works with int16 arrays.
        spacing: The spacing between the center of the voxels in X, Y and Z direction.
        modality: Imaging modality.
        """
        if window_width is None:
            window_width = (matrix.max() - matrix.min())
        if window_level is None:
            window_level = (matrix.max() + matrix.min()) // 2

        window_width = int(window_width)
        window_level = int(window_level)

        name_to_const = {
            "AXIAL": const.AXIAL,
            "CORONAL": const.CORONAL,
            "SAGITTAL": const.SAGITAL
        }

        if new_instance:
            self.start_new_inv_instance(matrix, name, spacing, modality,
                                        name_to_const[orientation],
                                        window_width, window_level)
        else:
            # Verifying if there is a project open
            s = ses.Session()
            if s.IsOpen():
                Publisher.sendMessage('Close Project')
                Publisher.sendMessage('Disconnect tracker')

            # Check if user really closed the project, if not, stop project creation
            if s.IsOpen():
                return

            mmap_matrix = image_utils.array2memmap(matrix)

            self.Slice = sl.Slice()
            self.Slice.matrix = mmap_matrix
            self.Slice.matrix_filename = mmap_matrix.filename
            self.Slice.spacing = spacing

            self.Slice.window_width = window_width
            self.Slice.window_level = window_level

            proj = prj.Project()
            proj.name = name
            proj.modality = modality
            proj.SetAcquisitionModality(modality)
            proj.matrix_shape = matrix.shape
            proj.matrix_dtype = matrix.dtype.name
            proj.matrix_filename = self.Slice.matrix_filename
            proj.window = window_width
            proj.level = window_level


            proj.original_orientation =\
                name_to_const[orientation]

            proj.threshold_range = int(matrix.min()), int(matrix.max())
            proj.spacing = self.Slice.spacing

            Publisher.sendMessage('Update threshold limits list',
                                  threshold_range=proj.threshold_range)

            ######
            session = ses.Session()
            filename = proj.name + ".inv3"

            filename = filename.replace("/", "")

            dirpath = session.CreateProject(filename)

            self.LoadProject()
            Publisher.sendMessage("Enable state project", state=True)