Skip to content

Instantly share code, notes, and snippets.

View phydev's full-sized avatar
:electron:
A minha história é um denso algoritmo.

Maurício Moreira-Soares phydev

:electron:
A minha história é um denso algoritmo.
View GitHub Profile
@phydev
phydev / write_vti.F90
Created September 26, 2019 09:59
Routine for writing vti files extracted from a class
subroutine grid_output(this, filename, other)
class(mesh) :: this
class(cell), optional, intent(in) :: other
character(len=20), intent(in) :: filename
integer :: tag, ip, L(3), nodes, s(3), icom(3), ip_new, gcom(3), lp(3), i
tag = 33423
L = this%L
nodes = this%nodes
@phydev
phydev / call_without_brackets.py
Last active October 20, 2019 01:30
creates a decorator that allows calling functions parameterless without empty brackets.
"""
creates a decorator that allows to call functions parameterless without empty brackets.
@author: phydev.github.io
date: Sunday, 20 october 2019 02:20
"""
def without_brackets(function):
"""
function that will be used as decorator for calling functions without using brackets
:param function: any function without arguments
@phydev
phydev / builtin_print.c
Last active October 25, 2019 20:22
Python3.7 print built-in function
builtin_print(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
{
static const char * const _keywords[] = {"sep", "end", "file", "flush", 0};
static struct _PyArg_Parser _parser = {"|OOOp:print", _keywords, 0};
PyObject *sep = NULL, *end = NULL, *file = NULL;
int flush = 0;
int i, err;
if (kwnames != NULL &&
!_PyArg_ParseStackAndKeywords(args + nargs, 0, kwnames, &_parser,
@phydev
phydev / allen-cahn.jl
Last active December 23, 2019 01:39
Integrating the allen-cahn equation using convolutions in Julia Language
"""
allen-cahn.jl
integration of the allen-cahn equation from the phase-field formalism using convolutions
@author: phydev.github.io
License: GNU GPL v3
"""
# importing external packages
using DSP # digital signal processing package
using Plots
@phydev
phydev / gen_dataset.py
Last active August 3, 2023 14:55
Script to generate a dataset of trajectories with 4 different mechanisms (normal diffusion, confined, anomalous and direct) with TrajPy
import trajpy.trajpy as tj
import trajpy.traj_generator as tjg
trajectory = 'anomalous'
r = np.zeros((250, 4))
n_steps = 250
n_samples = 5000
dt = 1.0
D = 100.
@phydev
phydev / center_of_mass.py
Last active June 10, 2020 16:14
This function calculates the center of mass for a field \phi(x,y,z) taking into consideration periodic boundary conditions.
import numpy as np
def center_of_mass(rho, L, ndim=2):
"""
:param rho: array N x M
:param L: array with the grid dimensions np.array([Lx, Ly])
:param ndim: number of dimensions
:return com: center of mass
"""
com, volume = np.zeros((ndim)), 0.0
"""
Convert sklearn dataset into pandas DataFrame object.
Source: https://stackoverflow.com/a/46379878/5059616
"""
import numpy as np
import pandas as pd
from sklearn import datasets
@phydev
phydev / compute_features.py
Last active August 11, 2020 20:22
compute features using trajpy
"""
Computes the set of features for each .dat file in PATH,
then loads a random forest classifier and predicts if the
trajectory is confined, normal, anomalous or superdiffusive.
The output is given inside PATH as a yyyy-mm-dd.csv file.
@phydev
"""
import os
@phydev
phydev / train_test_split_tensors.py
Last active August 19, 2020 19:09
This function adapts the sklearn.mode_selection.train_test_split function in order to accept tensorflow tensors as input and return tensors as well.
def train_test_split_tensors(X, y, **options):
"""
encapsulation for the sklearn.model_selection.train_test_split function
in order to split tensors objects and return tensors as output
:param X: tensorflow.Tensor object
:param y: tensorflow.Tensor object
:dict **options: typical sklearn options are available, such as test_size and train_size
"""
@phydev
phydev / lasso_regression.py
Last active October 21, 2020 08:44
Compute LASSO with tensorflow gradient.
"""
LASSO regression (L1 regularization) with gradient descent
TODO: estimate intercept
phydev.github.io
"""
def predict(X, beta):
"""
predict the regression
"""