Skip to content

Instantly share code, notes, and snippets.

@bparaj
bparaj / forward-ssh-agent-to-docker-container.md
Created February 14, 2023 10:11
How to share ssh agent with a docker container

You might want to ssh into a docker container and pull some remote repository which requires the SSH keys stored on the host machine. Edit your docker-compose file as follows:

  1. Set the environment variable SSH_AUTH_SOCK to the socket being used by the agent.
  2. Mount that agent in the container.
services:
  app:
 environment:
@bparaj
bparaj / iou_visualization.py
Last active April 22, 2021 20:50
Script to compute and visualize intersection over union (iou) for example rectangle pairs. Uses matplotlib.
import matplotlib.pyplot as plt
from matplotlib.patches import Rectangle
def get_iou(b1, b2):
b1_x, b1_y, b1_w, b1_h = b1
b2_x, b2_y, b2_w, b2_h = b2
# Assume b1 is the bottom-left-most rectangle.
assert b1_x <= b2_x and b1_y <= b2_y
@bparaj
bparaj / install_python_libraries_apple_silicon_m1.md
Created April 1, 2021 22:16
Install scikit-learn within a virtual Python environment on Mac (Apple Silicon M1)

Issue

I was trying to install scikit-learn with:

python3 -m pip install scikit-learn

but it failed with a ton of errors while dealing with numpy as a dependency. Main error:

@bparaj
bparaj / using_tmux.md
Last active October 2, 2020 14:01
Basic commands for using tmux

Start a new tmux session with a name

tmux new -s mysessionname

Detach from the current session

  1. While pressing Ctrl, hit b and release both.
  2. Hit d.

List available tmux sessions

@bparaj
bparaj / check_s3_bucket_access.py
Created July 2, 2020 16:06
Check for read/write permission on a list of s3 bucket names. Use boto3.
import boto3
# Build your list of bucket names.
s3_list = ["abc", "xyz", "my-dev-bucket"]
s3 = boto3.client('s3')
for i, bkt in enumerate(s3_list):
# Try reading first.
try:
@bparaj
bparaj / physical_cpu_core_count.py
Created May 6, 2020 03:46
Count number of physical cpu cores to determine the number of parallel processes when using multiprocessing module in Python
import re
# Assume a system where /proc/cpuinfo exists. We will just parse this file with re module.
cpu_info = open("/proc/cpuinfo").read()
# Pattern of the line in "/proc/cpuinfo" which has the needed count:
# cpu cores : 12
patc = re.compile(r"cpu cores\s:\s(\d+)")
# Search and extract.
@bparaj
bparaj / copy_mysql_database_from_windows_to_linux
Created April 26, 2019 20:32
Create a new mysql database in a Linux server from the mysql database dump obtained from Windows server. This need arose when I wanted to run tests on a snapshot of the production database.
1. Create a MySQL dump with `mysqldump.exe`::
mysqldump.exe --single-transaction -u user -p password -h localhost db_name > db_name_dump.sql
2. Transfer `db_name_dump.sql` to your Linux machine
3. Assume you are now in a Linux machine. `db_name_dump.sql` may not be encoded in utf-8.
$ # Find out what encoding the file has from the terminal.
$ file -i db_name_dump.sql
@bparaj
bparaj / cluster_images_with_pca.py
Last active December 4, 2021 20:12
Cluster images with kmeans after dimension reduction with PCA. Use Python, OpenCV and scikit-learn.
import cv2
import numpy as np
from sklearn.decomposition import IncrementalPCA
from sklearn.cluster import KMeans
import glob
import re
from collections import defaultdict
import sys
1. When drawing links from source to target, make sure that the link simply touches the target.
This will make the arrow-head at the end of the link visible.
.attr("x1", function(d) { return d.source.x_ * width + nodeW / 2 ; })
.attr("y1", function(d) { return d.source.y_ * height + nodeH / 2 ; })
.attr("x2", function(d) { return d.target.x_ * width + nodeW / 2 + nodeW / 2 * Math.sign(d.source.x_ - d.target.x_); })
.attr("y2", function(d) { return d.target.y_ * height + nodeH / 2 + nodeH / 2 * Math.sign(d.source.y_ - d.target.y_); })
2. If you want to insert text in a rectangle, the text and the rect elements should be siblings. https://stackoverflow.com/a/5450269
To keep the text within the rect, specify the following attributes:
@bparaj
bparaj / example.prototxt
Last active February 7, 2018 23:10
Example Caffe layers
# 1.
# Flip the label values. Change 0's to 1's and viceversa.
# PowerLayer computes outputs y = (shift + scale * x) ^ power
layer {
name: "flip_label"
type: "Power"
bottom: "label"
top: "flip_label"
power_param {
power: 1