Skip to content

Instantly share code, notes, and snippets.

View SeanSyue's full-sized avatar
🏔️
Nature Lover

Yuchen Xue SeanSyue

🏔️
Nature Lover
View GitHub Profile
@SeanSyue
SeanSyue / numpy_count_occurrence.py
Last active May 20, 2018 15:32
Count the occurrence of certain item in an ndarray in Python. Ref: https://stackoverflow.com/a/28663910/8809592
import numpy as np
a = np.array([0, 3, 0, 1, 0, 1, 2, 1, 0, 0, 0, 0, 1, 3, 4])
unique, counts = numpy.unique(a, return_counts=True)
print(dict(zip(unique, counts)))
#include <opencv2/opencv.hpp>
using namespace cv;
int main() {
Mat img = imread("image.jpg");
namedWindow("test", WINDOW_NORMAL);
imshow("test", img);
waitKey(0);
return 0;
}
@SeanSyue
SeanSyue / python_switch.py
Created August 12, 2018 02:50
Implement switch functionality in Python.
# https://bytebaker.com/2008/11/03/switch-case-statement-in-python/
def switch_(index_):
def zero():
print("You typed zero.\n")
def sqr():
print("n is a perfect square\n")
@SeanSyue
SeanSyue / first_module.py
Created August 12, 2018 03:05
Example on understanding "__name__ == '__main__'" in Python.
print("This will always be shown first\n")
def main():
print("Calling first_module's main method\n")
if __name__ == '__main__':
# the following will be shown only in `first_module.py`
main()
@SeanSyue
SeanSyue / zip_sorted.py
Created August 12, 2018 07:42
Combine `zip()` and `sorted()` to get a new long sorted list.
list1 = [3, 2, 4, 1, 1]
list2 = ['three', 'two', 'four', 'one', 'one2']
zipped = zip(list1, list2) # zip two lists
sorted_zip = sorted(zipped) # sort
sorted_zip_comb = zip(*sorted_zip) # unzip, then zip again
print(list(sorted_zip_comb))
@SeanSyue
SeanSyue / gen_bin_num.py
Created August 12, 2018 07:50
Example on how to generate binary numbers in Python.
from random import randint
from bitstring import BitArray
# `bin()` function explained
bin30 = bin(30)
print("--> bin30:\n", bin30)
print("--> type(bin30):\n", type(bin30))
print("--> int(bin30, 2):\n", int(bin30, 2))
# generate a long list of random binary numbers
@SeanSyue
SeanSyue / import_doc.py
Created August 12, 2018 07:59
A simple example on importing docstring in Python.
import mymodule
print(mymodule.__doc__)
print(mymodule.f.__doc__)
@SeanSyue
SeanSyue / yolo_gen_train_lst.py
Created August 14, 2018 02:56
Generate training list for darknet yolo training.
from pathlib import Path
for img_file in Path('./JPEGImages').iterdir():
with open('train.txt', 'w') as f:
print(img_file.resolve(), file=f)
@SeanSyue
SeanSyue / filecontents-sample.tex
Created March 13, 2019 07:08
file­con­tents and file­con­tents* en­vi­ron­ments en­able a LATEX source file to gen­er­ate ex­ter­nal files as it runs through LATEX. <https://ctan.org/pkg/filecontents?lang=en>
% Provide some test content
\begin{filecontents*}{\jobname.bib}
@article{Bao2015,
author = {Bao, Z. and Pan, G. and Zhou, W.},
doi = {10.1109/TIT.2015.2421894},
issue = {6},
journal = IEEE_J_IT,
pages = {3413-3426},
title = {Asymptotic Mutual Information Statistics of
{mimo} Channels and {CLT} of Sample Covariance Matrices},
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{cite}
\begin{document}
\nocite{*}
\bibliographystyle{IEEEtran}
\bibliography{IEEEabrv,ref}{}
% \bibliography{IEEEabrv,references}{}