Skip to content

Instantly share code, notes, and snippets.

View rahulremanan's full-sized avatar

Rahul Remanan rahulremanan

View GitHub Profile
@rahulremanan
rahulremanan / pickle.py
Last active July 31, 2021 13:40
Managing memory utilization using Pickle
#===Import dependencies===
import gc
import pickle
import psutil
import numpy as np
import pandas as pd
#===Function to track memory usage===
def memory_utilization():
print('Current memory utilization: {}% ...'.format(psutil.virtual_memory().percent))
@rahulremanan
rahulremanan / pickle_dependencies.py
Created December 30, 2020 08:41
Pickle memory utilization example dependencies
import gc
import pickle
import psutil
import numpy as np
import pandas as pd
@rahulremanan
rahulremanan / dicom.py
Last active August 23, 2021 03:55
Handling DICOM X-Ray images
import pydicom, numpy as np
from pydicom.pixel_data_handlers.util import apply_voi_lut
def readDICOM(path, optimize_display=True, monochrome_correction=True):
dcm = pydicom.read_file(path)
dcm_arr = dcm.pixel_array
if optimize_display:
dcm_arr = apply_voi_lut(dcm_array, dcm)
@rahulremanan
rahulremanan / subprocess.py
Last active August 8, 2021 10:35
Shell commands in Python using subprocess
import subprocess
def execute_in_shell(command, verbose=False):
"""
command -- keyword argument, takes a list as input
verbsoe -- keyword argument, takes a boolean value as input
This is a function that executes shell scripts from within python.
Keyword argument 'command', should be a list of shell commands.
@rahulremanan
rahulremanan / twitter_saliency_filter_analysis--01.py
Last active August 30, 2021 19:39
Twitter saliency filter analysis -- Part 01: Installation
import logging
from pathlib import Path
logging.basicConfig(level=logging.ERROR)
BIN_MAPS = {"Darwin": "mac", "Linux": "linux"}
HOME_DIR = Path("../").expanduser()
try:
import google.colab
@rahulremanan
rahulremanan / twitter_saliency_filter_analysis--02.py
Created August 30, 2021 19:45
Import dependencies to run the saliency filter evaluation tool
import os,gc,json,glob,shlex,random,platform,warnings,subprocess,numpy as np, \
pandas as pd,matplotlib.pyplot as plt,matplotlib.image as mpimg
from PIL import Image
from tqdm.auto import tqdm
from scipy.stats import wilcoxon
from collections import namedtuple
from IPython.display import display
from matplotlib.patches import Rectangle
from image_manipulation import join_images
img_dir = './'
if IN_COLAB:
from google.colab import drive
drive.mount('/content/drive')
img_dir = '/content/drive/MyDrive/'
fairface_dir = f'{img_dir}/FairFace/'
if not os.path.exists(f'{fairface_dir}/fairface-img-margin125-trainval.zip'):
raise ValueError(f'Please check whether the FairFace dataset zip file exists at: {fairface_dir}/fairface-img-margin125-trainval.zip')
if not os.path.exists(f'{fairface_dir}/fairface_label_train.csv'):
raise ValueError(f'Please check whether the FairFace data labels csv file exists at: {fairface_dir}/fairface_label_train.csv')
@rahulremanan
rahulremanan / twitter_saliency_filter_analysis--04.py
Created August 30, 2021 19:59
Helper functions to handle the FairFace dataset
def random_imgID_generator(df, pairs=True):
num_images = len(df)
id1 = random.SystemRandom().choice(range(0,num_images))
if pairs:
id2 = random.SystemRandom().choice(range(0,num_images))
return id1, id2
return id1
def eval_conditions(df, id1, id2):
id_condition = id1 == id2
unzip_dir = str(data_dir.absolute())
fairface_data = f'{fairface_dir}/fairface-img-margin125-trainval.zip'
@rahulremanan
rahulremanan / twitter_saliency_filter_analysis--06.py
Created August 30, 2021 20:11
Perform basic checks on the FairFace dataset
img_labels = pd.read_csv(fairface_data_checks(fairface_data))
img_labels.head()
num_images = len(img_labels)
print(f'Total number of FairFace images: {num_images}')