Skip to content

Instantly share code, notes, and snippets.

View CianMRoche's full-sized avatar

Cian ("k" + "ian") CianMRoche

View GitHub Profile
@CianMRoche
CianMRoche / combined_legend_handle.md
Created July 5, 2023 17:44
Matplotlib combined legend handle demo

This is a demo demonstrating how to combine multiple plot objects into a single legend entry. The term "handle" is used to refer to the marker in the legend.

import matplotlib.pyplot as plt
import numpy as np

legend_handles = []

# Set up plot
fig, ax = plt.subplots(dpi=150)
@CianMRoche
CianMRoche / pdf2png.bat
Last active January 5, 2023 08:08
Convert all pdfs in a folder to pngs with ghostscript on windows
@echo off
REM Make sure to have ghostscript installed and in system path (default folder C:\Program Files\gs\gsXX.XX.X\bin contains gswin64c.exe)
setlocal
set GS_OUTPUT_DIR=PNG
mkdir %GS_OUTPUT_DIR%
for %%i in (*.pdf) do gswin64c -q -sDEVICE=png16m -r300 -o "%GS_OUTPUT_DIR%\%%~ni.png" "%%i"

Trapezoid function (symmetric)

Uses numpy only, with parameters for center, slope, plateau width, base height ("offset" below) and base to plateau distance ("height" below)

Similar to this astropy model but not object-oriented / a bit more transparent

import numpy as np

def trapezoid(x, center=0, slope=1, width=1, height=1, offset=0):
    """
@CianMRoche
CianMRoche / imports.md
Last active July 23, 2022 16:41
Import custom python package from another folder, as simple as possible

Goal

  • Have a custom python packages folder which we will refer to as custom_packages.
  • No using pip or conda or extra functionality, just import functions etc.

Think "useful functions I want access to and wont be changing that much". More detailed description here.

Directory Structure

Your custom_packages folder should have the following structure (but doesnt have to live in the home directory)

~ (home directory)
@CianMRoche
CianMRoche / interactive.md
Last active July 13, 2022 21:58
Interactive plot in jupyter notebooks

Two examples of making interactive plots in jupyter notebooks

Minimal Example

From this stackoverflow answer.
See this for more info

%matplotlib notebook # (If running in browser-hosted jupyter notebook)
#%matplotlib widget # (If running in vscode) and ensure you have "ipympl" module installed in your python environment
from ipywidgets import *
@CianMRoche
CianMRoche / ps_func.md
Created July 11, 2022 17:11
Powershell Profile Function: Port-Forwarded Jupyter Notebook

Notes

  • Acts like .bashrc on UNIX.

  • For use with running Jupyter notebook from compute nodes (see this gist) on windows machines.

Instructions

  1. Set up new powershell profile as in here but dont add the stuff they say to add (# Style default PowerShell Console and so on)

  2. Open profile in ISE like that tutorial says via ise $profile

@CianMRoche
CianMRoche / HPC-jupyter.md
Last active May 21, 2024 01:36
How to run Jupyter Notebooks on Computing Clusters, via ssh or in vscode

Running a Jupyter Notebook on a Computing Cluster

For some general cluster setup information, see this repo which was designed for the SubMIT cluster, or this repo for Stampede2. There should be some transferrable information within.

Options for running notebooks:

  1. In an IDE
    If it is supported, you may use jupyter notebooks in an IDE like visual studio code by installing the jupyter and python extensions, but it is currently often difficult to get this to function using the resources of compute nodes as opposed to the login nodes. See the above linked repos for setting up an IDE over ssh. Thankfully there are some methods to connect your IDE directly to the compute node, which will be described below. This approach provides the best coding experience when working.
  2. In your browser via ssh
    Manually run a notebook in an interactive compute node session on the cluster, forward the port on which the n