当前位置: 首页>>代码示例>>Python>>正文


Python pydicom.read_file函数代码示例

本文整理汇总了Python中pydicom.read_file函数的典型用法代码示例。如果您正苦于以下问题:Python read_file函数的具体用法?Python read_file怎么用?Python read_file使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了read_file函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: is_dicom_dir

def is_dicom_dir(datapath):
    """
    Check if in dir is one or more dicom file. We use two methods.
    First is based on dcm extension detection.
    """
    # Second tries open files
    # with dicom module.

    retval = False
    datapath = op.expanduser(datapath)
    for f in os.listdir(datapath):
        if f.endswith((".dcm", ".DCM")):
            retval = True
            return True
        # @todo not working and I dont know why
        try:
            pydicom.read_file(os.path.join(datapath, f))

            retval = True
        # except pydicom.errors.InvalidDicomError:
        #     logger.debug("Invalid Dicom while reading file " + str(f))
        except Exception as e:
            logger.warning("Unable to read dicom file " + str(f))
            logger.warning(e)
            # import traceback
            # traceback.print_exc()

        if retval:
            return True
    return False
开发者ID:mjirik,项目名称:io3d,代码行数:30,代码来源:dcmreaddata.py

示例2: DicomRead

def DicomRead (exam, series, noEchos):
    #Call in the images within the exam and series of interest
    directory = 'exam_' + str(exam) + '/Ser' + str(series)
    os.chdir(directory)
    listFilesDCM = natsorted(glob.glob('E*S*I*.MR.dcm'))
    #Get the reference information from the very first image of that list of images    
    RefDs = pydicom.read_file(listFilesDCM[0])  #Stored ref file 
    #Import array dims are the dimensions of the imported 3D array. ConstPixelDims are the dimensions of the 4D matrix (row, column, slice, echo)
    #spacing is the size of each voxel in 3D space along the three axes.    
    ImportArrayPixelDims = (int(RefDs.Rows), int(RefDs.Columns), len(listFilesDCM)) #Dimensions of rows, columns, slicers
    ConstPixelDims = (int(RefDs.Rows), int(RefDs.Columns), int(len(listFilesDCM)/noEchos), noEchos)
    ConstPixelSpacing = (float(RefDs.PixelSpacing[0]), float(RefDs.PixelSpacing[1]), float(RefDs.SliceThickness))  #spacing values (mm)
    
    #these are a list of the dimensions. Not used at the moment. 
    x = numpy.arange(0.0, (ConstPixelDims[0]+1)*ConstPixelSpacing[0], ConstPixelSpacing[0])
    y = numpy.arange(0.0, (ConstPixelDims[1]+1)*ConstPixelSpacing[1], ConstPixelSpacing[1])
    z = numpy.arange(0.0, (ConstPixelDims[2]+1)*ConstPixelSpacing[2], ConstPixelSpacing[2])  
    
    #Below seciont of cose will import each dcm in the folder of interest. The array data is all saved to ArrayDicom. 
    #The header data is all saved to header.X where X is the image of interest
    #The first Y echoes are saved so echos, where Y is the number of echos for each slice.     
    ArrayDicom = numpy.zeros(ImportArrayPixelDims, dtype=RefDs.pixel_array.dtype)    
    echoTimes =[]
    class container:
        pass
    header = container()
    for filenameDCM in listFilesDCM:
        ds = pydicom.read_file(filenameDCM)
        ArrayDicom[:,:,listFilesDCM.index(filenameDCM)] = ds.pixel_array
        echoTimes = numpy.hstack((echoTimes, ds.EchoTime))
        echos = echoTimes[0:noEchos]
    for image in range (0, len(listFilesDCM)):
        ds = pydicom.read_file(listFilesDCM[image])
        header.image = ds 
    return(ArrayDicom, echos, ConstPixelDims, RefDs, header)
开发者ID:gattia,项目名称:PyMap,代码行数:35,代码来源:importDicomHeader.py

示例3: _extract_series_frames

