Skip to content

Instantly share code, notes, and snippets.

View Miladiouss's full-sized avatar

Milad Pourrahmani Miladiouss

View GitHub Profile
@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)
convert -resize 800x800 -delay 25 -loop 0 image-file-*.png animation.gif
@Miladiouss
Miladiouss / pix2world_timing.py
Last active October 29, 2019 00:32
Demonstrates that evaluating wcs.pixel_to_world is efficient for large vectors but very inefficient for single calls.
from time import sleep, time
import numpy as np
from astropy import wcs
w = wcs.WCS(naxis=2)
w.wcs.ctype = ["RA---SIN","DEC--SIN"]
w.wcs.cdelt = (6.2/3600, 6.2/3600)
w.wcs.crval = (30, 60)
w.wcs.crota = (0, 15)
# ----------------------------------------
@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 / IPythonAppetizer.py
Last active October 25, 2019 19:38
Usual imports and function for an IPython / Jupyter environment, specially for data science (e.g. matplotlib, numpy, time, ...).
# https://gist.github.com/Miladiouss/332b21e3c9d82ca2085b1396641cc80d
#-----------------------------------------------------------
# IPython
from IPython.core.interactiveshell import InteractiveShell
from IPython.display import clear_output
InteractiveShell.ast_node_interactivity = "all"
from IPython import display
#-----------------------------------------------------------
# Scientific