Skip to content

Instantly share code, notes, and snippets.

View JohnnyRacer's full-sized avatar
🎲
Hydrated and purified

JohnnyRacer JohnnyRacer

🎲
Hydrated and purified
  • Seattle, WA
View GitHub Profile
@JohnnyRacer
JohnnyRacer / readme.md
Last active July 13, 2025 19:44
Retrieving an interval between dates in SQLAlchemy

SQLAlchemy provides ways to work with date and time intervals, allowing you to select and filter data based on time differences. Here's a breakdown:

1. Calculating date differences using DATEDIFF (or similar functions)

Many databases offer functions like DATEDIFF() (SQL Server, MySQL) or EXTRACT(unit FROM age()) (PostgreSQL) to calculate the difference between two dates in specific units (days, months, years, etc.). You can incorporate these functions into your SQLAlchemy queries using func, according to Stack Overflow and DbVisualizer.

Example:

from sqlalchemy import func, text
from datetime import datetime

# Assuming 'your_table' has 'start_date' and 'end_date' columns
@JohnnyRacer
JohnnyRacer / readme.md
Created July 13, 2025 01:55
Implementing "OR" conditions in PyPika

The "OR" conditions in PyPika can be achieved using the | operator or the Criterion.any() function. Here are some examples:

  1. Using the | operator: This is the most straightforward way to combine multiple conditions with "OR".
from pypika import Query, Table

customers = Table('customers')
q = Query.from_(customers).select(
@JohnnyRacer
JohnnyRacer / readme.md
Created February 5, 2025 18:16
Using Convolution To Split Tensors PyTorch

While you can't directly use convolution to split tensors in PyTorch, you can achieve a similar effect by creatively using the convolution operation. Here's how: Method 1: Using Convolution to Extract Specific Regions You can use convolution to extract specific regions of a tensor by designing appropriate kernels. For example:

import torch
import torch.nn as nn

# Create a sample tensor
@JohnnyRacer
JohnnyRacer / readme.md
Created December 13, 2024 12:40
Approaches to creating a random integers that are ranged in PyTorch

1. Sample a Center and then a Small Range Around It:

This is the simplest and often most effective approach:

import torch

def randint_constrained(low, high, range, size, device=None):
    """
    Generates random integers within a specified range, constrained so that 
@JohnnyRacer
JohnnyRacer / readme.md
Created November 28, 2024 06:35
convert .mat to .png using Python

To convert a .mat file to a .png image in Python, you can use the following steps:

  1. Load the .mat file:
import scipy.io as sio
import matplotlib.pyplot as plt

# Load the .mat file
mat_data = sio.loadmat('your_file.mat')
@JohnnyRacer
JohnnyRacer / readme.md
Created November 28, 2024 04:35
Per-image best threshold PyTorch

Finding the per-image best threshold in PyTorch can be achieved in several ways, depending on your specific use case. Here's a general approach for binary classification:

  1. Otsu's Method: Otsu's method is a widely used technique for automatic thresholding. It finds the optimal threshold by maximizing the inter-class variance.
import torch
import torchvision.transforms as transforms
from skimage.filters import threshold_otsu

def find_best_threshold(image):
@JohnnyRacer
JohnnyRacer / readme.md
Created November 28, 2024 04:32
ODS F-score PyTorch

PyTorch doesn't have a built-in function for calculating ODS F-score, you can easily implement it using the available metrics. Here's how: Step 1: Install torchmetrics pip install torchmetrics

Step 2: Implement the ODS F-score calculation

import torch
from torchmetrics import PrecisionRecallCurve
@JohnnyRacer
JohnnyRacer / readme.md
Created November 28, 2024 04:30
Optimal Image Scale F-score PyTorch

In PyTorch, the "optimal image scale" for achieving the best F-score depends heavily on your specific task and dataset, but generally, you should aim to find an image scale where the model can capture the relevant details while minimizing unnecessary information that could introduce noise, with the best scale often being determined through experimentation using a validation set and techniques like grid search or randomized search to find the optimal value. Key points to consider when choosing an image scale for optimal F-score:

  • Object size in relation to image size: If your objects of interest are small, a larger image scale might be needed to capture details, while large objects may benefit from a smaller scale to avoid excessive background information. Dataset variability: If your dataset has significant variations in object size or image quality, a wider range of scales might be beneficial to test. Computational constraints: Larger image scales require more processing power, so cons
@JohnnyRacer
JohnnyRacer / readme.md
Last active November 13, 2024 21:48
Replacing PyTorch Modules

Method 1: Direct Replacement

If you know the exact name or reference to the module you want to replace, you can directly assign a new module to it.

import torch.nn as nn

class MyModel(nn.Module):
    def __init__(self):
        super().__init__()
@JohnnyRacer
JohnnyRacer / install.sh
Created July 25, 2022 14:52
Install docker-compose
!#/bin/bash
export DOCKER_CONFIG=${DOCKER_CONFIG:-$HOME/.docker};
mkdir $DOCKER_CONFIG; # Creates the directory if not exists.
curl -SL https://github.com/docker/compose/releases/download/v2.4.1/docker-compose-linux-x86_64 -o $DOCKER_CONFIG/cli-plugins/docker-compose;
sudo ln -s $DOCKER_CONFIG/docker-compose /usr/local/bin/docker-compose # Creates a soft link