Skip to content

Instantly share code, notes, and snippets.

View matteo-peltarion's full-sized avatar

Matteo Barbieri matteo-peltarion

  • @Peltarion
View GitHub Profile
@matteo-peltarion
matteo-peltarion / rgb_to_gs_segmentation_mask.py
Created November 18, 2021 10:25
Script to convert RGB segmentation masks to grayscale (index-based) ones
#!/usr/bin/env python
# coding: utf-8
import os
from glob import glob
from PIL import Image
import numpy as np
@matteo-peltarion
matteo-peltarion / Hugginface-pt-tf-conversion.ipynb
Last active September 25, 2023 16:08
A Notebook showing how to convert Huggingface model from PyTorch and Tensorflow
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@matteo-peltarion
matteo-peltarion / covid19_analysis_06_plot_europe.py
Last active March 25, 2020 16:38
COVID-19 analysis code 04 - plot europe
# Utility function to retrieve data from a single country
def get_country_df(world_df, country_name):
# Some countries have several Provinces/States, must aggregate
country_df = world_df[world_df['Country/Region'] == country_name] \
.groupby(["Country/Region", "Date"]) \
.sum() \
.sort_values(by='Date')
# Restore columns
@matteo-peltarion
matteo-peltarion / covid19_analysis_05_plot_predictions.py
Last active March 25, 2020 16:38
COVID-19 analysis code 05 - plot predictions
# Imports for performing ML analysis
from sklearn.linear_model import LinearRegression
from scipy.optimize import curve_fit
from datetime import timedelta
# Set range of data to build model
# It might make sense to skip part of the initial points, when the exponential trend was still not evident
START_DATE = datetime(2020, 2, 23).date()
@matteo-peltarion
matteo-peltarion / covid19_analysis_04_plot_cases.py
Last active March 25, 2020 16:39
COVID-19 analysis code 04 - plot cases
# Estimate a reasonable value for Y tick given the maximum number of cases
Y_GRID_TICK = 10**int(np.log10(country_df['Confirmed'].max()))/2
ax = plt.gca()
sns.set_style("whitegrid", {'grid.linestyle': ':'})
ax.xaxis.set_major_locator(ticker.MultipleLocator(4))
# Plot
country_df.plot(x='Date', y=["Confirmed", "Deaths", "Recovered"], figsize=(18,9), ax=ax, marker='o')
@matteo-peltarion
matteo-peltarion / covid19_analysis_03_data_preprocessing.py
Last active March 23, 2020 17:05
COVID-19 analysis code 03 - data preprocessing
# Save the country's name in a variable, so that it can be easily changed to show data
# from another country.
country_name = "Italy"
# Get data for a single country, sorted by date
country_df = world_df[world_df['Country/Region'] == country_name].sort_values(by='Date')
# Compute daily increase, in absolute number
country_df['Increase'] = country_df['Confirmed'].diff()
@matteo-peltarion
matteo-peltarion / covid19_analysis_02_data_loading.py
Last active March 25, 2020 16:27
COVID-19 analysis code 02 - data loading
from datetime import datetime
csv_files = glob.glob(DATA_DIR + "/*.csv")
# Auxiliary function to extract date from file name
def extract_date(file_name):
date_str = os.path.basename(file_name)[:-4]
date = datetime.strptime(date_str, '%m-%d-%Y').date()
@matteo-peltarion
matteo-peltarion / covid19_analysis_setup.sh
Last active March 25, 2020 12:15
COVID-19 analysis code 01 - Environment setup
git clone https://github.com/CSSEGISandData/COVID-19
cd COVID-19
conda create --name jupyter python=3 matplotlib seaborn \
pandas scikit-learn
conda activate jupyter
conda install -c conda-forge jupyterlab
mkdir notebooks && cd notebooks
jupyter lab
@matteo-peltarion
matteo-peltarion / covid19_analysis_01_imports.py
Created March 23, 2020 14:00
COVID-19 analysis code 01 - Imports
# Basic imports for path
import os, sys, glob
# Data loading and
import pandas as pd
import numpy as np
# Plotting
import seaborn as sns
import matplotlib.pyplot as plt
"""Module to parse dataset."""
# Imports.
import os
from os.path import join
import glob
import pandas as pd
from PIL import Image
import numpy as np