def _extract_series_frames(simulation, dicom_dir):
    #TODO(pjm): give user a choice between multiple study/series if present
    selected_series = None
    frames = {}
    dicom_dose = None
    rt_struct_path = None
    res = {
        'description': '',
    }
    for path in pkio.walk_tree(dicom_dir):
        if pkio.has_file_extension(str(path), 'dcm'):
            plan = dicom.read_file(str(path))
            if plan.SOPClassUID == _DICOM_CLASS['RT_STRUCT']:
                rt_struct_path = str(path)
            elif plan.SOPClassUID == _DICOM_CLASS['RT_DOSE']:
                res['dicom_dose'] = _summarize_rt_dose(simulation, plan)
                plan.save_as(_dose_dicom_filename(simulation))
            if plan.SOPClassUID != _DICOM_CLASS['CT_IMAGE']:
                continue
            orientation = _float_list(plan.ImageOrientationPatient)
            if not (_EXPECTED_ORIENTATION == orientation).all():
                continue
            if not selected_series:
                selected_series = plan.SeriesInstanceUID
                res['StudyInstanceUID'] = plan.StudyInstanceUID
                res['PixelSpacing'] = plan.PixelSpacing
                if hasattr(plan, 'SeriesDescription'):
                    res['description'] = plan.SeriesDescription
            if selected_series != plan.SeriesInstanceUID:
                continue
            info = {
                'pixels': np.float32(plan.pixel_array),
                'shape': plan.pixel_array.shape,
                'ImagePositionPatient': _string_list(plan.ImagePositionPatient),
                'ImageOrientationPatient': _float_list(plan.ImageOrientationPatient),
                'PixelSpacing': _float_list(plan.PixelSpacing),
            }
            for f in ('FrameOfReferenceUID', 'StudyInstanceUID', 'SeriesInstanceUID', 'SOPInstanceUID'):
                info[f] = getattr(plan, f)
            z = _frame_id(info['ImagePositionPatient'][2])
            info['frameId'] = z
            if z in frames:
                raise RuntimeError('duplicate frame with z coord: {}'.format(z))
            _scale_pixel_data(plan, info['pixels'])
            frames[z] = info
    if not selected_series:
        raise RuntimeError('No series found with {} orientation'.format(_EXPECTED_ORIENTATION))
    if rt_struct_path:
        res['regionsOfInterest'] = _summarize_rt_structure(simulation, dicom.read_file(rt_struct_path), frames.keys())
    sorted_frames = []
    res['frames'] = sorted_frames
    for z in sorted(_float_list(frames.keys())):
        sorted_frames.append(frames[_frame_id(z)])
    return res
开发者ID:e-carlin,项目名称:sirepo,代码行数:54,代码来源:rs4pi.py

示例4: setUp

    def setUp(self):
        dcm_path = path.join(test_dir,
                             'data',
                             'dcmstack',
                             '2D_16Echo_qT2',
                             'TE_20_SlcPos_-33.707626341697.dcm')
        self.dcm = pydicom.read_file(dcm_path)

        self.stack = dcmstack.DicomStack()
        self.stack.add_dcm(self.dcm)
        self.dcm = pydicom.read_file(dcm_path)
开发者ID:moloney,项目名称:dcmstack,代码行数:11,代码来源:test_dcmstack.py

示例5: retrieve_dicom

 def retrieve_dicom(self, filename):
     """Return a file as a dicom object."""
     try:
         return pydicom.read_file(self.retrieve_file(filename), force=True)
     except Exception as e:
         raise(e, "Exception {} raised with {}, {}".format(
               (filename, type(e).__name__)))
开发者ID:cni,项目名称:rtfmri,代码行数:7,代码来源:client.py

示例6: read_slice

def read_slice(path, d):
    """Read a single slice."""
    try:
        df = pydicom.read_file(str(path))
    except pydicom.filereader.InvalidDicomError as e:
        log.error('Error reading %s: %s', path, e)
        return
    if 'PixelData' not in df:
        return
    d.setdefault('orientation', df.ImageOrientationPatient)
    if d['orientation'] != df.ImageOrientationPatient:
        raise Exception('Orientation mismatch.')
    d.setdefault('shape', df.pixel_array.shape)
    if d['shape'] != df.pixel_array.shape:
        raise Exception('Shape mismatch: {}'.format(path))
    d.setdefault('dtype', df.pixel_array.dtype)
    if d['dtype'] != df.pixel_array.dtype:
        raise Exception('Type mismatch.')
    d.setdefault('voxel_spacing', get_voxel_spacing(df))
    position = tuple(float(x) for x in df.ImagePositionPatient)
    bvalue = get_bvalue(df)
    echotime = get_echotime(df)
    pixels = get_pixels(df)
    d.setdefault('positions', set()).add(position)
    d.setdefault('bvalues', set()).add(bvalue)
    d.setdefault('echotimes', set()).add(echotime)
    key = (position, bvalue, echotime)
    slices = d.setdefault('slices', {})
    if key in slices:
        log.error('Overlapping slices (%s), discarding %s', key, path)
        s = 'Overlapping slices, discarding {}'.format(path)
        d['errors'].append(s)
    slices[key] = pixels
开发者ID:jupito,项目名称:dwilib,代码行数:33,代码来源:dicomfile.py

