Skip to content

Instantly share code, notes, and snippets.

View Miladiouss's full-sized avatar

Milad Pourrahmani Miladiouss

View GitHub Profile
@Miladiouss
Miladiouss / index_of_which_bin.py
Last active September 13, 2018 22:33
Given a (flattened) list of lists, which element of which list does i correspond to?
def index_of_which_bin(bin_sizes, absolute_index, verbose=False):
"""
Given the absolute index, returns which bin it falls in and which element of that bin it corresponds to.
"""
# Which class/bin does i fall into?
accum = np.add.accumulate(bin_sizes)
if verbose:
print("accum =", accum)
bin_index = len(np.argwhere(accum <= absolute_index))
if verbose:
@Miladiouss
Miladiouss / SimpleParticleSimulator.py
Last active October 2, 2018 03:50
A simple educational particle simulator that captures laws of classical mechanics. In the current state, it will plot an exaggerated perihelion precision of Mercury.
# Author: Miladiouss (Milad Pourrahmani)
# Author's Website: Miladiouss.com
# Date: Sep 26, 2018
# Title: Simple Particle Simulator
# Description:
"""
I am hoping to assemble a series of brief educational computer programs that will capture the simplicity and elegance of laws of physics, both in nature and in apprehension.
This particular educational project (Simple Particle Simulator) is the first of this series. In less than 25 lines, plotting and comments aside, we can obtain the trajectory of a particle in ANY force field. The generated plot displays the simulated and exaggerated perihelion precision of Mercury.
@Miladiouss
Miladiouss / logistic_map.py
Created February 22, 2019 00:12
Simple code for bifurcation of logistic map
# Library import for plotting
import matplotlib.pyplot as plt
%matplotlib inline
# Define Logistic Map
def f(x, r):
return r * x * (1 - x)
# Define history and parameters
history = [0.5]
@Miladiouss
Miladiouss / GPU Detection Test in Python and PyTorch.py
Created March 8, 2019 00:01
Ensure Python and PyTorch can detect all the GPUs in your system.
# GPU Detection Tests
from torch.cuda import device_count
import gpustat
print("""
If pytorch raises "RuntimeError: cuda runtime error (30)" after suspension,
run the following commands in linux terminal:
sudo rmmod nvidia_uvm
sudo rmmod nvidia
@Miladiouss
Miladiouss / Stamp.py
Last active March 11, 2019 18:48
Print a stamp in Python with date, time, device name, and user name (e.g. 'Fri Mar 08 2019 at 00:41:02 on myPC by User')
from datetime import datetime
timeFormat = '%a %b %d %Y at %H:%M:%S'
import socket
deviceName = socket.gethostname()
import getpass
userName = getpass.getuser()
t1 = datetime.now()
print(t1.strftime(timeFormat) + ' on ' + deviceName + ' by ' + userName)
@Miladiouss
Miladiouss / HSC-Cat-Selection.sql
Last active June 15, 2019 00:22
An HSC-PDR2 SQL query to extract RA, Dec, tract, patch of clean galaxies with a magnitude cut.
-- An HSC-PDR2 SQL query to extract RA, Dec, tract, patch of clean galaxies with a magnitude cut.
-- Useful Links:
-- /https://hsc-release.mtk.nao.ac.jp/hscMap-pdr2/app/#/?_=%7B%22view%22%3A%7B%22a%22%3A5.770914805732617,%22d%22%3A-0.019535944017082533,%22fovy%22%3A0.0002503311005562077,%22roll%22%3A0%7D,%22sspParams%22%3A%7B%22type%22%3A%22SDSS_TRUE_COLOR%22,%22filter%22%3A%5B%22HSC-I%22,%22HSC-R%22,%22HSC-G%22%5D,%22simpleRgb%22%3A%7B%22beta%22%3A22026.465794806718,%22a%22%3A1,%22bias%22%3A0.05,%22b0%22%3A0%7D,%22sdssTrueColor%22%3A%7B%22beta%22%3A26003.369421497522,%22a%22%3A1,%22bias%22%3A0.14439999999999995,%22b0%22%3A0%7D%7D%7D
-- https://hsc-release.mtk.nao.ac.jp/schema/#pdr2.pdr2_wide.forced
SELECT
object_id
, tract
@Miladiouss
Miladiouss / Ordinality.py
Last active July 1, 2019 10:13
Calculate and appends ordinality to a dataframe
def appendOrdinality(df, columns, ascending, strictlyIncreasing=False):
"""
Appends absolute and relative ordinality to a dataframe.
Oridinality is the normalized position of a row in a sorted dataframe.
df:
Pandas DataFrame
columns:
List of column names for to be used for sorting (e.g. ['prob_1'])
ascending:
@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
@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)
# ----------------------------------------
convert -resize 800x800 -delay 25 -loop 0 image-file-*.png animation.gif