Example #1
0
def parseString(inString, silence=False):
    if sys.version_info.major == 2:
        from StringIO import StringIO as IOBuffer
    else:
        from io import BytesIO as IOBuffer
    parser = None
    doc = parsexml_(IOBuffer(inString), parser)
    rootNode = doc.getroot()
    rootTag, rootClass = get_root_tag(rootNode)
    if rootClass is None:
        rootTag = 'stringsType'
        rootClass = stringsType
    rootObj = rootClass.factory()
    rootObj.build(rootNode)
    # Enable Python to collect the space used by the DOM.
    doc = None
    if not silence:
        sys.stdout.write('<?xml version="1.0" ?>\n')
        rootObj.export(
            sys.stdout,
            0,
            name_=rootTag,
            namespacedef_=
            'xmlns:tns="https://leoschwarz.github.io/opensim-protocol/schema/StringsSchema.xsd"'
        )
    return rootObj
Example #2
0
def decompress_case(archive_path, debug=False):
    """Decompress a archive in memory.

    Args:
        archive_path (str): The filepath of the archive
        debug (bool): To print debug information

    Returns:
        slices (list): The list of decompressed files

    """
    mode = ''
    if archive_path.lower().endswith('.gz') or archive_path.lower().endswith(
            '.tgz'):
        mode = 'r:gz'
    elif archive_path.lower().endswith(
            '.bz2') or archive_path.lower().endswith('.tbz'):
        mode = 'r:bz2'
    tar_archive = tarfile.open(archive_path, mode)
    slices = []
    if debug:
        print("Untar...")

    start = time.time()

    while True:
        tarinfo = tar_archive.next()
        if tarinfo is None:
            break
        if tarinfo.isfile():
            file_object = tar_archive.extractfile(tarinfo)
            file_like_object = IOBuffer(file_object.read())
            file_object.close()
            file_like_object.seek(0)
            try:
                dcm = pydicom.read_file(file_like_object)
                slices.append(dcm)
            except:
                continue

    end_untar = time.time()
    if debug:
        print("Untar time: {}".format(end_untar - start))
    return slices
Example #3
0
def parseString(inString, silence=False):
    if sys.version_info.major == 2:
        from StringIO import StringIO as IOBuffer
    else:
        from io import BytesIO as IOBuffer
    parser = None
    doc = parsexml_(IOBuffer(inString), parser)
    rootNode = doc.getroot()
    rootTag, rootClass = get_root_tag(rootNode)
    if rootClass is None:
        rootTag = 'image_descriptor'
        rootClass = image_descriptor
    rootObj = rootClass.factory()
    rootObj.build(rootNode)
    # Enable Python to collect the space used by the DOM.
    doc = None
    if not silence:
        sys.stdout.write('<?xml version="1.0" ?>\n')
        rootObj.export(sys.stdout, 0, name_=rootTag, namespacedef_='')
    return rootObj
Example #4
0
def peek_compressed_study(archive_path):
    """Peeks the `StudyInstanceUID` of a compressed DICOM archive, that is,
    reads the first DICOM file in the archive and returns the associated ID.

    Args:
        archive_path (str): The filepath of the archive.

    Returns:
        str or None: The StudyInstanceUID if possible, None otherwise.

    """
    mode = ''
    if archive_path.lower().endswith('.gz') or archive_path.lower().endswith(
            '.tgz'):
        mode = 'r:gz'
    elif archive_path.lower().endswith(
            '.bz2') or archive_path.lower().endswith('.tbz'):
        mode = 'r:bz2'
    try:
        tar_archive = tarfile.open(archive_path, mode)
    except:
        return None
    dcm = None
    while True:
        tarinfo = tar_archive.next()
        if tarinfo is None:
            break
        if tarinfo.isfile():
            file_object = tar_archive.extractfile(tarinfo)
            file_like_object = IOBuffer(file_object.read())
            file_object.close()
            file_like_object.seek(0)
            try:
                dcm = pydicom.read_file(file_like_object)
            except:
                continue
            break
    if dcm is not None:
        return dcm.StudyInstanceUID
    return None
Example #5
0
def parseString(inString, silence=False):
    if sys.version_info.major == 2:
        from StringIO import StringIO as IOBuffer
    else:
        from io import BytesIO as IOBuffer
    parser = None
    doc = parsexml_(IOBuffer(inString), parser)
    rootNode = doc.getroot()
    rootTag, rootClass = get_root_tag(rootNode)
    if rootClass is None:
        rootTag = 'carrierType'
        rootClass = carrierType
    rootObj = rootClass.factory()
    rootObj.build(rootNode)
    # Enable Python to collect the space used by the DOM.
    doc = None
##     if not silence:
##         sys.stdout.write('<?xml version="1.0" ?>\n')
##         rootObj.export(
##             sys.stdout, 0, name_=rootTag,
##             namespacedef_='xmlns:target="http://cars.example.com/schema"')
    return rootObj
