def get3DRecon(data):
    RefDs = data[0]
    data = np.delete(data,0,0)
    ConstPixelSpacing = (float(RefDs.PixelSpacing[0]), float(RefDs.PixelSpacing[1]), float(RefDs.SliceThickness))
    try:
        voxel_ndarray, ijk_to_xyz = dicom_numpy.combine_slices(data)
    except dicom_numpy.DicomImportException as e:
        print("Handling incompatible dicom slices")
        makeCompatible(data, prec=5)
        voxel_ndarray, ijk_to_xyz = dicom_numpy.combine_slices(data)
    upper_thresh = 0
    lower_thresh = 0
    voxel_ndarray[voxel_ndarray > upper_thresh] = 1
    voxel_ndarray[voxel_ndarray <= lower_thresh] = 0
    return (voxel_ndarray,ijk_to_xyz)
Example #2
0
def extract_voxel_data(DCM_files):
    datasets = [dicom.read_file(f) for f in DCM_files]
    try:
        voxel_ndarray, ijk_to_xyz = dn.combine_slices(datasets)
    except dn.DicomImportException as e:
        raise e
    return voxel_ndarray
Example #3
0
def get3DRecon(data):
    RefDs = data[0]
    ConstPixelSpacing = (float(RefDs.PixelSpacing[0]),
                         float(RefDs.PixelSpacing[1]),
                         float(RefDs.SliceThickness))

    try:
        voxel_ndarray, ijk_to_xyz = dicom_numpy.combine_slices(data)
    except dicom_numpy.DicomImportException as e:
        # invalid DICOM data
        raise NameError(
            'Unable to do 3D reconstruction. Slice missing? or incompatible slice data?'
        )
    # arbitrary for now, can be set to different values for CT scan as in Hounsfield unit,
    # bone is from +700 to +3000
    """
	
	upper_thresh = 0
	lower_thresh = 0

	voxel_ndarray[voxel_ndarray > upper_thresh] = 1
	voxel_ndarray[voxel_ndarray <= lower_thresh] = 0
	
	"""

    return (voxel_ndarray, ijk_to_xyz)
def load_dicom_dir_dicom_orientation(dicomdir,
                                     all_headers=False,
                                     include_file_list=False):
    """
    Load the images from dicomdir into a numpy array, returned in "DICOM" standard dimension ordering
    (X, Y, Z), where Z==0 is the inferior ("feet") end, increasing Z is toward the superior ("head").
    Y increases toward the posterior, X increases toward the patient's left.
    """
    import logging

    logging.basicConfig()
    import dicom_numpy as dnp  # https://github.com/innolitics/dicom-numpy

    slices = filter_duplicates_from_dicomdir(dicomdir)

    img, affine = dnp.combine_slices([s[1] for s in slices])

    hdr = slices[0][1]
    hdr.Affine = affine
    hdr.RescaleIntercept = 0
    hdr.RescaleSlope = 1.0

    if all_headers or include_file_list:
        hdr = [s[1] for s in slices] if include_file_list == False else slices
        for h in hdr:
            if include_file_list:
                h = h[1]
            h.RescaleIntercept = 0
            h.RescaleSlope = 1.0
            h.Affine = affine

    return img, hdr
Example #5
0
 def read_dcm_dir(input_dir,
                  header=False,
                  globber='*',
                  channels_axis=None,
                  series=None):
     """
     Reads a 3D dicom image: input path can be a file or directory (DICOM series).
     Return the image array, metadata, and whether it has channels
     """
     # find all dicom files within the specified folder, read every file separately and sort them by InstanceNumber
     slices = PdcmIO.extract_slices(input_dir,
                                    globber=globber,
                                    series=series)
     img, affine = combine_slices(slices)
     metadata = PdcmIO.aff2meta(affine)
     if header:
         # TODO: add header support, something like
         #  metdata.header = [{str(key): ds[key] for key in ds.keys()} for ds in slices]
         raise NotImplementedError(
             "header=True is currently not supported for a series")
     samples_per_pixel = slices[0].SamplesPerPixel
     img = PdcmIO.move_channels_axis(
         img,
         samples_per_pixel=samples_per_pixel,
         channels_axis=channels_axis,
         planar_configuration=slices[0].get('PlanarConfiguration', None),
         default_axes=PdcmIO.DEFAULT_CHANNELS_AXES_DICOM_NUMPY)
     return img, metadata, samples_per_pixel > 1
