Skip to content

Instantly share code, notes, and snippets.

View 338rajesh's full-sized avatar
😀

Rajesh Nakka 338rajesh

😀
View GitHub Profile
@338rajesh
338rajesh / gears_rotation_anim.py
Created June 21, 2024 15:05
It creates the animation for a pair of rotating gears
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
from os import path, makedirs
CSD = path.dirname(__file__)
def rotate_points(xy: np.ndarray, theta: float = 0.0, pivot_point: tuple[float, float] = (0.0, 0.0), ):
""" Rotate the points `x` and `y` by specified angle about the point (xc, yc). """
@338rajesh
338rajesh / movie_maker.py
Last active June 21, 2024 12:11
For making video from a series of images
from os import path, listdir
import cv2
import imageio
import argparse
from tqdm import tqdm
class MovieMaker:
""" Helper definitions for making videos """
@338rajesh
338rajesh / make_npy_arr.py
Created August 28, 2023 17:58
A simple python script for making a .npy file of images, given the directory of the images.
"""
It prepares and saves .npy array to the specified path.
"""
import os
import numpy as np
import sys
from PIL import Image
from tqdm import tqdm
import argparse
@338rajesh
338rajesh / remove_outliers_numpy.py
Created May 6, 2023 10:12
Remove outliers of a 2D numpy array, based on the columns data distribution.
import numpy as np
def remove_outliers(arr, col_indices=None, k=1.5):
"""
Removes outliers from the given 2D array using inter-quartile range.
arr: np.ndarray
a 2D numpy array
col_indices=[0,]
@338rajesh
338rajesh / shuffle_arrays_concurrently.py
Created May 3, 2023 03:55
Shuffles multiple arrays concurrently
import numpy as np
# shuffling arrays concurrently
def shuffle_arrays(*arr, order=1):
num_rows = arr[0].shape[0]
assert all([i.shape[0] == num_rows for i in arr])
indicies = np.array(list(range(num_rows)), dtype=np.int64)
@338rajesh
338rajesh / get_curve_chunks.py
Last active November 15, 2022 12:17
Get the chunks of data points on curve about a pivot value.
import numpy as np
import matplotlib.pyplot as plt
import os
from scipy import integrate
CFD = os.path.dirname(__file__)
def get_chunks(x: np.ndarray[float], y: np.ndarray[float], pivot: float = 0.0):
sort_indices = np.argsort(x)
@338rajesh
338rajesh / clip_histogram.py
Created August 27, 2022 16:54
It clips the histogram by minimum frequency
import numpy as np
import matplotlib.pyplot as plt
def clip_histogram(data, get_indices=False, **hist_kwargs):
n, bns = np.histogram(data, **hist_kwargs)
bin_pairs = [(bns[i], bns[i+1]) for i in range(len(bns)-1)]
new_data = []
indices = []
for (i, (bnl, bnu)) in enumerate(bin_pairs):
bin_width = bnu - bnl
import time
import os
class ProgressBar:
def __init__(self, num_iters, header=None) -> None:
self.t0 = time.time()
self.n = num_iters
self.term_cols = (os.get_terminal_size().columns)*0.25
import numpy as np
import matplotlib.pyplot as plt
from PIL import Image
import os
num_chunks = 6
img_size = (64, 64)
container_ID = "CIRCLES_VF10_RS10_R5"
cfd = os.path.dirname(__vsc_ipynb_file__)
container_dir = os.path.join(cfd, "containers", "rve", f"container_{container_ID}")
@338rajesh
338rajesh / attrb_on_root_h5.jl
Created July 4, 2022 12:07
Snippet for adding attributes on the root of h5 file in Julia
h5open(data_set_path, "w") do h5file_id
attributes(h5file_id)["eqrad"] = 1.0
attributes(h5file_id)["inclusion_volume_fraction"] = 32.0
attributes(h5file_id)["rve_xlb"] = bbox[1]
attributes(h5file_id)["rve_ylb"] = bbox[2]
attributes(h5file_id)["rve_xub"] = bbox[3]
attributes(h5file_id)["rve_yub"] = bbox[4]
end