Skip to content

Instantly share code, notes, and snippets.

View KrisYu's full-sized avatar

Xue Yu KrisYu

View GitHub Profile
@KrisYu
KrisYu / pca.py
Created December 8, 2021 14:27
pca to fit plane
# https://stackoverflow.com/questions/38754668/plane-fitting-in-a-3d-point-cloud
def PCA(data, correlation = False, sort = True):
""" Applies Principal Component Analysis to the data
Parameters
----------
data: array
The array containing the data. The array must have NxM dimensions, where each
of the N rows represents a different individual record and each of the M columns
@KrisYu
KrisYu / cmd_parse.py
Created November 17, 2021 20:19
command line parse
# https://stackoverflow.com/questions/15753701/how-can-i-pass-a-list-as-a-command-line-argument-with-argparse
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('-i', '--input',type = str, default = "sample.json")
parser.add_argument( "-l","--list", nargs="*", type=int, default=[0,1,2])
args = parser.parse_args()
@KrisYu
KrisYu / read_json.py
Created November 17, 2021 15:38
read json file
def read_data_from_file( filename ):
with open(filename, 'r') as json_file:
data = json.load(json_file)
return data
@KrisYu
KrisYu / distance_from_array_to_point.py
Created November 2, 2021 00:31
distance from one point to array of points
# https://stackoverflow.com/questions/48609781/distance-between-one-point-and-rest-of-the-points-in-an-array/48610147
from scipy.spatial import distance
X = [(35.0456, -85.2672)]
coords = [(35.1174, -89.9711),
(35.9728, -83.9422),
(36.1667, -86.7833)]
distance.cdist(X, coords, 'euclidean')
array([[ 4.70444794, 1.6171966 , 1.88558331]])
@KrisYu
KrisYu / delete_file.py
Last active November 1, 2021 20:00
delete some files in a path
import os
files = os.listdir()
# files is a string list
for filename in files:
print(filename)
# https://askubuntu.com/questions/443830/delete-all-files-whose-filenames-contain-a-particular-string
@KrisYu
KrisYu / plot_3d.py
Created November 1, 2021 17:05
plot 3d points
# plot 3d points
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure(figsize=(4,4))
ax = fig.add_subplot(111, projection='3d')
# points np array
@KrisYu
KrisYu / read.py
Last active November 1, 2021 17:07
read and write file
with open('filename') as f:
lines = f.readlines()
with open('write','w') as f:
f.write('Hello\n')
@KrisYu
KrisYu / rotate.py
Last active October 25, 2021 20:00
Rotate point about another point in degrees python
#https://stackoverflow.com/questions/34372480/rotate-point-about-another-point-in-degrees-python
import numpy as np
def rotate(p, origin=(0, 0), degrees=0):
angle = np.deg2rad(degrees)
R = np.array([[np.cos(angle), -np.sin(angle)],
[np.sin(angle), np.cos(angle)]])
# atleast_2d View inputs as arrays with at least two dimensions.
o = np.atleast_2d(origin)
@KrisYu
KrisYu / oobb.py
Last active February 23, 2024 01:01
oriented bounding box for points
#!/usr/bin/env python3
# https://stackoverflow.com/questions/32892932/create-the-oriented-bounding-box-obb-with-python-and-numpy
def oobb(pts):
'''
given:
pts, 2d or 3d
return:
corners, center of the oobb
'''
@KrisYu
KrisYu / Eigen Cheat sheet
Created September 25, 2020 15:25 — forked from gocarlos/Eigen Cheat sheet
Cheat sheet for the linear algebra library Eigen: http://eigen.tuxfamily.org/
// A simple quickref for Eigen. Add anything that's missing.
// Main author: Keir Mierle
#include <Eigen/Dense>
Matrix<double, 3, 3> A; // Fixed rows and cols. Same as Matrix3d.
Matrix<double, 3, Dynamic> B; // Fixed rows, dynamic cols.
Matrix<double, Dynamic, Dynamic> C; // Full dynamic. Same as MatrixXd.
Matrix<double, 3, 3, RowMajor> E; // Row major; default is column-major.
Matrix3f P, Q, R; // 3x3 float matrix.