Example #6
0
 def read_dcm_file(filename,
                   header=False,
                   allow_default_affine=False,
                   channels_axis=None):
     """
     Read a single dicom file.
     Return the image array, metadata, and whether it has channels
     """
     ds = pydicom.dcmread(filename)
     ds = convert_ds(ds)
     if ds.__class__ is MultiFrameFileDataset:
         img, affine = unpack_dataset(
             ds, allow_default_affine=allow_default_affine)
     else:
         img, affine = combine_slices([ds])
     metadata = PdcmIO.aff2meta(affine)
     if header:
         metadata.header = {str(key): ds[key] for key in ds.keys()}
     samples_per_pixel = ds.SamplesPerPixel
     img = PdcmIO.move_channels_axis(
         img,
         samples_per_pixel=samples_per_pixel,
         channels_axis=channels_axis,
         planar_configuration=ds.get('PlanarConfiguration', None),
         default_axes=PdcmIO.DEFAULT_CHANNELS_AXES_PYDICOM)
     return img, metadata, samples_per_pixel > 1
def extract_voxel_data(list_of_dicom_files):
    datasets = [dicom.read_file(f) for f in list_of_dicom_files]
    try:
        voxel_ndarray, ijk_to_xyz = dicom_numpy.combine_slices(datasets)
    except dicom_numpy.DicomImportException as e:
        # invalid DICOM data
        raise
    return voxel_ndarray
def _extract_voxel_data(datasets):
    try:
        voxel_ndarray, ijk_to_xyz = dicom_numpy.combine_slices(datasets)
    except dicom_numpy.DicomImportException as e:
        print("Exception extracting voxel data: ", e)
        raise e
    except AttributeError as e:
        print("Exception extracting voxel data: ", e)
        raise dicom_numpy.DicomImportException('Invalid dicom.dataset.Dataset among datasets! ', e)
    return voxel_ndarray
Example #9
0
def _extract_voxel_data(datasets):
    try:
        voxel_ndarray, ijk_to_xyz = dicom_numpy.combine_slices(datasets)
    except dicom_numpy.DicomImportException as e:
        print('Exception extracting voxel data: ', e)
        raise e
    except AttributeError as e:
        print('Exception extracting voxel data: ', e)
        raise dicom_numpy.DicomImportException('Invalid dicom.dataset.Dataset among datasets! ', e)

    return voxel_ndarray
def extract_voxel_data(dcm_lst):
    # https: // dicom - numpy.readthedocs.io / en / latest /
    import dicom
    import dicom_numpy
    datasets = [dicom.read_dile(fName) for fName in dcm_lst]
    try:
        voxel_ndarray, ijk_to_xyz = dicom_numpy.combine_slices(
            slice_datasets=datasets)
    except dicom_numpy.DicomImportException as e:
        raise
    return voxel_ndarray
Example #11
0
 def test_dcmToSimpleITK(self):
     dcmFiles = []
     for (dirPath, subDirs, fileNames) in os.walk(sample_dicom_folder):
         for fileName in fileNames:
             if not fileName.startswith('.'):
                 dcmFiles.append(os.path.join(sample_dicom_folder,
                                              fileName))
     datasets = [pydicom.dcmread(dcmSliceFile) for dcmSliceFile in dcmFiles]
     dicomAsArray, ijk_to_xyz = dicom_numpy.combine_slices(datasets)
     sITK_image = sitk.GetImageFromArray(dicomAsArray)
     assert (sITK_image == dcmToSimpleITK(sample_dicom_folder))
Example #12
0
def read_dicom_folder(dicom_folder, rescale=None):
    ''' Reads all .dcm files in `dicom_folder` and merges them to one volume

    Returns:
        The volume and the affine transformation from pixel indices to xyz coordinates
    '''
    dss = [
        pydicom.dcmread(str(dicom_folder / dcm))
        for dcm in os.listdir(dicom_folder) if dcm.endswith('.dcm')
    ]
    vol, mat = dicom_numpy.combine_slices(dss, rescale)
    return vol, dss[0]
