Skip to content

Instantly share code, notes, and snippets.

View mjm522's full-sized avatar

Michael J Mathew mjm522

View GitHub Profile
@mjm522
mjm522 / A set of instructions to setup ROS Melodic with Conda.md
Last active December 10, 2024 10:11
Setting up nvdia docker with ros melodic with conda to use hardware acceleration

Steps

  1. Install docker using instructions here. This installation instructions were tested on Ubuntu 18.04 with Docker version 19.03.3, build a872fc2f86.

  2. To run docker as a non-root user, follow the post installation instruction here.

  3. Install nvidia toolkit:Reference

$ sudo apt-get update && sudo apt-get install -y nvidia-container-toolkit
@mjm522
mjm522 / Isolate CPU Ubuntu.md
Last active July 20, 2024 16:01
Isolate CPU core to run Python code Ubuntu

Note: To run a process dedicated on a CPU you can use the tasket command. For example

taskset -c 5,11 python -m timeit 'sum(range(10**7))'

However, this will not guarantee that the CPUs 5,11 will be used for that process alone. The same CPU can be interrupted by the scheduler and may not be properly isolated. So inorder to isolate the CPU the following steps are to be taken. Another feature that could be turned off for these CPUs are the interrupt feature 'IRQ'. However this is not covered here.

  1. List all available cores and hyperthreads in your processor.
lscpu --all --extended
@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)