def convert(input, output, transfer_syntax, item_length, use_group_length): header, data_set = odil.read(input) to_remove = [ "FileMetaInformationVersion", "MediaStorageSOPClassUID", "MediaStorageSOPInstanceUID", "TransferSyntaxUID", "ImplementationClassUID", "ImplementationVersionName" ] for name in to_remove: header.remove(getattr(odil.registry, name)) odil.write(data_set, output, header, transfer_syntax, item_length, use_group_length)
def convert_reconstruction(bruker_directory, series, reconstruction, iod_converter, transfer_syntax, destination, iso_9660): """ Convert and save a single reconstruction. :param bruker_directory: Bruker directory object :param series: series number in the Bruker directory :param reconstruction: reconstruction number in the series :param iod_converter: conversion function :param transfer_syntax: target transfer syntax :param destination: destination directory :param iso_9660: whether to use ISO-9660 compatible file names """ logger.info("Converting {}:{}".format(series, reconstruction)) bruker_binary = bruker_directory.get_dataset("{}{:04d}".format( series, int(reconstruction))) bruker_json = json.loads(bruker.as_json(bruker_binary)) logger.info("Found {}:{} - {} ({})".format( series, reconstruction, bruker_json.get("VisuAcquisitionProtocol", ["(none)"])[0], bruker_json.get("RECO_mode", ["none"])[0])) bruker_json["reco_files"] = list( bruker_directory.get_used_files("{}{:04d}".format( series, int(reconstruction)))) dicom_binaries = iod_converter(bruker_json, transfer_syntax) files = [] for index, dicom_binary in enumerate(dicom_binaries): if iso_9660: filename = "IM{:06d}".format(1 + index) else: filename = dicom_binary.as_string("SOPInstanceUID")[0] destination_file = os.path.join( destination, get_series_directory(dicom_binary, iso_9660), reconstruction, filename) if not os.path.isdir(os.path.dirname(destination_file)): os.makedirs(os.path.dirname(destination_file)) odil.write(dicom_binary, destination_file, transfer_syntax=transfer_syntax) files.append(destination_file) return files
def store(self, data_set): if layout == "flat": directory = "" elif layout == "tree": # Patient directory: <PatientName> or <PatientID>. patient_directory = None if "PatientName" in data_set and data_set.as_string( "PatientName"): patient_directory = data_set.as_string("PatientName")[0] else: patient_directory = data_set.as_string("PatientID")[0] # Study directory: <StudyID>_<StudyDescription>, both parts are # optional. If both tags are missing or empty, default to a # numeric index based on StudyInstanceUID. study_directory = [] if "StudyID" in data_set and data_set.as_string("StudyID"): study_directory.append(data_set.as_string("StudyID")[0]) if ("StudyDescription" in data_set and data_set.as_string("StudyDescription")): study_directory.append( data_set.as_string("StudyDescription")[0]) if not study_directory: study_instance_uid = data_set.as_string( "StudyInstanceUID")[0] study_directory.append( self._study_ids.setdefault(study_instance_uid, 1 + len(self._study_ids))) study_directory = "_".join(study_directory) # Study directory: <SeriesNumber>_<SeriesDescription>, both # parts are optional. If both tags are missing or empty, default # to a numeric index based on SeriesInstanceUID. series_directory = [] if "SeriesNumber" in data_set and data_set.as_int( "SeriesNumber"): series_directory.append( str(data_set.as_int("SeriesNumber")[0])) if ("SeriesDescription" in data_set and data_set.as_string("SeriesDescription")): series_directory.append( data_set.as_string("SeriesDescription")[0]) if not series_directory: series_instance_uid = data_set.as_string( "series_instance_uid")[0] series_directory.append( self._series_ids.setdefault(series_instance_uid, 1 + len(self._series_ids))) series_directory = "_".join(series_directory) if iso_9660: patient_directory = to_iso_9660(patient_directory) study_directory = to_iso_9660(study_directory) series_directory = to_iso_9660(series_directory) directory = os.path.join(patient_directory, study_directory, series_directory) if not os.path.isdir(os.path.join(self.directory, directory)): os.makedirs(os.path.join(self.directory, directory)) else: raise NotImplementedError() self.stored.setdefault(directory, 0) if iso_9660: filename = "IM{:06d}".format(1 + self.stored[directory]) else: filename = data_set.as_string("SOPInstanceUID")[0] odil.write(data_set, os.path.join(self.directory, directory, filename)) self.stored[directory] += 1 self.files.append(os.path.join(directory, filename))
def as_binary(input, output, transfer_syntax): _, data_set = odil.read(input) odil.write(data_set, output, transfer_syntax=transfer_syntax)
def store(self, data_set): if layout == "flat": directory = "" elif layout == "tree": # Patient directory: <PatientName> or <PatientID>. patient_directory = None if "PatientName" in data_set and data_set.as_string("PatientName"): patient_directory = data_set.as_string("PatientName")[0] else: patient_directory = data_set.as_string("PatientID")[0] # Study directory: <StudyID>_<StudyDescription>, both parts are # optional. If both tags are missing or empty, default to a # numeric index based on StudyInstanceUID. study_directory = [] if "StudyID" in data_set and data_set.as_string("StudyID"): study_directory.append(data_set.as_string("StudyID")[0]) if ("StudyDescription" in data_set and data_set.as_string("StudyDescription")): study_directory.append( data_set.as_string("StudyDescription")[0]) if not study_directory: study_instance_uid = data_set.as_string("StudyInstanceUID")[0] study_directory.append( self._study_ids.setdefault( study_instance_uid, 1+len(self._study_ids))) study_directory = "_".join(study_directory) # Study directory: <SeriesNumber>_<SeriesDescription>, both # parts are optional. If both tags are missing or empty, default # to a numeric index based on SeriesInstanceUID. series_directory = [] if "SeriesNumber" in data_set and data_set.as_int("SeriesNumber"): series_directory.append(str(data_set.as_int("SeriesNumber")[0])) if ("SeriesDescription" in data_set and data_set.as_string("SeriesDescription")): series_directory.append( data_set.as_string("SeriesDescription")[0]) if not series_directory: series_instance_uid = data_set.as_string("series_instance_uid")[0] series_directory.append( self._series_ids.setdefault( series_instance_uid, 1+len(self._series_ids))) series_directory = "_".join(series_directory) if iso_9660: patient_directory = to_iso_9660(patient_directory) study_directory = to_iso_9660(study_directory) series_directory = to_iso_9660(series_directory) directory = os.path.join( patient_directory, study_directory, series_directory) if not os.path.isdir(os.path.join(self.directory, directory)): os.makedirs(os.path.join(self.directory, directory)) else: raise NotImplementedError() self.stored.setdefault(directory, 0) if iso_9660: filename = "IM{:06d}".format(1+self.stored[directory]) else: filename = data_set.as_string("SOPInstanceUID")[0] odil.write( data_set, os.path.join(self.directory, directory, filename)) self.stored[directory] += 1 self.files.append(os.path.join(directory, filename))