def preprocess_dataset(raw_data_dir, out_dir, set):
    """
    load and concat the raw dicom/nrrd data, resample it to equalize the spacings and save out
    the numpy arrays per patient. Additionally, determine the class_weights as 1-class_ratio
    """

    set_dir = os.path.join(raw_data_dir, set)
    set_dir_seg = set_dir + '-segm'
    out_set_dir = os.path.join(
        out_dir, set) if set is not 'leaderboard' else os.path.join(
            out_dir, 'train')
    if not os.path.exists(out_set_dir):
        os.mkdir(out_set_dir)

    seg_paths = [
        os.path.join(set_dir_seg, ii) for ii in os.listdir(set_dir_seg)
    ]
    collect_class_weights = []
    ix = 0
    for path, dirs, files in os.walk(set_dir):
        if len(files) > 0:
            dicom_slices = [
                dicom.read_file(os.path.join(path, f)) for f in files
            ]
            img_arr, img_affine = dicom_numpy.combine_slices(dicom_slices)
            pid = path.split('/')[-3]
            seg_path = [seg_path for seg_path in seg_paths
                        if pid in seg_path][0]
            seg_arr, seg_info = nrrd.read(seg_path)
            if img_arr.shape == seg_arr.shape:
                data_arr = np.concatenate(
                    (img_arr[:, :, :, np.newaxis], seg_arr[:, :, :,
                                                           np.newaxis]),
                    axis=3)
                rs_data_arr = resample_array(data_arr, img_affine)
                class_ratios = np.unique(rs_data_arr[..., 1],
                                         return_counts=True)[1] / float(
                                             rs_data_arr[..., 1].size)
                collect_class_weights.append(class_ratios)
                np.save(
                    os.path.join(out_set_dir,
                                 '{}.npy'.format(path.split('/')[-3])),
                    rs_data_arr)
                print "processed", ix, os.path.join(
                    out_set_dir, '{}.npy'.format(path.split('/')[-3]))
            else:
                print "failed to process due to shape mismatch: {} {} {} {}".format(
                    img_arr.shape, seg_arr.shape, pid, set)
            ix += 1
    class_weights = 1 - np.mean(np.array(collect_class_weights), axis=0)
    print "class weights for set {} and classes BG, PZ, GC:".format(
        set), class_weights
Example #14
0
def dcmToSimpleITK(dcmDirectory):
    """Return a simple ITK image from a pile of dcm files. The returned sITK image has been rescaled based on the
    value of the rescale slope on the dicom tag. Array-like data of the 3D image can be obtained with the
    GetArrayFromImage() method"""
    list_dcmFiles = []
    for directory, subDirectory, list_dcmFileNames in os.walk(dcmDirectory):
        for dcmFile in list_dcmFileNames:
            if '.dcm' in dcmFile.lower():
                list_dcmFiles.append(os.path.join(directory, dcmFile))
    dcmImage = [pydicom.dcmread(dcmSliceFile) for dcmSliceFile in list_dcmFiles]
    voxel_ndarray, ijk_to_xyz = dicom_numpy.combine_slices(dcmImage)    
    sITK_image = sitk.GetImageFromArray(voxel_ndarray)
    return (sITK_image)
Example #15
0
def load_dicom_as_numpy(dicom_dir: str) -> np.ndarray:
    """
    Method to load ct image in numpy, slightly adapted form https://dicom-numpy.readthedocs.io/en/latest/
    :param dicom_dir: directory where dicom data is located
    :return: numpy array of ct data
    """
    datasets = [
        pydicom.read_file(dicom_dir + f) for f in os.listdir(dicom_dir)
    ]
    try:
        voxel_ndarray, ijk_to_xyz = dicom_numpy.combine_slices(datasets)
    except dicom_numpy.DicomImportException:
        raise
    return voxel_ndarray
Example #16
0
 def _loadFromDicom(self, filepath, *args, **kwargs):
     import pydicom
     import dicom_numpy
     if os.path.splitext(filepath)[1] not in self.dicom_extensions:
         if not os.path.isdir(filepath):
             raise TypeError('file must be a directory containing dicom files or a single dicom file')
         dcm_datasets = [pydicom.dcmread(os.path.join(filepath, x)) for x in os.listdir(filepath) if os.path.splitext(x)[1] in self.dicom_extensions]
         vol, affine = dicom_numpy.combine_slices(dcm_datasets)
         vol = vol.transpose(2,1,0).copy("C")
         self._cached_affine_matrix = affine
         return vol
     else:
         return np.expand_dims(pydicom.dcmread(filepath).pixel_array, axis=0)
     return None
