Skip to content

Instantly share code, notes, and snippets.

View devforfu's full-sized avatar

Ilia devforfu

View GitHub Profile
@devforfu
devforfu / dask_short.py
Last active October 23, 2021 14:54
Shortened Dask example
# Runs a toy image generation task on Dask.
#
# Note that the code was shortened for better readability.
# Please refer the following repository to see the full example:
# https://github.com/devforfu/dask_benchmark
@cli
def main(task_params: TaskParams) -> None:
jobs = [
Job(
@devforfu
devforfu / codex.py
Last active August 14, 2021 19:41
A snippet requesting OpenAI's Codex generated with the help of Codex.
#!/Users/ck/.conda/envs/codex/bin/python
"""
The script wrapping OpenAI client into a simple CLI. Was generated using Codex with minor manual tweaks.
"""
import sys
from argparse import ArgumentParser
from dataclasses import dataclass
from typing import Dict, List
import openai
def main():
params = create_default_parser(__file__).parse_args()
super_seed(params.seed)
loaders, meta = create_data_loaders(dataset_name=params.dataset,
dataset_root=params.dataset_path,
num_workers=params.num_workers,
batch_size=params.batch_size,
sample_transformer=transforms.Compose([
@devforfu
devforfu / lightning_experiment.py
Created November 8, 2020 19:44
Lightning experiment
class BasicCNNExperiment(BaseExperiment):
def create_model(self) -> nn.Module:
return nn.Sequential(
nn.Conv2d(3, 16, 3),
nn.ReLU(inplace=True),
nn.Conv2d(16, 32, 3),
nn.ReLU(inplace=True),
nn.AdaptiveMaxPool2d(1),
nn.Flatten(1, -1),
def basic_parser(script_filename: str = None) -> ArgumentParser:
experiment_name = (
Path(script_filename).stem
if script_filename is not None
else None
)
datasets = available_datasets()
p = ArgumentParser()
p.add_argument('--dataset_path', required=True)
p.add_argument('--dataset', choices=datasets, default=datasets[0])
@devforfu
devforfu / create_data_loaders.py
Created November 8, 2020 18:59
Create data loaders
def create_data_loaders(
dataset_root: str,
dataset_name: str,
sample_transformer: Callable = None,
target_transformer: Callable = None,
num_workers: int = cpu_count(),
batch_size: int = 32,
download: bool = True,
pin_memory: bool = False,
) -> Tuple[OrderedDict, Dict]:
@devforfu
devforfu / error_messages.py
Created June 23, 2020 15:16
Error messages
import numpy as np
class MathErrors:
ZeroDivision = 'division failed: argument {argument} is zero.'
NaN = 'multiplication failed: argument {argument} is not a number.'
OutOfRange = 'value {argument} should be in range [{a}, {b}), but {value} received'
def divide(a: float, b: float) -> float:
@devforfu
devforfu / traceback_decorator.py
Last active June 23, 2020 16:44
Traceback logging decorator
import logging
import traceback
from typing import Callable
def report_failure_traceback(logger: logging.Logger, exc: Exception) -> None:
logger.critical('*** Unexpected error ***')
logger.critical('')
lines = traceback.format_tb(exc.__traceback__)
lines = ''.join(lines).split('\n')
@devforfu
devforfu / dicom.txt
Created November 20, 2019 11:26
Example of DICOM file content
(0008, 0018) SOP Instance UID UI: ID_3d2ea4e22
(0008, 0060) Modality CS: 'CT'
(0010, 0020) Patient ID LO: 'ID_e758d530'
(0020, 000d) Study Instance UID UI: ID_ebb91b8154
(0020, 000e) Series Instance UID UI: ID_57e1457f93
(0020, 0010) Study ID SH: ''
(0020, 0032) Image Position (Patient) DS: ['-125', '26', '272.300049']
(0020, 0037) Image Orientation (Patient) DS: ['1', '0', '0', '0', '1', '0']
(0028, 0002) Samples per Pixel US: 1
(0028, 0004) Photometric Interpretation CS: 'MONOCHROME2'
@devforfu
devforfu / example.py
Last active August 25, 2019 17:12
Catalyst example. (Doesn't work as expected).
import os
import re
from pdb import set_trace
from multiprocessing import cpu_count
from pprint import pprint as pp
from imageio import imread
import numpy as np
import pandas as pd
import PIL.Image