Download as odt, pdf, or txt
Download as odt, pdf, or txt
You are on page 1of 2

EXAMPLE OF CODE

import os
import pandas as pd
import glob

# Read data from the Excel file and extract the first 8 characters from 'PATIENT ID'
data = pd.read_excel('/home/emth/Desktop/costa/CORYS_orig.xlsx', usecols='B, G')
ids_from_excel = data['PATIENT ID '].str.replace(' ', '').str[:8]

# List of image directories from 1 to 41


base_directory = '/home/emth/Desktop/costa/DICOM/'
image_directories = [os.path.join(base_directory, str(i)) for i in range(1, 42)]

# Find matches between image files and Excel data based on the first 8 characters of IDs
matched_file = []
for image_directory in image_directories:
image_files = glob.glob(os.path.join(image_directory, '*.dcm'))
image_ids_trimmed = [os.path.splitext(os.path.basename(image_file))[0][:8] for image_file
in image_files]

for image_file, image_id in zip(image_files, image_ids_trimmed):


if image_id in ids_from_excel.values:
matched_file.append((image_id, image_file))

# Create a DataFrame from the matched_file list


matched_file_df = pd.DataFrame(matched_file, columns=['Image ID', 'File Path'])

# Print the results and save to an Excel file


print("Patient_ID are:", ids_from_excel.values)
print("Total Matches Found:", len(matched_file))
print(matched_file_df)
# Check the current working directory
print(os.getcwd())

#Saving matched dataframe into a specific directory


matched_file_df.to_excel('/home/emth/Documents/pythonProject2/matched_files.xlsx',
index=False)

You might also like