Example #17
0
 def start(self):
     """
     open a file and sends notifier
     """
     super(DicomInput, self).start()
     datasets = [dicom.read_file(os.path.join(self.folder_path, f)) \
         for f in os.listdir(self.folder_path)]
     try:
         voxel_ndarray, _ = dicom_numpy.combine_slices(datasets)
     except dicom_numpy.DicomImportException:
         # invalid DICOM data
         raise
     self.package['data'] = voxel_ndarray
     self.log_line('read dicom files')
     self.output_notifier.notify_observers(self.package)
Example #18
0
 def read_dcm_dir(input_dir, globber='*', channels_axis=None):
     """Reads a 3D dicom image: input path can be a file or directory (DICOM series)"""
     # find all dicom files within the specified folder, read every file separately and sort them by InstanceNumber
     files = list(Path(input_dir).glob(globber))
     if len(files) == 0:
         raise FileNotFoundError(
             f'Received an empty directory: "{input_dir}"')
     slices = [pydicom.dcmread(filename) for filename in files]
     slices.sort(key=lambda ds: ds.get('InstanceNumber', 0))
     img, affine = combine_slices(slices)
     metadata = PdcmIO.aff2meta(affine)
     img = PdcmIO.move_channels_axis(
         img,
         samples_per_pixel=slices[0].SamplesPerPixel,
         channels_axis=channels_axis)
     return img, metadata
Example #19
0
 def read_dcm_file(filename,
                   header=False,
                   allow_default_affine=False,
                   channels_axis=None):
     """Read a single dicom file"""
     ds = pydicom.dcmread(filename)
     ds = convert_ds(ds)
     if ds.__class__ is MultiFrameFileDataset:
         img, affine = unpack_dataset(
             ds, allow_default_affine=allow_default_affine)
     else:
         img, affine = combine_slices([ds])
     metadata = PdcmIO.aff2meta(affine)
     if header:
         metadata.header = {str(key): ds[key] for key in ds.keys()}
     img = PdcmIO.move_channels_axis(img,
                                     samples_per_pixel=ds.SamplesPerPixel,
                                     channels_axis=channels_axis)
     return img, metadata
Example #20
0
def get_scan(dicom_path, scan_size):
    # Getting DICOM images from path:
    if not os.path.exists(dicom_path):
        print('DICOM files not exists!')
        return
    dicom_files = listdir(dicom_path)
    dicom_files.sort()
    voxel_ndarray, ijk_to_xyz = dicom_numpy.combine_slices([
        pydicom.read_file(dicom_path + '/' + dcm_file, force=True)
        for dcm_file in dicom_files
    ])
    vox_array = []
    for i in range(voxel_ndarray.shape[-1]):
        if vox_array == []:
            vox_array = imresize(voxel_ndarray[:, :, i], scan_size)
        else:
            vox_array = np.dstack(
                (vox_array, imresize(voxel_ndarray[:, :, i], scan_size)))
    return vox_array
Example #21
0
def create_DICOM_Array(PathDicom):
    filenames_list = []

    file_list = os.listdir(PathDicom)
    for file in file_list:
        filenames_list.append(PathDicom + file)
    datasets = [dicom.read_file(f) \
                for f in filenames_list]
    try:
        voxel_ndarray, _ = dicom_numpy.combine_slices(datasets)
        voxel_ndarray = voxel_ndarray.astype(float)
        voxel_ndarray = np.swapaxes(voxel_ndarray, 0, 1)
        print(voxel_ndarray.dtype)
        # voxel_ndarray = voxel_ndarray[:-1:]
        # print(voxel_ndarray.shape)
    except dicom_numpy.DicomImportException:
        # invalid DICOM data
        raise

    return voxel_ndarray