示例7: test_invalid_sop_no_file_meta

 def test_invalid_sop_no_file_meta(self):
     """Test exception raised if invalid sop class but no file_meta"""
     ds = read_file(get_testdata_files('CT_small.dcm')[0])
     with pytest.raises(AttributeError,
                        match="'DicomDir' object has no attribute "
                              "'DirectoryRecordSequence'"):
         DicomDir("some_name", ds, b'\x00' * 128, None, True, True)
开发者ID:jrkerns,项目名称:pydicom,代码行数:7,代码来源:test_dicomdir.py

示例8: isdicom

def isdicom(filename):
    if os.path.basename(filename).lower() == 'dicomdir':
        return False
    try:
        return pydicom.read_file(filename)
    except pydicom.filereader.InvalidDicomError:
        return False
开发者ID:kayarre,项目名称:dicomSort,代码行数:7,代码来源:dicomsorter.py

示例9: read_dicom_with_pydicom

def read_dicom_with_pydicom(dicom_file, dicom_fields):
    """
    Read DICOM file using PyDICOM python library.

    :param dicom_file: DICOM file to read
     :type dicom_file: str
    :param dicom_fields: Dictionary containing DICOM fields and values
     :type dicom_fields: dict

    :return: updated dictionary of DICOM fields and values
     :rtype : dict

    """

    # Read DICOM file
    dicom_dataset = dicom.read_file(dicom_file)

    # Grep information from DICOM header and store them
    # into dicom_fields dictionary under flag Value
    # Dictionnary of DICOM values to be returned
    for name in dicom_fields:
        try:
            description = dicom_fields[name]['Description']
            value = dicom_dataset.data_element(description).value
            dicom_fields[name]['Value'] = value
        except:
            continue

    return dicom_fields
开发者ID:cmadjar,项目名称:DICAT,代码行数:29,代码来源:anonymizer_methods.py

示例10: loadDicomImages

    def loadDicomImages(self, dicomFilesPath):

        # TODO Implement verification if dicom files exists
        dicomPath = os.path.join(dicomFilesPath, os.listdir(dicomFilesPath)[1])
        try:
            dicomFile = pydicom.read_file(dicomPath)
        except Exception as e:
            raise e

        if 0x00280106 in dicomFile and 0x00280107 in dicomFile:
            self.IMAGE_SMALLEST_PIXEL = dicomFile[0x00280106].value  # SmallestImagePixelValue
            self.IMAGE_LARGEST_PIXEL = dicomFile[0x00280107].value  # LargestImagePixelValue
        else:
            self.IMAGE_SMALLEST_PIXEL = 0
            self.IMAGE_LARGEST_PIXEL = 100

        self.wxUpperSlider.SetRange(self.IMAGE_SMALLEST_PIXEL, self.IMAGE_LARGEST_PIXEL)
        self.wxLowerSlider.SetRange(self.IMAGE_SMALLEST_PIXEL, self.IMAGE_LARGEST_PIXEL)
        self.wxLowerSlider.Update()
        self.Layout()

        dicomImages = vtkDICOMImageReader()
        dicomImages.SetDirectoryName(dicomFilesPath)
        dicomImages.Update()

        self.DICOM_IMAGES = dicomImages
        self.adjustImageThreshold(800)
        #self.__plotImage(self.ROOT_PIPE)
        self.createVolume()
        self.decimateVolume(0.5)
        self.view3DImage(self.ROOT_PIPE)
开发者ID:SHINOTECH,项目名称:vtkDicomRender,代码行数:31,代码来源:main.py

示例11: test_invalid_sop_file_meta

 def test_invalid_sop_file_meta(self):
     """Test exception raised if SOP Class is not Media Storage Directory"""
     ds = read_file(get_testdata_files('CT_small.dcm')[0])
     with pytest.raises(InvalidDicomError,
                        match="SOP Class is not Media Storage "
                              "Directory \(DICOMDIR\)"):
         DicomDir("some_name", ds, b'\x00' * 128, ds.file_meta, True, True)
开发者ID:jrkerns,项目名称:pydicom,代码行数:7,代码来源:test_dicomdir.py

示例12: read_data

