Skip to content

Instantly share code, notes, and snippets.

View Stefano314's full-sized avatar
👋
Hi there!

Stefano Bianchi Stefano314

👋
Hi there!
View GitHub Profile
@Stefano314
Stefano314 / linearRegression.py
Created July 26, 2023 07:54
Simple Linear Regression
class LinearRegression:
"""
Simple linear regression using the Ordinary Least Square method:
Coefs = (x.T * X)^-1 * x.T * y
where Coefs[0] = q, Coefs[1] = m
"""
def __init__(self, x, y): # Just give X and Y
@Stefano314
Stefano314 / ConnectomeClass.py
Created June 1, 2023 07:32
Connectome Class for Brain Grapsh
import networkx as nx
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
class Connectome:
def __init__(self, G, quantities = None, time = None):
self.G = G
@Stefano314
Stefano314 / Grid3D.py
Created May 31, 2023 10:19
Grid3D Class
import numpy as np
import matplotlib.pyplot as plt
class Grid3D:
def __init__(self, pointsPosition, boxSubdivision):#, cellSize):
self._minCorner, self._maxCorner = self._GetCorners(pointsPosition)
self.cellSize = [0,0,0]#cellSize
self.boxSubdivision = boxSubdivision
@Stefano314
Stefano314 / mask_to_obj.py
Last active March 12, 2023 00:51
Create 3D Object (obj) Mesh from a Numpy array Mask. Useful to visualize brain Images in Virtual Reality, with Blender for instance.
import numpy as np
import trimesh
def array_mask_mesh(A : np.ndarray, radius=1., width=0.05, edges = False, path : str='test.obj', volume='cube'):
"""
Create a 3D object of a (N,M,L) array mask
"""
centers = np.argwhere(A)
radius = [radius]*centers.shape[0]
@Stefano314
Stefano314 / heat_eq_network.py
Last active December 11, 2022 22:33
Heat Equation and Random walk on Networks. Some easy and general function to simulate and visualize heat equation/random walks on network.
import networkx as nx
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.cm as colormap
heat_equation = True
random_walk_diffusion = False
np.random.seed(1)
@Stefano314
Stefano314 / random_walk.cpp
Last active August 24, 2022 21:30
Random Walk on Edgelist Format in C++ //// Random Walk on Network in C++ //// Cover Time, Hitting Time
// READ THE COMMENT FOR ADDITIONAL INFORMATION
#include <iostream>
#include <vector>
#include <fstream>
#include <sstream>
#include <algorithm> // for "erase-remove idiom", unique, remove
#include <set>
#include <random> // for unifrom distribution using mt19937
#include <ctime> // for random seed
@Stefano314
Stefano314 / dictionaries_union.py
Created June 30, 2022 07:54
Create the union of a generic list of dictionaries.
def flatten_list(lst : list) -> list:
"""
Description
-----------
Flatten an irregular list of lists (meaning that it can contain also non lists). If there are multiple nested lists
the function must be used as many times as the nested order.
"""
result = []
@Stefano314
Stefano314 / pandas_infer_connections.py
Created June 19, 2022 14:42
Pandas DataFrame internal connections inference.
from tqdm import tqdm
from pandas.util.testing import assert_frame_equal
import pandas as pd
import numpy as np
def df_equal(df1, df2):
"""
Description
-----------
@Stefano314
Stefano314 / fast_combinations.py
Last active June 19, 2022 14:44
Fast way to get the combination pair list of the elements in a list.
import numpy as np
def fast_combinations(row : list, self_loops = False) -> np.array:
"""
Description
-----------
Fast way to obtain the combination of all the elements inside a list using Numpy.
Self connections are present.
Parameters
@Stefano314
Stefano314 / edgelist_generation.py
Last active June 19, 2022 14:50
Create an edgelist from a Pandas Dataframe (in a form of Synonyms Table).
import pandas as pd
import numpy as np
from tqdm import tqdm
def fast_combinations(row : list, self_loops = False) -> np.array:
"""
Description
-----------
Fast way to obtain the combination of all the elements inside a list using Numpy.
Self connections are present.