Skip to content

Instantly share code, notes, and snippets.

View mjm522's full-sized avatar
💭
robotics

Michael J Mathew mjm522

💭
robotics
View GitHub Profile
@mjm522
mjm522 / fasten_for_loop.py
Created June 23, 2022 14:59
How to use multiprocessing Pool to parallely run a for loop in python with multiple arguments.
from multiprocessing import Pool
import numpy as np
import time
def gen_data(s, e, N):
q = np.linspace(s, e, N)
dq = np.linspace(s, e, N)*2
ddq = np.linspace(s, e, N)*3
time.sleep(0.5)
@mjm522
mjm522 / shared_numpy.py
Last active March 28, 2022 13:57
Following is a class that creates shared numpy via Linux Posix.
import ctypes
import mmap
import os
import stat
import numpy as np
from functools import reduce
from operator import mul
from typing import Any
rtld = ctypes.cdll.LoadLibrary("librt.so")
@mjm522
mjm522 / function.cpp
Created March 22, 2022 09:27
Wrapping a template-d function in pybind11
T* constructOrFindInstance(const std::string& instance_name)
{
auto result = mSegment.find<T>(instance_name.c_str());
if (result.first != nullptr) {
return result.first;
} else {
return mSegment.construct<T>(instance_name.c_str())();
}
}
@mjm522
mjm522 / two_d_array_compare.py
Last active January 10, 2022 21:46
Compare 2D arrays. Return list of indices containing the rows.
import numpy as np
def two_d_array_compare(array_a, array_b, thresh):
"""
this function does a pairwise comparision matching between array_b and array_a
it returns all the row numbers in array_a that has a similar entry in array_b
:param array_a: m x k
:param array_b: n x k
:thresh float difference
:return: row numbers of similar rows
@mjm522
mjm522 / parse_words.sh
Created November 18, 2021 15:49
A bash file to read line by line from a file and the split the line to words using space as a delimiter.
#!/usr/bin/bash
input="full path to file"
while IFS= read -r line; do
arrIN=(${line// / }) ## to split with ; arrIN=(${line//;/ })
## do something with the words, words can be obtained by ${arrIN[i]} where i is the index
done < "${input}"
@mjm522
mjm522 / draw_cuboid.py
Last active March 30, 2021 15:54
Draw Cuboid
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
def plot_cuboid(sample_cuboid):
fig = plt.figure(figsize=(12,9))
ax = fig.add_subplot(111, projection='3d')
for pair in sampling_cuboid['plot_pairs']:
start_point = sampling_cuboid['centre_point'] + sampling_cuboid[pair[0]]
end_point = sampling_cuboid['centre_point'] + sampling_cuboid[pair[1]]
@mjm522
mjm522 / histogram_with_outline.py
Last active March 29, 2021 14:16
Python normalised histogram with just outline
import numpy as np
import matplotlib.pyplot as plt
x_min = 0.51
x_max = 0.54
num_bins = 20
num_data=7000
x_mean = 0.53
plt.style.use("ggplot")
import trimesh
from numpy.linalg import inv
import numpy as np

def pretty_print(array):
    print(np.round(array, 2))

def change_centroid(mesh_path, save_path, additional_T=None):
 mesh = trimesh.load_mesh(mesh_path)
@mjm522
mjm522 / instructions.md
Created October 14, 2020 13:22
Build PlotJuggler in ROS2 (foxy + Ubuntu 20.04)

Step 1

Install Qt5 dependencies

sudo apt-get install qt5-default qttools-dev qttools5-dev-tools libqt5svg5-dev qtmultimedia5-dev

Step 2

sudo apt-get install ros-foxy-test-msgs

@mjm522
mjm522 / compress_pdf.bash
Created October 7, 2020 08:05
Bash function to compress pdf
function compress_pdf()
{
input_file=$1
output_file=$1_compressed.pdf
level=/$2
#levels: screen > ebook > printer > prepress,
gs -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=$level -sOutputFile=$output_file $input_file
echo "Saved in the same folder with name $output_file"
}