4Quant / LungStageAnnotator

The documentation and support for the LungStageAnnotator tool
http://4quant.com/LungStageAnnotator
2 stars 0 forks source link

Add DICOM headers to LungStageAnnotator file #76

Closed kmader closed 7 years ago

kmader commented 7 years ago

Having a copy of the headers would make checking and fixing problems later easier

kmader commented 7 years ago

Added a new function, so the headers can be cleanly extracted and then saved as JSON without breaking the current serialization. They are in the fields pet_header and ct_header

def _extract_jsonsafe_header(series_id):
    # type: (str) -> List[Tuple[Tuple[long, long], str, Any]]
    """
    Extracts a json-safe header from the dicom
    :param series_id:
    :return:
    """
    from dicom import read_file  # dont use the DICOM Database this works better
    from dicom.datadict import CleanName # get the cleanname from a tag
    series_files = slicer.dicomDatabase.filesForSeries(series_id)
    assert len(series_files) > 0, "No files found for series {}".format(series_id)
    n_tags = read_file(series_files[0], stop_before_pixels=True)
    out_tags = [] # type: List[Tuple[Tuple[long, long], str, Any]]
    for k,v in n_tags.items():
        try:
            full_name = CleanName(k)
        except Exception as e:
            logging.info("DICOM tag {} does not have a nice name: {}".format(k, e))
            full_name = ''
        try:
            _ = json.dumps(v)
            new_v = v
        except Exception as e:
            logging.info("DICOM tag {} cannot be converted because: {}".format(k, e))
            new_v = str(v)
        out_tags += [((k.group, k.elem), full_name, new_v)]
    return out_tags