<jupyter_start><jupyter_text>**Mounting Google Drive**<jupyter_code>from google.colab import drive
drive.mount('/content/drive')<jupyter_output>Drive already mounted at /content/drive; to attempt to forcibly remount, call drive.mount("/content/drive", force_remount=True).<jupyter_text>**Load packages**<jupyter_code>import numpy, shutil, os, nibabel
import sys, getopt
import argparse
import imageio<jupyter_output><empty_output><jupyter_text>**Data source and Destination paths**<jupyter_code>input_path = '/content/drive/MyDrive/Radiology_workshop_monjoy/data_files/Normal/'
rotation_angle = 90
save_pngs_path = '/content/drive/MyDrive/Radiology_workshop_monjoy/save_pngs_path/Normal/'<jupyter_output><empty_output><jupyter_text>**Get list of nii or nii.gz source files**<jupyter_code>slice_counter = 0
source_files = os.listdir(input_path)
#identify sample ids and get source ids 
source_ids = [files[0:10] for files in source_files if files.endswith('.nii.gz')]
sample_ids = list(set(source_ids))
sample_ids
for file in sample_ids:
    fname = os.path.basename(file)
    print(fname)
    image_array = nibabel.load(input_path + fname+'.nii.gz').get_data()
    print(len(image_array.shape))
    # set destination folder
    if not os.path.exists(save_pngs_path+'/'+fname):
        os.makedirs(save_pngs_path+'/'+fname)
        print("Created ouput directory: " + save_pngs_path+'/'+fname)
    
    # For 3D image    
    if len(image_array.shape) == 3:
        nx, ny, nz = image_array.shape
        total_slices = image_array.shape[2]
        print(total_slices)
        # iterate through slices
        for current_slice in range(0, total_slices):
            # alternate slices
            if (slice_counter % 1) == 0:
                # rotate or no rotate
                if rotation_angle == 90:
                    data = numpy.rot90(image_array[:, :, current_slice])
                elif rotation_angle == 180:
                    data = numpy.rot90(numpy.rot90(image_array[:, :, current_slice]))
                elif rotation_angle == 270:
                    data = numpy.rot90(numpy.rot90(numpy.rot90(image_array[:, :, current_slice])))
                 #alternate slices and save as png
                if (slice_counter % 1) == 0:
                    print('Saving image...')
                    image_name = fname[:-4] + "_z" + "{:0>3}".format(str(current_slice+1))+ ".png"
                    imageio.imwrite(save_pngs_path+fname+'/'+image_name, data)
                    print('Saved.')
                    #move images to folder
                    print('Moving image...')
                    src = image_name
                    slice_counter += 1
                    print('Moved.')
                    print('Finished converting images')<jupyter_output>study_0247
{'fraction_non_alphanumeric': 0.11697165410836025, 'fraction_numerical': 0.015428776462145676, 'mean_word_length': 2.697612732095491, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 16, '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': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '401841', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 933, 'n_tokens_neox': 870, 'n_words': 173, 'path': 'nii2png.ipynb', 'repo_name': 'monjoybme/COVIDReport', 'size': '2787'}