BenediktHeinrichs / metadataextractor

Extracting metadata from research data and providing it in an interoperable format.
https://metadataextractor.otc.coscine.dev/
MIT License
2 stars 1 forks source link

DicomExtract #8

Closed catgonzftw1 closed 4 months ago

catgonzftw1 commented 5 months ago

I attempted to add code that extracts from a DICOM file.

catgonzftw1 commented 5 months ago
def extract_dicom_metadata(found_files: list) -> dict:
    dicom_metadata = {}  # Initialize the metadata dictionary

    for file_name in found_files:
        if file_name.lower().endswith('.dcm'):
            ds = pydicom.dcmread(file_name)
            for elem in ds.iterall():
                if elem.tag.is_private:
                    continue  # Skip private tags
                tag_str = str(elem.tag)
                value = elem.value
                if isinstance(value, pydicom.valuerep.DSfloat):
                    value = float(value)
                dicom_metadata[tag_str] = value

    return dicom_metadata

dicom_metadata = extract_dicom_metadata(found_files)

I think the above is the pertinent code. It used the list of found files retrieved by scan_directory()

def scan_directory(directory: str, file_patterns: list[str]) -> list:
    file_list = []
    for pattern in file_patterns:
        for file in Path(directory).rglob(pattern):
            if file.is_file():
                file_list.append(file.as_posix())
    return file_list

directory = r"C:\Cat\10-19 CRC_1382\13 sarah_preclinical_data"  # Use 'r' prefix to treat backslashes as literal characters
file_patterns = ["*.dcm", "*.mct", "*.parameters"]  # Update with your desired patterns
found_files = scan_directory(directory, file_patterns)
print(found_files)
catgonzftw1 commented 5 months ago

@BenediktHeinrichs I made changes as you noted. Please let me know if it still needs something changed. I can give it another shot :)

catgonzftw1 commented 5 months ago

@BenediktHeinrichs I was able to edit those files and create pull requests for the unfinished changes. Please let me know if it looks correct :)

catgonzftw1 commented 5 months ago

@BenediktHeinrichs I moved all the changes into this pull request but when I ran the file it throws an error in the first line

from .IDataExtract import IDataExtract

The codespace suggested I make a virtual environment which I did but still get the import error:

ImportError: attempted relative import with no known parent package

catgonzftw1 commented 5 months ago

@BenediktHeinrichs I made some changes and tested it. I think it's working now.

BenediktHeinrichs commented 4 months ago

@catgonzftw1 extraction seems to work now. Is this what you envisoned?

image

If yes, just briefly notify me and I'll merge this.