Skip to content

Instantly share code, notes, and snippets.

View VasLem's full-sized avatar

Vassilis Lemonidis VasLem

  • UAntwerp
  • Antwerp, Belgium
View GitHub Profile
@VasLem
VasLem / .virtualenv_autoactivate.sh
Last active September 30, 2024 09:12
Made for Linux. Auto activates and deactivates Python virtual environments of a project. The requirement is that the virtual environment resides in .venv inside the root directory of the project. To use it, source it in your .bashrc.
check_for_venv() {
DIR_NAME=$PWD
VENVLOC=
while [ "$DIR_NAME" != "/" ]; do
if [ -e "$DIR_NAME/bin/python" ] && [ -e "$DIR_NAME/bin/activate" ]; then
VENVLOC="$DIR_NAME"
_VENV_NAME=$(basename $DIR_NAME)
break
elif [ -e "$DIR_NAME/.venv/bin/python" ] && [ -e "$DIR_NAME/.venv/bin/activate" ] ; then
@VasLem
VasLem / conftest.py
Last active April 10, 2023 14:45
Session fixture to asymmetrically load and write files for pytest tests . During writing it is equivalent to mounting a directory on top of another. During reading, the original directory is read if exists, else the test directory is read. The behavior is configurable. Read docstring for more.
"""Defines a fixture that does the following:
- Create an asymmetric way to read and write files.
If to write files, redirect to TEST_DATA_DIRS corresponding folder.
If to read files, check whether TEST_DATA_DIRS corresponding file exists there and read from there, else read from DATA_DIR.
TEST_DATA_ONLY_DIRS are directly read from the TEST_DATA_DIR
If files end with IGNORE_EXTENSIONS, read from DATA_DIRS
Only path strings are currenly supported, not Path variables!
(**NOTICE**: System builtins are also overriden)
@VasLem
VasLem / unet_residual.py
Last active April 28, 2023 18:56
UNet with Conv1D shortcut Residual Blocks
from typing import List, Union, cast
from torch import nn
from torch.nn import BCEWithLogitsLoss, MaxPool2d, functional
class ResBlock(nn.Module):
def __init__(self, in_channels, out_channels, doShortcut=True):
super().__init__()
inter_channels = round((in_channels + out_channels) / 2)
@VasLem
VasLem / iou.py
Created August 3, 2023 12:25
Masked patch related functions
form typing import List
from patch import Patch
def iou(patch, patches: List[Patch]):
# commpute IoU between a patch and a list of patches
ref_mask = patch.mask
ref_box = patch.bbox
ref_mask_size = np.sum(ref_mask > 0)
seg_ious = []
for patch in patches:
reg_box = patch.bbox
@VasLem
VasLem / video_metadata_handler.py
Last active September 5, 2023 15:09
VideoMetadataHandler
# Assumes ffmpeg has been installed in conda environment
# Assumes Linux environment.
import os
import shutil
import subprocess
import cv2
import numpy as np
@VasLem
VasLem / generate_curved_polygon.py
Last active September 12, 2023 10:05
Generate points of a curved polygon using 2nd degree Bezier curves
import numpy as np
import cv2
import math
from bezier.curve import Curve
def generate_curved_polygon(
num_sides: int,
radius: float,
orientation: int = 0,
side_curvature=0.1,
@VasLem
VasLem / download_cosmic.py
Last active April 22, 2024 16:10
Download cosmic database files and convert them to bed files
#!/usr/bin/env python
# %%
import pandas as pd
import numpy as np
import requests
import tarfile
import tempfile
import os
from math import ceil
from tqdm.auto import tqdm
@VasLem
VasLem / annotate_bed.sh
Last active April 21, 2024 16:45
Annotate a bed file using an input bed annotation , assigning all columns, apart from the first 3. Requires bedtools in $PATH
#!/bin/bash
# required: bedtools in $PATH
print_help(){
echo -e "
1st pos. argument: the bed path to annotate
2nd pos. argument: the bed path to get the annotations from
3rd pos. argument: the output filename
4th pos. argument: whether the output has headers (1) or not (0)
5th optional pos. argument: the prefix to add to the annotated columns, if header to be output
"
@VasLem
VasLem / join_bed.sh
Created April 21, 2024 16:47
Join bed files, without looking at strandedness, only at the first 3 columns (chr, start, end). Requires bedtools and bedops in $PATH.
#!/bin/bash
# required: bedtools and bedops in $PATH
print_help(){
echo -e "
1st pos. argument: the bed paths space separated
2nd pos. argument: the bed paths ids, space separated, with size equal to bed paths
3rd pos. argument: the output filename
4rth pos.argument: whether the inputs have headers (1) or not (0)
"
}
@VasLem
VasLem / annotate_bed.py
Last active May 7, 2024 11:36
Intersect a bed file without header with a source file without header, with special handling of the strand information
#!/usr/bin/env python
from tqdm import tqdm
import sys
import contextlib
import logging
from typing import Literal
LOGGER = logging.getLogger("bed_annotate")