def read_data(dcmdir, indices=None, wildcard='*.dcm', type=np.int16):

    dcmlist = []
    for infile in glob.glob(os.path.join(dcmdir, wildcard)):
        dcmlist.append(infile)

    if indices == None:
        indices = range(len(dcmlist))

    data3d = []
    for i in range(len(indices)):
        ind = indices[i]
        onefile = dcmlist[ind]
        if wildcard == '*.dcm':
            data = pydicom.read_file(onefile)
            data2d = data.pixel_array
            try:
                data2d = (np.float(data.RescaleSlope) * data2d) + np.float(data.RescaleIntercept)
            except:
                print('problem with RescaleSlope and RescaleIntercept')
        else:
            data2d = cv2.imread(onefile, 0)

        if len(data3d) == 0:
            shp2 = data2d.shape
            data3d = np.zeros([shp2[0], shp2[1], len(indices)], dtype=type)

        data3d[:,:,i] = data2d

    #need to reshape data to have slice index (ndim==3)
    if data3d.ndim == 2:
        data3d.resize(np.hstack((data3d.shape,1)))

    return data3d
开发者ID:mjirik,项目名称:lisa,代码行数:34,代码来源:tools.py

示例13: anon

def anon(fname):
    patient_list = contained_dirs(fname)

    for i in patient_list:
        if not os.listdir(i):
            continue
        elif os.path.split(os.path.split(i)[1])[1][:4] == 'AAA_':
            acquis_list = contained_dirs(i)
            for j in acquis_list:
                f = []
                for (dirpath, dirnames, filenames) in os.walk(j):
                    f.extend(filenames)
                    break
                ll = 0
                for k in filenames:
                    image = j + '/' + k
                    ds = pydicom.read_file(image)
                    ID = ds.PatientID
                    new_PN = ID
                    new_ID = 'FLOW_' + os.path.split(i)[1] + '_' + ID
                    anonymize(image, image, new_person_name=new_PN,
                              new_patient_id=new_ID, remove_curves=True, remove_private_tags=True)

                    if ll % 50 == 0:
                        print 'anonymized', ll, 'over', len(filenames)
                    ll += 1

        else:
            continue
    return
开发者ID:totocaca,项目名称:LCS_tractor,代码行数:30,代码来源:dickinfo.py

示例14: pydicom_zapping

def pydicom_zapping(dicom_file, dicom_fields):
    """
    Actual zapping method for PyDICOM

    :param dicom_file: DICOM to anonymize
     :type dicom_file: str
    :param dicom_fields: Dictionary with DICOM fields & values to use
     :type dicom_fields: dict

    :return: None

    """

    dicom_dataset = dicom.read_file(dicom_file)

    for name in dicom_fields:
        new_val = ""
        if 'Value' in dicom_fields[name]:
            new_val = dicom_fields[name]['Value']

        if dicom_fields[name]['Editable'] is True:
            try:
                dicom_dataset.data_element(
                    dicom_fields[name]['Description']).value = new_val
            except:
                continue
        else:
            try:
                dicom_dataset.data_element(
                    dicom_fields[name]['Description']).value = ''
            except:
                continue
    dicom_dataset.save_as(dicom_file)
开发者ID:cmadjar,项目名称:DICAT,代码行数:33,代码来源:anonymizer_methods.py

示例15: _create_dicomdir_info

    def _create_dicomdir_info(self):
        """
        Function crates list of all files in dicom dir with all IDs
        """

        filelist = files_in_dir(self.dirpath)
        files = []
        metadataline = {}

        for filepath in filelist:
            head, teil = os.path.split(filepath)
            dcmdata = None
            if os.path.isdir(filepath):
                logger.debug("Subdirectory found in series dir is ignored: " + str(filepath))
                continue
            try:
                dcmdata = pydicom.read_file(filepath)

            except pydicom.errors.InvalidDicomError as e:
                # some files doesnt have DICM marker
                try:
                    dcmdata = pydicom.read_file(filepath, force=self.force_read)

                    # if e.[0].startswith("File is missing \\'DICM\\' marker. Use force=True to force reading")
                except Exception as e:
                    if teil != self.dicomdir_filename:
                        # print('Dicom read problem with file ' + filepath)
                        logger.info('Dicom read problem with file ' + filepath)
                        import traceback
                        logger.debug(traceback.format_exc())
            if hasattr(dcmdata, "DirectoryRecordSequence"):
                # file is DICOMDIR - metainfo about files in directory
                # we are not using this info
                dcmdata = None

            if dcmdata is not None:
                metadataline = _prepare_metadata_line(dcmdata, teil)
                files.append(metadataline)

        # if SliceLocation is None, it is sorted to the end
        # this is not necessary it can be deleted
        files.sort(key=lambda x: (x['SliceLocation'] is None, x["SliceLocation"]))

        dcmdirplus = {'version': __version__, 'filesinfo': files, }
        if "StudyDate" in metadataline:
            dcmdirplus["StudyDate"] = metadataline["StudyDate"]
        return dcmdirplus
开发者ID:mjirik,项目名称:io3d,代码行数:47,代码来源:dcmreaddata.py


注:本文中的pydicom.read_file函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。