Skip to content

Instantly share code, notes, and snippets.

View mikics's full-sized avatar

Michele Castriotta mikics

  • Specialvideo
  • Imola
View GitHub Profile
"""
Image segmentation using a trivial implementation of Lloyd's k-means algorithm in numpy
"""
import numpy as np
import cv2 as cv
# INPUTS
k = 5 # k-means number of clusters
eps = 1e-8 # loop ending condition
@mikics
mikics / random_polygon.py
Created February 1, 2023 14:11
This gist creates a random polygon starting from a set of random points
import matplotlib.pyplot as plt
import numpy as np
num_points = 10
x, y = np.random.uniform(-1, 1, size=(2, num_points))
# Calculate the angle of each point with respect to the origin
theta = np.arctan2(y, x) * 180 / np.pi
@mikics
mikics / pi_montecarlo.py
Last active January 31, 2023 17:45
Simple python script implementing the Monte-Carlo method for a numerical evaluation of pi
import matplotlib.pyplot as plt
import numpy as np
# Number of random x,y points to generate
num_samples = 1000000
# Set the seed for reproducibility
np.random.seed(42)
# Generate random x,y points in the unit square
@mikics
mikics / mpi_demo.py
Last active February 7, 2023 15:32
Demo showing how to use ther MPI.COMM_SELF communicator to parallelize parametric problems in FEniCSx. In particular, the code solves a nonlinear Burgers' equation with stochastic viscosity.
# Copyright (C) 2023 Michele Castriotta
import math
import sys
from functools import partial
import numpy as np
from mpi4py import MPI
from petsc4py import PETSc
@mikics
mikics / singularity_4_dolfinx.def
Created September 30, 2022 14:08
Singularity definition file for running FEniCSx on HPC cluster
# Copyright (C) 2022 Fabio Grasso, Michele Castriotta
#
# Fabio Grasso:
# f.grasso@isac.cnr.it
#
# Michele Castriotta
# mich.castriotta@gmail.com
Bootstrap: library
@mikics
mikics / png_to_gif.py
Last active July 30, 2022 16:10
This gist shows how to convert .png images into a .gif with python
# From https://pythonprogramming.altervista.org/png-to-gif/
import subprocess
import os
i = "*.png"
o = "output.gif"
subprocess.call("convert -delay 4 -loop 0 " + i + " " + o, shell=True)
os.system("start output.gif")
@mikics
mikics / pv_time_harmonic_animation.py
Last active July 30, 2022 16:09
This gist shows how to create multiple .png images showing the time-harmonic motion of an electromagnetic field saved as an ADIOS output file (.bp)
# trace generated using paraview version 5.10.1
#import paraview
#paraview.compatibility.major = 5
#paraview.compatibility.minor = 10
#### import the simple module from the paraview
from paraview.simple import *
#### disable automatic camera reset on 'Show'
paraview.simple._DisableFirstRenderCameraReset()
@mikics
mikics / fano_resonance.py
Created February 4, 2022 15:47
Snippet for plotting the Fano resonance function in an interactive window, with two sliders for the width and the asymmetric (or Fano) factor.
"""
For the slider, I've used the solution at this link:
https://stackoverflow.com/questions/6697259/interactive-matplotlib-plot-with-two-sliders
"""
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.widgets import Slider, Button, RadioButtons
def fano_func(w, w0, gamma, F):
@mikics
mikics / lorentzian.py
Created February 4, 2022 14:54
Snippet for plotting the Lorentzian function in an interactive window, with a slider for the width of the Lorentzian.
"""
For the slider, I've used the solution at this link:
https://stackoverflow.com/questions/6697259/interactive-matplotlib-plot-with-two-sliders
"""
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.widgets import Slider, Button, RadioButtons
w0 = 0
#!/usr/bin/env python3
import os, sys, gi
gi.require_version('Wnck', '3.0')
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, Wnck
# Edit this as you like with your mpv options
mpv_title = 'MPV Wallpaper'
mpv_options = '--no-audio --loop --no-input-default-bindings --no-osc'