Skip to content

Instantly share code, notes, and snippets.

View rahulremanan's full-sized avatar

Rahul Remanan rahulremanan

View GitHub Profile
@rahulremanan
rahulremanan / Python_generators--DICOM_reader_generator--03.py
Last active March 31, 2023 16:16
A DICOM reader generator function
def dicom_generator(dicom_chunks):
i = 0
while True:
_inp = dicom_chunks[i]
_img = read_dicom(_inp)
yield _img
i += 1 # Exploiting the stateful nature of generators
@rahulremanan
rahulremanan / Python_generators--DICOM_reader--02.py
Created March 31, 2023 15:47
A DICOM reader helper function
def read_dicom(inp):
dicom = pydicom.dcmread(file_path)
image = dicom.pixel_array
if image.max() - image.min() > 0:
image = (image - image.min()) / (image.max() - image.min())
if dicom.PhotometricInterpretation == 'MONOCHROME1':
image = 1 - image
@rahulremanan
rahulremanan / Python_generators--Import_libraries--01.py
Last active March 31, 2023 15:57
Import libraries to handle DICOM files using Python generator function
import random, pydicom, numpy as np, tensorflow as tf
@rahulremanan
rahulremanan / Python_generators--Basic_example--00.py
Created March 31, 2023 15:40
A simple Python generator function
def f():
'''Modified PEP 255 example'''
try:
yield 1
try:
yield 2
1/0 # zero division to trigger ZeroDivisionError
yield 3 # never get here
except ZeroDivisionError:
yield 4
@rahulremanan
rahulremanan / named_tuples--tf_function--04.py
Created February 28, 2023 16:38
Performance optimization through a decorated Tensorflow function
@tf.function
def run_named_tuple_graph():
_ijk0 = (tf.constant(0), Pair(tf.constant(1), tf.constant(2)))
_ijk_fin = tf.while_loop(c, b, _ijk0)
return _ijk_fin
ijk_final = run_named_tuple_graph()
print(ijk_final[-1].j.numpy(), ijk_final[-1].k.numpy())
@rahulremanan
rahulremanan / named_tuples--tf_manipulate--03.py
Last active February 28, 2023 18:13
Using Tensorflow while_loop to manipulate the named tuples
Pair = collections.namedtuple('Pair', 'j, k')
ijk_0 = (tf.constant(0), Pair(tf.constant(1), tf.constant(2)))
c = lambda i, p: i < 10
b = lambda i, p: (i + 1, Pair((p.j + p.k), (p.j - p.k)))
ijk_final = tf.while_loop(c, b, ijk_0)
print(ijk_final[-1].j.numpy(), ijk_final[-1].k.numpy())
print(ijk_final[-1][0].numpy(), ijk_final[-1][1].numpy())
@rahulremanan
rahulremanan / named_tuples--manipulate--02.py
Created February 28, 2023 16:25
A simple Python for-loop to manipulate the named tuples
for i in range(10):
p = Pair((p.j + p.k), (p.j - p.k))
@rahulremanan
rahulremanan / named_tuples--create--01.py
Created February 28, 2023 16:22
Create a simple named tuple to represent x and y coordinates
Pair = collections.namedtuple('Pair', 'j k')
p = Pair(1.0, 2.0)
@rahulremanan
rahulremanan / named_tuples--import_lib--00.py
Created February 28, 2023 16:14
Import libraries for working with named tuples
import collections, tensorflow as tf
@rahulremanan
rahulremanan / endpoint_server_cert--verify.py
Created January 31, 2023 15:48
Verify the endpoint server certificate against the CA certificate chain
!openssl verify -CAfile /root/ca/intermediate/certs/ca-chain.cert.pem \
/root/ca/user/user.cert.pem