Skip to content

Instantly share code, notes, and snippets.

View vadimkantorov's full-sized avatar
💭
looking for an internship for summer/fall 2021

Vadim Kantorov vadimkantorov

💭
looking for an internship for summer/fall 2021
View GitHub Profile
@vadimkantorov
vadimkantorov / argparse_dict_argument.py
Last active December 29, 2023 22:05
A one-line example enabling Python's argparse to accept dictionary arguments
# Example:
# $ python argparse_dict_argument.py --env a=b --env aa=bb
# Namespace(env={'a': 'b', 'aa': 'bb'})
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--env', action = type('', (argparse.Action, ), dict(__call__ = lambda a, p, n, v, o: getattr(n, a.dest).update(dict([v.split('=')])))), default = {}) # anonymously subclassing argparse.Action
print(parser.parse_args())
@vadimkantorov
vadimkantorov / urlgrepfb.sh
Created September 7, 2016 16:01
A Bash script for extracting URLs from Facebook message archive file (messages.htm) for a given user name. The script will print most recent links first.
# Usage: bash urlgrepfb.sh "Vadim Kantorov" > urls.txt
cat messages.htm | xpath -q -e "//div[@class='thread' and contains(text(), '$1')]" | grep -o 'http[^ <]*' | tac
@vadimkantorov
vadimkantorov / strip_latex_comments.sh
Last active February 11, 2021 16:58
A Bash function to strip %-style LaTeX comments. The script correctly treats escaped percents, and comments after double backslashes.
# Usage:
# strip_latex_comments file.tex file.nocomments.tex
# strip_latex_comments_inplace file.tex
function strip_latex_comments {cat "$1" | '"sed 's/\([^\\]\|^\)\(\(\\\\\)*\)%.*/\1\2%/g' "'> "$2"; }
function strip_latex_comments_inplace {find . -name '*.tex' -exec sed -i'' 's/\([^\\]\|^\)\(\(\\\\\)*\)%.*/\1\2%/g' {} \; }
alias stip_latex_comments2="sed -e 's/\([^\\]\|^\)\(\(\\\\\)*\)%.*/\1\2%/g'"
# strip_latex_comments2 file.tex > file.nocomments.tex
@vadimkantorov
vadimkantorov / rdv_naturalisation.sh
Last active January 16, 2024 20:26
A Bash script to monitor naturalisation rendez-vous availability at Prefectures Bobigny, Nanterre. Consider renting one or several EC2 micro instances at Paris AWS data center to avoid bans.
# Usage:
# 1. Put in your email/SendGrid credentials or phone/Twilio credentials
# 2. Run as: "bash rdv_naturalisation.sh nanterre" or "bash rdv_naturalisation.sh bobigny" or "bash rdv_naturalisation.sh cre"
# 3. Get your RDV! The script will dump the HTML of the page and will continue execution even after the first RDV is found.
SENDGRID_BEARER_TOKEN='' # should start with "SG."
SENDGRID_EMAIL='' # put your e-mail here
TWILIO_SID='' # should start with "AC"
TWILIO_AUTH_TOKEN=''
TWILIO_FROM_NUMBER='' # put your Twilio Trial Phone Number here, should start with +
@vadimkantorov
vadimkantorov / matlabr.sh
Last active June 30, 2017 04:15
A Bash script that runs a MATLAB script and closes it immediately after. Assumes matlab executable is discoverable.
#! /bin/bash
# Installation: add the following alias to your ~/.bashrc
# alias matlabr='bash <(curl -sS https://gist.githubusercontent.com/vadimkantorov/56f6eb35fab2135c04706a0a67aa2fe6/raw)'
# Usage:
# matlabr /path/to/script.m
# matlabr --fixlibstdc++ /path/to/script.m # to preload system's libstdc++ before running MATLAB. This will help for erors like:
# Invalid MEX-file '/PATH/TO/MYMEX.mexa64': /PATH/TO/MATLAB/ROOT/linux64/bin/glnxa64/../../sys/os/glnxa64/libstdc++.so.6:
# version `GLIBCXX_3.4.20' not found (required by /PATH/TO/SOME/LIBRARY/LINKED/TO/MYMEX.mexa64/MYLIBRARY.so).
@vadimkantorov
vadimkantorov / gdb.sh
Last active September 24, 2023 23:04
A Bash alias to run a Torch script under gdb
# Usage:
# gdbpython script.py my_script_arguments
# gdbbacktrace ./my_program arg1 arg2
alias gdbpython="gdb -ex run --args python"
alias gdbbacktrace="gdb -batch -ex run -ex bt --args"
@vadimkantorov
vadimkantorov / whiten.lua
Last active January 8, 2017 13:27
Torch routine and module for whitening, supporting large matrices, e.g. 5K x 10K
-- Produces mean and the whitening transform of matrix x[n, d] holding n data points of dimension k, using rank k approximation
-- Auxiliary gaussian matrix g[d, f * k] will use f = 3 by default
-- Original paper: http://users.cms.caltech.edu/~jtropp/papers/HMT11-Finding-Structure-SIREV.pdf
function whiten(x, k, f)
local n, d = unpack(x:size():totable())
local mean = x:mean(1)
local g = x.new():randn(d, math.min((f or 3) * k, x:size(2)))
@vadimkantorov
vadimkantorov / DynamicView.lua
Last active November 10, 2016 06:32
DynamicView module for Torch that views an input tensor dynamically at runtime with user-provided function of its size. See an example of total variation computation below.
function DynamicView(getSizeTable)
local module = nn.View(-1)
module.updateOutput = function(self, input) return nn.View.updateOutput(self:resetSize(unpack(getSizeTable(input:size()))), input) end
return module
end
-- function TotalVariation() --accepts 4D tensors
-- local sk = torch.Tensor(2, 1, 2,2)
-- sk[1][1]:copy(torch.Tensor(2,2):set(torch.Storage({-1, 1, 0, 0})))
-- sk[2][1]:copy(torch.Tensor(2,2):set(torch.Storage({-1, 0, 1, 0})))
@vadimkantorov
vadimkantorov / hdf5_util.lua
Last active March 11, 2017 10:30
Torch routines for saving and loading acyclic objects with HDF5
-- Usage:
-- hdf5_save('/path/to/my.h5', {a = torch.Tensor(1), b = {torch.CudaTensor(2)}, 'string', c = 5, d = true})
-- obj = hdf5_load('/path/to/my.h5')
-- obj = hdf5_load('/path/to/my.h5', {'a', 'b'})
-- tensor = hdf5_load('/path/to/my.h5', {'a'})
require 'hdf5'
function hdf5_save(path, obj)
local function dfs(datapath, obj, h)
@vadimkantorov
vadimkantorov / vis_html_table.lua
Last active November 24, 2016 14:24
Torch routine for generating HTML tables with embedded images
require 'image'
require 'base64' -- luarocks install lbase64
function vis_html_table(vis_path)
return {
file = io.open(vis_path, 'w'):write('<html><body><table>'),
close = function(self)
self.file:write('</tr></table></body></html>'):close()
end,
figure = function(self, img, top_legend, bottom_legend, rescale, width, height)