Skip to content

Instantly share code, notes, and snippets.

View josepsmartinez's full-sized avatar

José Pedro Silveira Martinez josepsmartinez

View GitHub Profile
@josepsmartinez
josepsmartinez / label2attr.py
Created February 15, 2022 17:43
Converts CVAT annotation samples from *checkbox tags* to *object tag attributes*
import bs4
source_path = "annotations_source.xml"
output_xml_path = "annotations_out.xml"
LABELS = [
"no-category", "valid", "non-centered", "occluded", "spoof",
"rotated", "blurred"]
@josepsmartinez
josepsmartinez / exit_codes.sh
Last active February 14, 2022 21:42
Bash practices for handling exit codes (Reference: https://stackoverflow.com/a/45817972/4449273)
#!/bin/bash
# In case of simple logging
complex_command && echo "Success" >> log.txt || echo "Failure" >> log.txt
# In case of complex command chaining
complex_command
if [ $? -ne 0]; then
success_complex_command # this command could fail as well
# its exit code can be treated in a nested manner by testing $? again
@josepsmartinez
josepsmartinez / sudo-func-call.sh
Created April 16, 2021 15:43
Calling Bash functions with sudo
#!/bin/bash
# Adapted from (https://unix.stackexchange.com/a/269080/388950)
no_sudo_func () {
apt update
}
F=$(declare -f no_sudo_func)
sudo bash -c "$F; no_sudo_func"
@josepsmartinez
josepsmartinez / gcloudt
Created April 14, 2021 15:36
Alias for `gcloud $@ --tunnel-through-iap`. Remember do `chmod a+x` the script.
#!/bin/bash
PATH_TO_GCLOUD=/snap/bin/google-cloud-sdk.gcloud
${PATH_TO_GCLOUD} $@ --tunnel-through-iap
@josepsmartinez
josepsmartinez / online_averaging.py
Last active March 27, 2021 01:24
Python class for online computing of `np.mean`
import numpy as np
class OnlineStatsCompute():
def __init__(self, *l):
if len(l) > 0:
self.add_l(*l)
def add_e(self, e):
"""Includes a single element `e` in the OnlineStatsCompute.
Updates attributes `N` and `mean`.
@josepsmartinez
josepsmartinez / docstring.py
Created March 12, 2021 13:58
Python docstring template (like gensim and sklearn)
def foo():
"""<FUNCTION DESCRIPTION>
<BLANK LINE>
Parameters
----------
<VARIABLE> : <TYPE>
<DESCRIPTION>
...
<BLANK LINE>
Returns
@josepsmartinez
josepsmartinez / ssh-set-permissions.sh
Last active October 3, 2021 15:49
ssh permissions
#!/bin/bash
# Configs
chmod 700 $HOME/.ssh
chmod 644 $HOME/.ssh/authorized_keys
chmod 644 $HOME/.ssh/known_hosts
chmod 644 $HOME/.ssh/config
# Keys
chmod 600 $HOME/.ssh/id_rsa
@josepsmartinez
josepsmartinez / install-rstudio.sh
Last active March 4, 2021 16:26
Script for installing Rstudio on Ubuntu 20
#!/bin/bash
# For Ubuntu 20 (reference: https://rstudio.com/products/rstudio/download-server/debian-ubuntu/ accessed at Thu 04 Mar 2021 12:39:05 PM -03)
sudo apt-get install r-base
sudo apt-get install gdebi-core
cd /tmp
# Server
# wget https://download2.rstudio.org/server/bionic/amd64/rstudio-server-1.4.1106-amd64.deb
# sudo gdebi rstudio-server-1.4.1106-amd64.deb
@josepsmartinez
josepsmartinez / star-func-def.py
Created February 23, 2021 17:12
Defining a function in Python with *
def foo(a,
b,
*, # arguments after `*` should be named when calling function
c):
print(f"a: {a}")
print(f"b: {b}")
print(f"c: {c}")
try:
foo(1, 2, 3)
except TypeError as e:
@josepsmartinez
josepsmartinez / anomaly-detection.py
Created February 19, 2021 20:14
Anomaly Detection with OneClassSVM and Isolation Forest
import numpy as np
import pandas as pd
from sklearn.svm import OneClassSVM
from sklearn.ensemble import IsolationForest
from sklearn.model_selection import KFold
from sklearn.metrics import accuracy_score
def df2np(df, dtype=np.float64):
return df.to_numpy().astype(dtype)