<jupyter_start><jupyter_text><jupyter_code>import os
import numpy as np
import matplotlib.pyplot as plt

#
# Read Data from google drive
#
from google.colab import drive #for loading gdrive data
from google.colab import files

# install dependencies not included by Colab
# use pip3 to ensure compatibility w/ Google Deep Learning Images 
!pip3 install -q pydicom 
!pip3 install -q tqdm 
!pip3 install -q imgaug 
!pip3 install -q SimpleITK
!pip3 install -q medpy

import SimpleITK as sitk  #for reading mha files
from medpy.io import load #mha files
import pydicom #to read dicom files




#
# Load data from google drive
#

drive.mount('/content/gdrive')



'''
GET FILE LISTING----------------------------------------------------------------
'''
def get_dirs(top_dir): 
    import os

    dir_list = os.scandir(top_dir)
    directories=[]
    file_list = {}
    for ii in dir_list:
        file_list[ii.name]=[]
        directories.append(ii.name)
        #get file listings for each directory found
        full_name = os.path.join(top_dir,ii.name)
        print(ii,full_name)
        if (os.path.isdir(full_name) == 1):
            print('dir found')
            files = os.scandir(full_name) #os.path.join(top_dir,ii.name))
        else:
            files = full_name
            print('-----  ',ii)
        
        for jj in files:
            file_list[ii.name].append(jj.name)


    
    return directories, file_list
### Main level
top_dir = '/content/gdrive/My Drive/DBT_DATA/'


upper_dir =top_dir
main_dir, flist = get_dirs(upper_dir)

for ii in main_dir:
    print(ii)
'''
for key_val in flist.keys():
    print(flist[key_val])
    new_upper = os.path.join(upper_dir, key_val,str(flist[key_val][0]))
    temp_dir,temp_list= get_dirs(new_upper)
    print('---- ', temp_dir)
 ###
 ### Build links to dcm files
 ###
 print('upper dir = ',upper_dir)
 counter = 0
 for root, directories, filenames in os.walk(upper_dir): 
    for directory in directories: 
        print('dir = ',os.path.join(root, directory))
    for filename in filenames:  
        print('f= ',os.path.join(root,filename))
        counter = counter + 1
        if (counter > 10):
            break
    if (counter > 10):
        break
### Open dcm file
###
from pydicom import dcmread
file = '/content/gdrive/My Drive/DBT_DATA/TEST_DATA/manifest-1605042674814/Breast-Cancer-Screening-DBT/DBT-P00118/01-01-2000-DBT-S05386-MAMMO screening digital bilateral-70928/7697.000000-17471/1-1.dcm'


ds = dcmread(file)
print(ds.PatientName)
ds.file_meta.TransferSyntaxUID
plt.imshow(ds.pixel_array[0,:,:], cmap=plt.cm.bone) 

np.shape(ds.pixel_array)
ds_slice = ds.pixel_array[0,:,:]
plt.imshow(ds_slice, cmap=plt.cm.jet)
plt.colorbar()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14113345521023765, 'fraction_numerical': 0.02522851919561243, 'mean_word_length': 2.8753541076487252, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '114002', 'license': 'MIT', 'n_tokens_mistral': 1021, 'n_tokens_neox': 951, 'n_words': 229, 'path': 'get_dbt_data.ipynb', 'repo_name': 'einsteinxx/UCLA_BE223C_SPRING_2021', 'size': '2735'}