Skip to content

Instantly share code, notes, and snippets.

View yassineAlouini's full-sized avatar
⚙️
PyTorch Exploration...

Yassine Alouini yassineAlouini

⚙️
PyTorch Exploration...
View GitHub Profile
@yassineAlouini
yassineAlouini / localize_df.py
Last active February 13, 2018 14:09
Localize a timestamp column
import pytz
import pandas as pd
def localize_datetime(input_df, timezone='Europe/Paris',
datetime_column='tms_gmt'):
"""
Convert datetime column from UTC to another timezone.
"""
tmz = pytz.timezone(timezone)
df = input_df.copy()
@yassineAlouini
yassineAlouini / future_imports.py
Created January 18, 2018 09:35
Useful future imports for Python 2.7
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals
@yassineAlouini
yassineAlouini / carto_bokeh_map.py
Created February 5, 2018 16:28
Use CartoDB as a tile provider for Bokeh
# Inspired from here: https://bokeh.pydata.org/en/latest/docs/user_guide/geo.html
from bokeh.tile_providers import CARTODBPOSITRON
from bokeh.io import output_file, show
from bokeh.plotting import figure
bound = 20000000 # meters
fig = figure(tools='pan, wheel_zoom', x_range=(-bound, bound), y_range=(-bound, bound))
fig.axis.visible = False
fig.add_tile(CARTODBPOSITRON)
show(fig)
@yassineAlouini
yassineAlouini / elo.py
Last active February 2, 2019 11:01
Script to update the ELO score after a new game. Works with two teams of multiple players.
import elo
import pandas as pd
from slacker import Slacker
from tabulate import tabulate
import emoji
slack = Slacker('SLACK_API_KEY') # API key
test = False
game = True
K_FACTOR = 40
@yassineAlouini
yassineAlouini / multindex_histogram.py
Created February 21, 2018 09:38
Histogram computation on a multi-index
import pandas as pd
df = pd.DataFrame({'t': [1, 2, 3, 4, 5, 6], 'sid': [1, 1, 1, 2, 2, 2], 'time': [1, 2, 2, 2, 1, 1]})
hist_df = df.groupby(['sid', 'time']).t.value_counts(bins=[1, 2, 3, 10]).reset_index(name='counts')
@yassineAlouini
yassineAlouini / random_choice_to_geojson.py
Created February 28, 2018 15:55
Add a random choice column to GeoJSON file
import geopandas as gpd
import numpy as np
# Set a seed for reproducibility
np.random.seed(0)
input_path = 'input/path/to/geojson'
output_path = 'output/path/to/geojson'
choices = [1, 2, 3]
@yassineAlouini
yassineAlouini / expand_grid.py
Last active March 22, 2018 14:31
Create a DataFrame from all the possible combination of a dict
import pandas as pd
import itertools
import numpy as np
# From here: https://pandas.pydata.org/pandas-docs/stable/cookbook.html#creating-example-data
def expand_grid(data_dict):
rows = itertools.product(*data_dict.values())
return pd.DataFrame.from_records(rows, columns=data_dict.keys())
@yassineAlouini
yassineAlouini / vectorized_gis.py
Created April 27, 2018 12:22
Some vectorized GIS operations
from shapely import wkb
import numpy as np
from functools import wraps
def vectorize(**vectorize_kwargs):
""" A (numpy) vectorizing decorator
"""
def _vectorize(func):
@wraps(func)
def wrapper(*args, **kwargs):
@yassineAlouini
yassineAlouini / dfs_difference.py
Created May 17, 2018 08:34
Show DataFrame differences
# Inspired from here: https://stackoverflow.com/questions/17095101/outputting-difference-in-two-pandas-dataframes-side-by-side-highlighting-the-d
def _show_dfs_difference(df1, df2):
ne_stacked = (df1 != df2).stack()
difference_locations = np.where(df1 != df2)
changed = ne_stacked[ne_stacked]
changed_from = df1.values[difference_locations]
changed_to = df2.values[difference_locations]
return pd.DataFrame({'from': changed_from, 'to': changed_to}, index=changed.index)
@yassineAlouini
yassineAlouini / vectorization.py
Created June 20, 2018 13:25
Some vectorized GIS operations
# -*- coding: utf-8 -*-
from functools import wraps
import numpy as np
from shapely import wkb
def vectorize(**vectorize_kwargs):
""" A (numpy) vectorizing decorator