python - How do I export dicom datasets to excel? -


i still quite new coding, , have couple of questions. working on few mri images file extension '.dcm'. imported 'dicom' module allows me extract specific parameters (such patients name age, type of scan, etc.) file. these values written notepad(values tab separated) , exported onto excel.

the first feature wanted add script being able search in subfolders files have '.dcm' extension , being able open each of them in script , extract information need. of right now, have such looks '.dcm' files in current directory. if use code below, can file names subfolders, when try open them using built in 'dicom.read_file()' method, give me error file not located. there way around that?

my_list= [] root, dirs, files in os.walk(path):  names in files:   if names.endswith(".dcm"):    my_list.append(names) 

secondly, how improve efficiency of code. have lot of recurring statements, when writing values notepad. there better/faster way it? else can improve on?

lastly, instead of exporting values need notepad , excel, there way directly export them excel?

for in range(len(my_list)):            ds = dicom.read_file(my_list[i])     if ds.seriesdescription not in series:          info = {}         info['patientname']=ds.patientname          info['seriesdescription']=ds.seriesdescription         series.append(ds.seriesdescription)         getrepetitiontime(ds)         getechotime(ds)         getinversiontime(ds)         getnumberofaverages(ds)         getspacingbetweenslices(ds)         getpercentsampling(ds)         getpercentphasefieldofview(ds)         getacquisitionmatrix(ds)         getflipangle(ds)         getimagesinacquisition(ds)         getpixelspacing(ds)         f.write(info['patientname'])         f.write("\t")         f.write(info['seriesdescription'])         f.write("\t")         f.write(info['repetitiontime'])         f.write("\t")         f.write(info['echotime'])         f.write("\t")         f.write(info['inversiontime'])         f.write("\t")         f.write(info['numberofaverages'])         f.write("\t")         f.write(info['spacingbetweenslices'])         f.write("\t")         f.write(info['percentsampling'])         f.write("\t")         f.write(info['percentphasefieldofview'])         f.write("\t")         f.write(info['acquisitionmatrix'])         f.write("\t")         f.write(info['flipangle'])         f.write("\t")         f.write(info['imagesinacquisition'])         f.write("\t")              f.write(info['pixelspacing'])         f.write("\n") 

might need adjustments dont have dcm files test, can idea:

import xlsxwriter import os import dicom   dcm_files = [] root, dirs, files in os.walk(path):     names in files:         if names.endswith(".dcm"):             dcm_files.append(os.path.join(root, names))  dcm_file in dcm_files:     ds = dicom.read_file(dcm_file)     workbook = xlsxwriter.workbook(os.path.basename(dcm_file) + '.xlsx')     worksheet = workbook.add_worksheet()      data = (             ["repetitiontime", ds.get("repetitiontime", "none")],             ["echotime", ds.get("echotime", "none")],             .             .             .             )      row = 0     col = 0      name, value in (data):         worksheet.write(row, col,     name)         worksheet.write(row, col + 1, value)         row += 1      workbook.close() 

Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -