Skip to content

Instantly share code, notes, and snippets.

View Miladiouss's full-sized avatar

Milad Pourrahmani Miladiouss

View GitHub Profile
@Miladiouss
Miladiouss / Select_CIFAR10_Classes.py
Last active October 27, 2023 03:41
Create PyTorch datasets and dataset loaders for a subset of CIFAR10 classes.
import torchvision
import torchvision.transforms as transforms
from torchvision.datasets import CIFAR10
from torch.utils.data import Dataset, DataLoader
import numpy as np
# Transformations
RC = transforms.RandomCrop(32, padding=4)
RHF = transforms.RandomHorizontalFlip()
RVF = transforms.RandomVerticalFlip()
@Miladiouss
Miladiouss / config.py
Last active January 3, 2023 20:45
Load a YAML file and convert it to a Python object. Ideal for nested datasets since working with nested dictionaries is not user-friendly. Works with IDE auto-completion recommendation systems. The example below is loading a config file.
from pathlib import Path
import yaml
here = Path(__file__).resolve().parent
with open(here / './config.yaml', 'r') as stream:
config_dict = yaml.safe_load(stream)
class Struct:
@Miladiouss
Miladiouss / plotly-1d-plot.py
Last active October 7, 2022 00:34
Plotting on a 1-D number axis.
import plotly.graph_objects as go
fig = go.Figure()
xdata = [(1 - 1/i) for i in range(1, 15)]
fig.add_trace(go.Scatter(
x=xdata, y=len(xdata)*[0], mode='markers', marker_size=10, marker_color='red'
))
fig.update_xaxes(showgrid=False)
fig.update_yaxes(showgrid=False,
@Miladiouss
Miladiouss / polotly-streaming-data.py
Created October 5, 2022 16:43
Example of dynamic data visualization with Plotly where more data points are gradually added.
import plotly.graph_objects as go
from time import sleep
import numpy as np
fig = go.FigureWidget()
# Display in Jupyter:
display(fig)
sleep(.25)
fig.add_trace(go.Scatter(x=[0], y=[0]))
@Miladiouss
Miladiouss / sip_utilities.py
Created April 28, 2022 00:02
Convenient utilities for constructing an Astropy SIP model from a FITS header.
from astropy.modeling.polynomial import SIP
import itertools
from astropy.io.fits.header import Header
def get_sip_coeffs(hdr: Header , kind='A') -> dict:
"""Return a SIP polynomial coefficients for the selected
coefficient type: (A, B, AP, BP).
Parameters
----------
@Miladiouss
Miladiouss / astropy_quantities.py
Last active April 13, 2022 22:27
Proper way of constructing and displaying quantities using Astropy in Jupyter Notebooks
# Science
import numpy as np
from astropy.units import Unit
# Notebook
from IPython.display import Markdown
from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity = "all"
# Handy functions
@Miladiouss
Miladiouss / line-plot-with-dict-styling.py
Created April 9, 2022 01:37
An advanced example of generating publication quality plots for scientific journals in Python using Plotly.
import plotly.graph_objects as go
import numpy as np
from scipy.special import gamma
from pathlib import Path
here = Path(__file__).parent
filename = Path(__file__).stem
xdata = np.linspace(-5, 5, 1000)
ydata = gamma(xdata)
@Miladiouss
Miladiouss / Python_FITS_Handler.py
Last active January 16, 2021 21:08
Easy astronomy FITS file handling for Python. It includes easy cutout and save function. Additionally, a percentile normalization method is provided which is ideal for scaling FITS files to better visualization (similar to MinMax of DS9).
import numpy as np
from pathlib import Path
# AstroPy
import astropy
from astropy.coordinates import SkyCoord, GCRS, ICRS, GeocentricTrueEcliptic, Galactic
import astropy.units as u
from astropy.io import fits
from astropy.wcs import WCS
@Miladiouss
Miladiouss / matplotlibTemplate.py
Created October 11, 2019 08:46
Scientific Plotting Template in Python using matplotlib.
"""
Author: Miladious
Latest Modification Date: Oct 11, 2019
About:
Creating a publication quality figure using matplotlib requires a lot of tweaks.
In this gist, I show the main tweaks for creating publication quality plots.
This is an example how one would plot astronomical images in python.
Feel free to modify to your field's standards.
"""
@Miladiouss
Miladiouss / histogramTransform.py
Last active July 21, 2020 20:29
Image Histogram Transformation.
import numpy as np
from matplotlib import pyplot as plt
import torch
class HistogramTransform(object):
"""
Transforms the distribution of the input tensor to match that
of the list of template histograms corresponding to each channel.