Skip to content

Instantly share code, notes, and snippets.

@bdsaglam
bdsaglam / insert-datetime-karabiner.json
Created February 7, 2021 16:54
Insert datetime with Karabiner
rules: [
{
"description": "hyper + d inserts datetime",
"manipulators": [
{
"from": {
"key_code": "d",
"modifiers": {
"mandatory": [
"left_shift",
@bdsaglam
bdsaglam / gsd.sh
Created September 16, 2020 20:22
Get script directory
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
@bdsaglam
bdsaglam / ccl.py
Last active August 11, 2020 06:42
Connected component labelling in Python
import numpy as np
# Time complexity: O(n), where n is the number of pixels in image
# Space complexity: O(n)
def label_connected_components(image: np.uint8) -> np.int:
nrow = image.shape[0]
ncol = image.shape[1]
component_map = np.full((nrow, ncol), -1, dtype=np.int)
current_comp_id = 1
for r in range(nrow):
@bdsaglam
bdsaglam / isSorted.swift
Last active August 10, 2020 16:02
Swift isSorted
//
// SequenceExtensions.swift
// Swift6006
//
// Created by Barış Deniz Sağlam on 10.08.2020.
// Copyright © 2020 BDS. All rights reserved.
//
import Foundation
@bdsaglam
bdsaglam / pulverizer.py
Created July 28, 2020 20:03
Pulverizer algorithm to find smallest linear combination of two positive numbers
def quotient_remainder(a, b):
# returns quotient and remainder of a division b
# a = q*b + r
q = 0
while a >= b:
q += 1
a -= b
return q, a
@bdsaglam
bdsaglam / expm.py
Created July 14, 2020 09:50
n-order Taylor approximation of matrix exponentiation for PyTorch
def expm(x, order=10):
'''
nth-order Taylor approximation of matrix exponential
'''
if order < 1:
raise ValueError("order cannot be smaller than 1")
I = torch.eye(x.shape[-1], dtype=x.dtype, device=x.device)
result = I
nom = I
@bdsaglam
bdsaglam / log.py
Last active June 18, 2020 17:02
Python logging example
# Credits: https://gist.github.com/nguyenkims
import logging
import sys
from logging.handlers import TimedRotatingFileHandler
FORMATTER = logging.Formatter("[%(asctime)s][%(name)s][%(levelname)s] - %(message)s")
LOG_FILE = "app.log"
def make_console_handler():
@bdsaglam
bdsaglam / hydra_in_jupyter.py
Last active April 29, 2024 08:53
Use Hydra in Jupyter notebook
import pathlib
import hydra
hydra._internal.hydra.GlobalHydra().clear()
config_dir = pathlib.Path('/path/to/configs/')
hydra.experimental.initialize(config_dir=config_dir)
cfg = hydra.experimental.compose(config_file='config.yaml', overrides=[])
print(cfg.pretty())
@bdsaglam
bdsaglam / pycharm.gitignore
Created June 13, 2020 09:40
gitignore file for PyCharm
data/
# Jupyter notebook checkpoints
notebooks/
*.ipynb_checkpoints/
# PyTorch Lightning
*lightning_logs/
# PyCharm files
@bdsaglam
bdsaglam / conv_output_size.py
Created April 21, 2020 17:52
Calculate output size after convolution
def conv_output_size(in_size, kernel, stride=1, padding=0):
return (in_size - kernel + 2*padding) // stride + 1