Skip to content

Instantly share code, notes, and snippets.

@AVTsoof
AVTsoof / argparse_update_defaults.py
Created June 6, 2023 12:45
python argparse update default values from python
import argparse
def get_parser() -> argparse.ArgumentParser:
...
def process_args(args: argparse.Namespace) -> argparse.Namespace:
...
@AVTsoof
AVTsoof / pydantic_to_streamlit_app.py
Created May 22, 2023 10:36
pydantic to streamlit app
import json
from enum import Enum
from typing import get_origin, get_args, Any, Dict, List
import streamlit as st
from pydantic import BaseModel
# Example Enum
class Color(str, Enum):
def get_files_in_dir(rootdir: str , suffix: str, subdirs: bool = True) -> List[str]:
rootdir = Path(str(rootdir))
if subdirs:
glob_pattern = "**/*"
else:
glob_pattern = "*"
files_lower = list(rootdir.glob(f"{glob_pattern}{suffix.lower()}"))
files_upper = list(rootdir.glob(f"{glob_pattern}{suffix.upper()}"))
@AVTsoof
AVTsoof / xview_split_geojson_per_image.py
Created April 13, 2023 13:13
XVIEW Dataset - split geojson per image file
import logging
from copy import deepcopy
from typing import List, Dict, Any
from pathlib import Path
from shapely.geometry import box as bbox_to_polygon
from tqdm import tqdm
import rasterio
import rasterio.warp
@AVTsoof
AVTsoof / setup.py
Last active July 6, 2023 07:58
example python setup.py that uses requirements.txt file
from distutils.core import setup
from setuptools import find_packages
requirements = [r for r in open("requirements.txt", "r").read().splitlines() if not r.startswith("#")]
setup(name='MyPackage',
version='1.0',
description='Description of my package',
author='author-name',
author_email='author@email.com',
@AVTsoof
AVTsoof / pytorch_scikit_train_val_split.py
Last active December 18, 2022 08:16
An example of how you can use the train_test_split function from scikit-learn to split a PyTorch dataset into training and validation sets, using the Subset class from torch.utils.data to create the training and validation datasets
"""
This code will split the MyDataset class into a training set and a validation set,
using the train_test_split function from scikit-learn to generate the indices for
the training and validation sets.
The train_test_split function randomly splits the indices into two sets,
with the training set containing 80% of the data and the validation set containing the remaining 20%.
The training and validation sets are then created using the Subset class and wrapped in PyTorch DataLoaders,
which can be used to iterate over the datasets and feed the data into a PyTorch model during training and evaluation.
"""
@AVTsoof
AVTsoof / import_parents_src_packages.py
Last active December 18, 2022 08:17
Making sure that custom src code imported packages path is ok no matter where you run the code from
import os
import sys
from pathlib import Path
# create absolute paths for this file and parents
__realpath__ = str(Path(os.path.realpath(__file__)))
__parents__ = [str(p) for p in Path(__realpath__).parents]
# add to PATH so that we can `import ...`
sys.path.append(__parents__[0])
@AVTsoof
AVTsoof / get_local_from_py_file.py
Last active July 14, 2022 12:05
Get locals from a *.py file given as an argument to another python file
def get_py_locals(filepath: str):
file_locals = {}
with open(filepath) as f:
content = f.read()
exec(content, None, file_locals)
return file_locals