Example #6
0
def write_reordered_traj(temp_inds, byte_inds, outtemps, temps,
                         frametuple_dict, nprod, writefreq, outtrajfns,
                         infobjs):
    """
    Reorders trajectories by temp. and writes them to disk
    
    :param temp_inds: list index of temps (in the list of all temps) for which 
                      reordered trajs will be produced on this proc.
    
    :param byte_inds: dict containing the (previously stored) byte indices
                      for each replica file (key = replica number)
    
    :param outtemps: list of all temps for which to produce reordered trajs.
    
    :param temps: list of all temps used in the REMD simulation.
    
    :param outtrajfns: list of filenames for output (ordered) trajs.
    
    :param frametuple_dict: dict containing a tuple (replica #, frame #) 
                            for each temp.
    
    :param nprod: number of production timesteps. 
                  Last (nprod / writefreq) frames 
                  from the end will be written to disk.
    
    :param writefreq: traj dump frequency in LAMMPS
    
    :param infobjs: list of file pointers to input (unordered) trajs.
    """

    nframes = int(nprod / writefreq)

    for n in temp_inds:
        # open string-buffer and file
        buf = IOBuffer()
        of = readwrite(outtrajfns[n], "wb")

        # get frames
        abs_temp_ind = np.argmin(abs(temps - outtemps[n]))
        frametuple = frametuple_dict[abs_temp_ind][-nframes:]

        # write frames to buffer
        if me == ROOT:
            pb = tqdm(frametuple,
                      desc=("Buffering trajectories for writing"),
                      leave=True,
                      position=ROOT + 2 * me,
                      unit='frame/replica',
                      unit_scale=True)

            iterable = pb
        else:
            iterable = frametuple

        for i, (rep, frame) in enumerate(iterable):
            infobj = infobjs[rep]
            start_ptr = int(byte_inds[rep][frame, 1])
            stop_ptr = int(byte_inds[rep][frame + 1, 1])
            byte_len = stop_ptr - start_ptr
            infobj.seek(start_ptr)
            buf.write(infobj.read(byte_len))
        if me == ROOT: pb.close()

        # write buffer to disk
        if me == ROOT: print("Writing buffer to file")
        of.write(buf.getvalue())
        of.close()
        buf.close()

    for i in infobjs:
        i.close()

    return
Example #7
0
def process_archive(archive_filepath, prefix=".", img_format="jpg"):
    logger = IOBuffer()
    archive_path = str(archive_filepath)

    case = dcmtools.decompress_case(archive_path)
    studyInstanceUID, study = dcmtools.load_study(case)

    if studyInstanceUID is None:
        logger.write(
            'No StudyInstanceUID found in filename {}'.format(archive_path))
        return

    folder = Path(prefix) / studyInstanceUID
    folder.mkdir(exist_ok=True)

    logger.write("Saving meta info: {}".format(folder))

    studies_meta = []

    for series in study:
        img = series[0]

        meta_info = {
            'Filename': str(archive_path),
            'StudyInstanceUID': str(img.StudyInstanceUID),
            'SeriesInstanceUID': str(img.SeriesInstanceUID),
            'PatientSex': str(img.PatientSex),
            'AcquisitionDate': int(img.AcquisitionDate),
            'SliceThicknessX': float(img.SliceThicknessX),
            'SliceThicknessY': float(img.SliceThicknessY),
            'SliceThicknessZ': float(img.SliceThicknessZ),
            'PatientAge': int(img.PatientAge[:-1]),
            'StudyDescription': str(img.StudyDescription),
            'SeriesDescription': str(img.SeriesDescription)
        }
        studies_meta.append(meta_info)

        logger.write(str(meta_info))

        voxels, spacing = dcmtools.clip_voxel_values(series)

        mid_slice_X = voxels[int(voxels.shape[0] // 2), :, :]
        mid_slice_Y = np.flipud(voxels[:, int(voxels.shape[1] // 2), :])
        mid_slice_Z = np.flipud(voxels[:, :, int(voxels.shape[2] // 2)])

        scipy.misc.imsave(
            folder / "{}-{}.{}".format(img.SeriesInstanceUID, "X", img_format),
            mid_slice_X)
        scipy.misc.imsave(
            folder / "{}-{}.{}".format(img.SeriesInstanceUID, "Y", img_format),
            mid_slice_Y)
        scipy.misc.imsave(
            folder / "{}-{}.{}".format(img.SeriesInstanceUID, "Z", img_format),
            mid_slice_Z)

    df_meta_info = pd.DataFrame(studies_meta)
    df_meta_info.to_csv(folder / "{}.{}".format(studyInstanceUID, "csv"))

    # rewind the tape
    logger.seek(0)
    return logger
Example #8
0
    # a list of all files to be processed
    archives_todo = []
    # traverse directory and find archives
    if path.is_dir():
        # grab the first DICOM image in the directory
        for (dirpath, dirnames, filenames) in os.walk(str(path)):
            for fname in filenames:
                fp = Path(dirpath) / fname
                if fp.suffix[1:] in archive_suffixes:
                    archives_todo.append(fp)
    elif path.is_file():
        # only one file given as input
        if path.suffix[1:] in archive_suffixes:
            archives_todo.append(path)

    logger = IOBuffer()
    # start worker processes
    with tqdm(desc="CTInfo", total=len(archives_todo), unit="file") as pbar:
        with Pool(processes=multiprocessing.cpu_count()) as pool:
            for log in tqdm(
                    pool.imap_unordered(
                        partial(process_archive, prefix=args.prefix),
                        archives_todo)):
                pbar.update(1)
                logger.write(log.getvalue())

    log_filep = Path(args.prefix) / "{}.log".format("MetaInfo")
    with open(log_filep, "w") as fp:
        # rewind the tape
        logger.seek(0)
        shutil.copyfileobj(logger, fp)