Skip to content

Instantly share code, notes, and snippets.

View ogvalt's full-sized avatar
🏠
Working from home

Oleksandr Popovych ogvalt

🏠
Working from home
  • Ukraine
View GitHub Profile
@ogvalt
ogvalt / Taskfile.yaml
Created April 18, 2024 10:54
This is my standard taskfile for my python projects. Pre-commit, poetry, mypy, pytest
version: '3'
dotenv: ['.env']
tasks:
setup:
desc: Setup development environment
cmds:
- poetry run pre-commit install
- poetry run pre-commit install --hook-type commit-msg
@ogvalt
ogvalt / argparse_pathtype.py
Created December 16, 2021 10:55
Validate type of argument parsed by argparse
"""
# Usage:
import argparse
from argparse_pathtype import PathType
parser = argparse.ArgumentParser(description='Process some integers.')
parser.add_argument('--dir-models', required=True, type=PathType(path_to="dir"),
help='Directory where models are stored')
"""
@ogvalt
ogvalt / local_hist.py
Created March 11, 2020 08:57
Example of using torch.nn.Unfold by calculating local histograms
# Docs to main working horse of those transformations:
# https://pytorch.org/docs/stable/nn.html?highlight=unfold#torch.nn.Unfold
#
def local_histogram(
batch: torch.Tensor,
*,
kernel_size: Union[int, tuple] = (3, 3),
stride: Union[int, tuple] = None,
bins: int = 10,
range_min: float = 0.,
@ogvalt
ogvalt / stratified_sample.py
Created November 23, 2019 19:18 — forked from srikarplus/stratified_sample.py
Stratified Sampling in Pytorch
def make_weights_for_balanced_classes(images, nclasses):
count = [0] * nclasses
for item in images:
count[item[1]] += 1
weight_per_class = [0.] * nclasses
N = float(sum(count))
for i in range(nclasses):
weight_per_class[i] = N/float(count[i])
weight = [0] * len(images)
for idx, val in enumerate(images):
@ogvalt
ogvalt / best_dividion.py
Created June 19, 2019 08:59
Brute force solution for dividing array into 3 parts of equal sum
def best_division(a):
"""
This function performs exhastive search over all possible divisions of array into 3 parts with equal sums
a - array on numbers (list<int|float>)
"""
results = []
for i in range(1, len(a)-2+1):
for j in range(i+1, len(a)-1+1):
first, second, third = sum(a[:i]), sum(a[i:j]), sum(a[j:])
opt_func = abs(first - second) + abs(second - third) + abs(first - third)