Example #22
0
def create_DICOM_Array(mrt_Folder, proband, model):
    filenames_list = []
    PathDicom = mrt_Folder + "/" + proband + "/dicom_sorted/" + model + "/"
    file_list = os.listdir(PathDicom)
    for file in file_list:
        filenames_list.append(PathDicom + file)
    datasets = [dicom.read_file(f) \
                for f in filenames_list]
    try:
        voxel_ndarray, _ = dicom_numpy.combine_slices(datasets)
        print(voxel_ndarray.dtype)
        voxel_ndarray = voxel_ndarray.astype(float)  #float
        voxel_ndarray = np.swapaxes(voxel_ndarray, 0, 1)
        print(voxel_ndarray.dtype)
        # voxel_ndarray = voxel_ndarray[:-1:]
        # print(voxel_ndarray.shape)
    except dicom_numpy.DicomImportException:
        # invalid DICOM data
        raise

    return voxel_ndarray
Example #23
0
def get_DICOM_data(DICOM_dir, keys='minimal'):

    # find DICOM files in the specified directory
    DICOM_file_paths = glob(DICOM_dir + '*.dcm')
    DICOM_file_paths = natsorted(DICOM_file_paths)

    # initialise metadata dictionary
    metadata = {}

    # hard code basic dicom tags
    minimal_keys = [
        'EchoTime', 'RepetitionTime', 'AcquisitionNumber', 'SeriesNumber',
        'InversionTime', 'FlipAngle', 'ImageOrientationPatient', 'ContentTime',
        'ProtocolName', 'InstanceNumber', 'Rows', 'TriggerTime',
        'AcquisitionTime', 'InPlanePhaseEncodingDirection',
        'SeriesInstanceUID', 'Columns', 'PixelSpacing'
    ]

    ############################## DICOM_NUMPY ################################

    datasets = [dicom.read_file(f) for f in DICOM_file_paths]

    # use dcmstack metadata if possible
    dcmstack_metadata = False

    try:

        DICOM_data, DICOM_affine = combine_slices(datasets)

    except Exception, e:

        print e
        print 'WARNING: could not assemble valid DICOM image for ' + DICOM_dir
        DICOM_data = np.array(-1)
        DICOM_affine = 'WARNING: could not get affine matrix'
        print 'Trying alternative method to construct DICOM image'

        ######################## DICOM STACK ##################################

        # read into stack of data
        DICOM_stack = dcmstack.DicomStack()

        try:

            for DICOM_file_path in DICOM_file_paths:

                DICOM_file = dicom.read_file(DICOM_file_path)
                DICOM_stack.add_dcm(DICOM_file)

            # get data and affine
            DICOM_data = DICOM_stack.get_data()
            DICOM_affine = DICOM_stack.get_affine()

            # get a nifti wrapper for the DICOM stack
            nii_wrp = DICOM_stack.to_nifti_wrapper()

            if keys == 'minimal':

                # just get minimal keys from the stack
                keys = DICOM_stack.minimal_keys

            elif keys == 'all':

                # get all metadata keys
                keys = nii_wrp.meta_ext.get_keys()

            else:

                print(
                    'WARNING: keys arguments must be set to "minimal" to search for a default set of basic DICOM tags, or "all" to search for all tags associated with each image.'
                )

            # build dictionary of key-value pairs for the metadata
            for key in keys:

                metadata.update({key: nii_wrp.get_meta(key)})

            dcmstack_metadata = True

        except Exception, e:

            print e
            print 'WARNING: could not assemble valid DICOM image for ' + DICOM_dir + 'with alternative method'
            DICOM_data = np.array(-1)
            DICOM_affine = 'WARNING: could not get affine matrix'
