Skip to content

Instantly share code, notes, and snippets.

@jorgensd
jorgensd / generate_team30_meshes.py
Last active March 1, 2022 23:15
Team 30 single phase engine with GMSH Python API
import gmsh
import numpy as np
from mpi4py import MPI
import argparse
rank = MPI.COMM_WORLD.rank
# http://www.compumag.org/jsite/images/stories/TEAM/problem30a.pdf
r1 = 0.02
@alexlib
alexlib / README.md
Created August 27, 2020 13:58 — forked from amroamroamro/README.md
[Python] Fitting plane/surface to a set of data points

Python version of the MATLAB code in this Stack Overflow post: http://stackoverflow.com/a/18648210/97160

The example shows how to determine the best-fit plane/surface (1st or higher order polynomial) over a set of three-dimensional points.

Implemented in Python + NumPy + SciPy + matplotlib.

quadratic_surface

@sancau
sancau / lru_with_ttl.py
Created July 19, 2020 13:35
Python LRU cache with TTL. Decorator.
import time
from functools import lru_cache
def lru_with_ttl(*, ttl_seconds, maxsize=128):
"""
A decorator to apply LRU in-memory cache to a function with defined maximum(!) TTL in seconds.
Be design an actual TTL may be shorter then the passed value (in rare randomized cases). But it can't be higher.
:param ttl_seconds: TTL for a cache record in seconds
@PaulEmmanuelSotir
PaulEmmanuelSotir / mlflow_log_repo_tags.py
Created June 8, 2020 20:47
Logs some special tags about repository informations to MLFlow, which turns to be usefull when running experiments without `mlfow run` CLI (allows better runs display information in MLFLow Web UI)
import re
import logging
import git
import mlflow
import configparser
def mlflow_log_repo_tags(default_project_name: str, entry_point: str = __file__, path_in_repository: str = __file__):
""" This code creates special mlflow tags about current repository infos, which is not done by mlflow when starting an MLFlow run from code instead of from `mlflow run` command
@patryk-oleniuk
patryk-oleniuk / artifact_viewer_web_embed.py
Created May 10, 2020 06:14
How to embed a website in mlflow artifact viewer
website_embed = '''<!DOCTYPE html>
<html>
<iframe src="https://en.wikipedia.org/wiki/Machine_learning" style='width: 700px; height: 450px' sandbox='allow-same-origin allow-scripts'>
</iframe>
</html>'''
with mlflow.start_run(experiment_id=1, run_name="website_embedding") as run:
with open("output.html", "w") as f:
f.write(website_embed)
mlflow.log_artifact("output.html")
@aricooperdavis
aricooperdavis / 3d_regression_example.py
Last active April 9, 2026 09:33
Example of 3D plots illustrating Linear Regression with 2 features and 1 target
import matplotlib.pyplot as plt
import numpy as np
import sklearn.linear_model
from mpl_toolkits.mplot3d import Axes3D
X_train = np.random.rand(2000).reshape(1000,2)*60
y_train = (X_train[:, 0]**2)+(X_train[:, 1]**2)
X_test = np.random.rand(200).reshape(100,2)*60
y_test = (X_test[:, 0]**2)+(X_test[:, 1]**2)
@raacampbell
raacampbell / polyN3_line.py
Created June 14, 2019 15:42
Fit a curve to a line in 3D
#!/usr/local/bin/python3
import numpy as np
import scipy.linalg
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
# some 3-dim points
n=20
xx=np.linspace(-12,5,num=n)
@raacampbell
raacampbell / polyN3.py
Created June 14, 2019 12:36
3D polynomial surface fit
#!/usr/local/bin/python3
# Make a 3d point cloud and fit a surface to it
import numpy as np
import scipy.linalg
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
@mivade
mivade / aiowatch.py
Created October 26, 2018 17:00
Using watchdog with asyncio
import asyncio
from pathlib import Path
from typing import Optional
from watchdog.events import FileSystemEvent, FileSystemEventHandler
from watchdog.observers import Observer
class _EventHandler(FileSystemEventHandler):
def __init__(self, queue: asyncio.Queue, loop: asyncio.BaseEventLoop,
@Susensio
Susensio / numpy_lru_cache.md
Last active November 26, 2024 21:25
Make function of numpy array cacheable

How to cache slow functions with numpy.array as function parameter on Python

TL;DR

from numpy_lru_cache_decorator import np_cache

@np_cache()
def function(array):
 ...