Skip to content

Instantly share code, notes, and snippets.

🎯
Focusing

Keiichi Kuroyanagi Keiku

🎯
Focusing
View GitHub Profile
@Keiku
Keiku / get_filename_including_parent_dir.py
Created Jul 10, 2020
Get file name including parent folder.
View get_filename_including_parent_dir.py
import pathlib
path = pathlib.Path('home/path/to/file.txt')
root = path.parent.parent
# PosixPath('home/path')
path.relative_to(root)
# PosixPath('to/file.txt')
@Keiku
Keiku / insightface_demo.py
Created Jul 2, 2020
InsightFace demo for face detection
View insightface_demo.py
# Reference: 1. Getting Started with Pre-trained Model from RetinaFace — insightface 0.1.5 documentation http://insightface.ai/build/examples_face_detection/demo_retinaface.html
# How to install insightface
# pip install insightface
# pip install mxnet-cu100
import insightface
import urllib
import urllib.request
import cv2
import numpy as np
@Keiku
Keiku / read_column_containing_list.py
Created Jun 28, 2020
Read a column containing a list as a list
View read_column_containing_list.py
# Reference: python - Reading csv containing a list in Pandas - Stack Overflow https://stackoverflow.com/questions/20799593/reading-csv-containing-a-list-in-pandas
# Copy this text.
"""
HK,"[u'5328.1', u'5329.3', '2013-12-27 13:58:57.973614']"
HK,"[u'5328.1', u'5329.3', '2013-12-27 13:58:59.237387']"
HK,"[u'5328.1', u'5329.3', '2013-12-27 13:59:00.346325']"
"""
import ast
@Keiku
Keiku / concat_strings_in_all_combinations.py
Created Jun 28, 2020
Concatenate strings in all combinations
View concat_strings_in_all_combinations.py
import itertools
['%s%s' % (x, y) for x, y in itertools.product(['a', 'b'], ['1', '2'])]
# ['a1', 'a2', 'b1', 'b2']
@Keiku
Keiku / remove_s3_files_before_specified_last_update_time.sh
Last active Jun 26, 2020
Remove S3 files before the specified last update time
View remove_s3_files_before_specified_last_update_time.sh
# Remove S3 files before the specified last update time
# Reference: amazon s3 - aws cli s3 bucket remove object with date condition - Stack Overflow https://stackoverflow.com/questions/51375531/aws-cli-s3-bucket-remove-object-with-date-condition
aws s3 ls --recursive s3://path/to/ | awk '$1 < "2020-06-25 12:00:00" {print $4}' | xargs -n1 -t -I 'KEY' aws s3 rm s3://path/to/'KEY'
View get_image_paths.py
import pathlib
# get image paths list in a directory
image_dir = pathlib.Path('images').resolve()
exts = ['.jpg', '.png']
image_paths = [path for path in image_dir.rglob('*') if path.suffix.lower() in exts]
# include parent directory
image_paths = [pathlib.Path(*path.parts[-2:]).as_posix() for path in image_dir.rglob('*') if path.suffix.lower() in exts]
@Keiku
Keiku / reset_seaborn_settings.py
Created Jun 9, 2020
Reset the seaborn setting once set.
View reset_seaborn_settings.py
# Reset the seaborn setting once set. It can be used in the middle of a notebook.
# Reference: python seaborn to reset back to the matplotlib - Stack Overflow https://stackoverflow.com/questions/26899310/python-seaborn-to-reset-back-to-the-matplotlib
# Either of the following may be used
# in matplotlib
import matplotlib as mpl
mpl.rcParams.update(mpl.rcParamsDefault)
# in seaborn
@Keiku
Keiku / convert_number_strings.py
Created Jan 31, 2017
Convert number strings with commas in pandas DataFrame to float.
View convert_number_strings.py
import pandas as pd
import locale
from locale import atof
df = pd.DataFrame([['1,200', '4,200'], ['7,000', '-0.03'], ['5', '0']])
# 0 1
# 0 1,200 4,200
# 1 7,000 -0.03
# 2 5 0
locale.setlocale(locale.LC_NUMERIC, '')
@Keiku
Keiku / roc_auc.py
Last active Oct 11, 2019
Plot ROC curve.
View roc_auc.py
import matplotlib.pyplot as plt
from sklearn.metrics import roc_curve, auc
import seaborn as sns
sns.set('talk', 'whitegrid', 'dark', font_scale=1.5, font='Ricty',
rc={"lines.linewidth": 2, 'grid.linestyle': '--'})
fpr, tpr, _ = roc_curve([1, 0, 1, 0, 1, 0, 0], [0.9, 0.8, 0.7, 0.7, 0.6, 0.5, 0.4])
roc_auc = auc(fpr, tpr)
@Keiku
Keiku / read_copytext.py
Created Jan 19, 2018
Read copy text to pandas DataFrame.
View read_copytext.py
import pandas as pd
from io import StringIO
def read_copytext(text):
text1 = StringIO(text)
df = pd.read_table(text1)
df.columns = ["col1"]
df["col1"] = df["col1"].str.replace("\s+", ",")
You can’t perform that action at this time.