Example #24
0
    def load_MRT(self):
        mrt_layer_name = str(
            self.proband_str.get()) + "_" + self.mrt_model[str(
                self.artefact_str.get())]
        filenames_list = []
        if mrt_layer_name in self.mrt_layer_names:
            pass
        else:
            self.mrt_layer_names.append(mrt_layer_name)
            PathDicom = self.sFolder + "/" + self.proband_str.get(
            ) + "/dicom_sorted/" + self.artefact_str.get() + "/"

            #load Dicom_Array
            files = sorted([
                os.path.join(PathDicom, file) for file in os.listdir(PathDicom)
            ],
                           key=os.path.getctime)
            datasets = [dicom.read_file(f) \
                        for f in files]

            try:
                voxel_ndarray, pixel_space = dicom_numpy.combine_slices(
                    datasets)
                voxel_ndarray_matlab = voxel_ndarray.transpose(1, 0, 2)
            except dicom_numpy.DicomImportException:
                # invalid DICOM data
                raise

            dx, dy, dz = 1.0, 1.0, pixel_space[2][
                2]  #pixel_space[0][0], pixel_space[1][1], pixel_space[2][2]
            pixel_array = voxel_ndarray[:, :, 0]
            x_1d = dx * np.arange(voxel_ndarray.shape[0])
            y_1d = dy * np.arange(voxel_ndarray.shape[1])
            z_1d = dz * np.arange(voxel_ndarray.shape[2])

            smodel = self.mrt_smodel[str(self.artefact_str.get())]
            sartefact = self.mrt_artefact[str(self.artefact_str.get())]
            mrt_layer = MRT_Layer(voxel_ndarray.shape[0],
                                  voxel_ndarray.shape[1],
                                  voxel_ndarray.shape[2], sartefact, smodel,
                                  x_1d, y_1d, z_1d, voxel_ndarray,
                                  mrt_layer_name, voxel_ndarray_matlab)
            self.mrt_layer_set.append(mrt_layer)
            self.optionlist.append("(" + str(len(self.mrt_layer_set)) + ") " +
                                   mrt_layer_name + ": " +
                                   str(voxel_ndarray.shape[0]) + " x " +
                                   str(voxel_ndarray.shape[1]))
            self.refresh_option(self.optionlist)

            self.number_mrt_label.config(text="1/" +
                                         str(mrt_layer.get_number_mrt()))
            self.number_mrt_label.place(x=1330, y=10)
            self.art_mod_label.config(
                text="Proband:  " + str(self.proband_str.get()) +
                "\nModel:  " + str(self.artefact_str.get()))
            self.art_mod_label.place(x=5, y=5)
            plt.cla()
            plt.gca().set_aspect('equal')  # plt.axes().set_aspect('equal')
            plt.xlim(0, voxel_ndarray.shape[0] * dx)
            plt.ylim(voxel_ndarray.shape[1] * dy, 0)
            plt.set_cmap(plt.gray())
            self.pltc = plt.pcolormesh(x_1d,
                                       y_1d,
                                       np.swapaxes(pixel_array, 0, 1),
                                       vmin=0,
                                       vmax=2094)

            # load
            File_Path = self.Path_marking + self.proband_str.get() + ".slv"
            loadFile = shelve.open(File_Path)
            number_Patch = 0
            cur_no = "0"
            if loadFile.has_key(self.artefact_str.get()):
                layer = loadFile[self.artefact_str.get()]
                while layer.has_key(
                        cur_no + "_11_" + str(number_Patch)) or layer.has_key(
                            cur_no + "_12_" +
                            str(number_Patch)) or layer.has_key(
                                cur_no + "_13_" +
                                str(number_Patch)) or layer.has_key(
                                    cur_no + "_21_" +
                                    str(number_Patch)) or layer.has_key(
                                        cur_no + "_22_" +
                                        str(number_Patch)) or layer.has_key(
                                            cur_no + "_23_" + str(number_Patch)
                                        ) or layer.has_key(
                                            cur_no + "_31_" + str(number_Patch)
                                        ) or layer.has_key(
                                            cur_no + "_32_" + str(number_Patch)
                                        ) or layer.has_key(cur_no + "_33_" +
                                                           str(number_Patch)):
                    patch = None
                    if layer.has_key(cur_no + "_11_" + str(number_Patch)):
                        p = layer[cur_no + "_11_" + str(number_Patch)]
                        patch = plt.Rectangle(
                            (min(p[0], p[2]), min(p[1], p[3])),
                            np.abs(p[0] - p[2]),
                            np.abs(p[1] - p[3]),
                            fill=False,
                            edgecolor="red",
                            lw=2)
                    elif layer.has_key(cur_no + "_12_" + str(number_Patch)):
                        p = layer[cur_no + "_12_" + str(number_Patch)]
                        patch = plt.Rectangle(
                            (min(p[0], p[2]), min(p[1], p[3])),
                            np.abs(p[0] - p[2]),
                            np.abs(p[1] - p[3]),
                            fill=False,
                            edgecolor="green",
                            lw=2)
                    elif layer.has_key(cur_no + "_13_" + str(number_Patch)):
                        p = layer[cur_no + "_13_" + str(number_Patch)]
                        patch = plt.Rectangle(
                            (min(p[0], p[2]), min(p[1], p[3])),
                            np.abs(p[0] - p[2]),
                            np.abs(p[1] - p[3]),
                            fill=False,
                            edgecolor="blue",
                            lw=2)
                    elif layer.has_key(cur_no + "_21_" + str(number_Patch)):
                        p = layer[cur_no + "_21_" + str(number_Patch)]
                        patch = Ellipse(
                            xy=(min(p[0], p[2]) + np.abs(p[0] - p[2]) / 2,
                                min(p[1], p[3]) + np.abs(p[1] - p[3]) / 2),
                            width=np.abs(p[0] - p[2]),
                            height=np.abs(p[1] - p[3]),
                            edgecolor="red",
                            fc='None',
                            lw=2)
                    elif layer.has_key(cur_no + "_22_" + str(number_Patch)):
                        p = layer[cur_no + "_22_" + str(number_Patch)]
                        patch = Ellipse(
                            xy=(min(p[0], p[2]) + np.abs(p[0] - p[2]) / 2,
                                min(p[1], p[3]) + np.abs(p[1] - p[3]) / 2),
                            width=np.abs(p[0] - p[2]),
                            height=np.abs(p[1] - p[3]),
                            edgecolor="green",
                            fc='None',
                            lw=2)
                    elif layer.has_key(cur_no + "_23_" + str(number_Patch)):
                        p = layer[cur_no + "_23_" + str(number_Patch)]
                        patch = Ellipse(
                            xy=(min(p[0], p[2]) + np.abs(p[0] - p[2]) / 2,
                                min(p[1], p[3]) + np.abs(p[1] - p[3]) / 2),
                            width=np.abs(p[0] - p[2]),
                            height=np.abs(p[1] - p[3]),
                            edgecolor="blue",
                            fc='None',
                            lw=2)
                    elif layer.has_key(cur_no + "_31_" + str(number_Patch)):
                        p = layer[cur_no + "_31_" + str(number_Patch)]
                        patch = patches.PathPatch(p,
                                                  fill=False,
                                                  edgecolor='red',
                                                  lw=2)
                    elif layer.has_key(cur_no + "_32_" + str(number_Patch)):
                        p = layer[cur_no + "_32_" + str(number_Patch)]
                        patch = patches.PathPatch(p,
                                                  fill=False,
                                                  edgecolor='green',
                                                  lw=2)
                    elif layer.has_key(cur_no + "_33_" + str(number_Patch)):
                        p = layer[cur_no + "_33_" + str(number_Patch)]
                        patch = patches.PathPatch(p,
                                                  fill=False,
                                                  edgecolor='blue',
                                                  lw=2)
                    self.ax.add_patch(patch)
                    number_Patch += 1

            self.fig.canvas.draw()
Example #25
0
import pydicom
import dicom_numpy
from os import listdir
import numpy as np
import math
from copy import deepcopy

# load data
path='path_to_mri_folder'
matrix = np.loadtxt('path_to xyz_to_ijk_matrix')
spikes = np.loadtxt('path_to_meg_data')
slices = [pydicom.read_file(path + '/' + s) for s in listdir(path)]
slices.sort(key = lambda x: int(x.InstanceNumber))
voxel_ndarray, _ = dicom_numpy.combine_slices(slices)


# you have to check that the orientation of slices and voxel_ndarray are the same
# sometimes you have to inverse z orientation
# voxel_ndarray = voxel_ndarray.T[::-1]
# plt.imshow(voxel_ndarray[-67], cmap=plt.cm.bone)
# plt.show()
# plt.imshow(slices[-67].pixel_array, cmap=plt.cm.bone)
# plt.show()

# switch to ijk space
spikes_in_ijk = np.dot(matrix, np.c_[spikes, np.ones(len(spikes))].T).T[:,:-1]*[1,1,1]
voxel_ind = spikes_in_ijk.astype(int)

# increase the intensity of the required voxels
# indices are specified depending on the orientation of the original volume, 
# so that the coordinate system of the displayed data coincides