Skip to content

Instantly share code, notes, and snippets.

View xalelax's full-sized avatar

Alessandro Angioi xalelax

View GitHub Profile
class FakeContextManager:
def __enter__(self):
return self
def __exit__(self, exc_type, exc_val, exc_tb):
pass
with FakeContextManager() as context:
print("Hello")
@xalelax
xalelax / requirements.txt
Created November 22, 2023 13:23
Requirements for Neptune+Pymc post
appnope==0.1.3
arrow==1.3.0
arviz==0.16.1
asttokens==2.4.1
attrs==23.1.0
backcall==0.2.0
black==23.11.0
bokeh==3.3.0
boto3==1.28.80
botocore==1.31.80
@xalelax
xalelax / pymc_and_neptune.py
Last active November 26, 2023 21:40
Bayesian inference + Neptune for logging artifacts
# %% [markdown]
# # Generate synthetic data
# %%
import numpy as np
np.random.seed(123)
N_SAMPLES = 500
x = np.random.uniform(low=-1, high=1, size=N_SAMPLES)
from itertools import product
faces = list(range(1, 7))
def contains_consecutive_sixes(outcome):
for i, j in zip(outcome, outcome[1:]):
if i == j == 6:
return True
import random
def generate_random_hypergraph(order, size):
nodes = set(range(order))
edges = set()
while len(edges) < size:
edge_size = random.randint(2, order)
edge = frozenset(random.sample(nodes, k=edge_size))
if edge not in edges:
edges.add(edge)
import networkx as nx
from networkx import NetworkXException
import matplotlib.pyplot as plt
def plot_hypergraph_components(hypergraph):
decomposed_graph = decompose_edges_by_len(hypergraph)
decomposed_edges = decomposed_graph['edges']
nodes = decomposed_graph['nodes']
from collections import defaultdict
def decompose_edges_by_len(hypergraph):
decomposed_edges = defaultdict(list)
for edge in hypergraph['edges']:
decomposed_edges[len(edge)].append(edge)
decomposition = {
'nodes': hypergraph['nodes'],
'edges': decomposed_edges
}
@xalelax
xalelax / test_hypergraph.py
Created December 27, 2020 18:26
A hypergraph with no particular meaning, just for the sake of definiteness
test_hypergraph = {
'nodes': ['a', 'b', 'c', 'd','e','f'],
'edges': [
('a', 'b'),
('b', 'c'),
('c', 'd'),
('a', 'c'),
('b', 'f'),
('f', 'c'),
('e', 'f'),
@xalelax
xalelax / simulate.py
Created October 26, 2020 13:22
Simulate function for PRG model
from model import Company
def simulate_sample_company(n_steps, **kwargs):
"""
Runs a model for n_steps and returns a pandas.DataFrame
containing the data collected at each step.
kwargs are forwarded to init of company.
"""
model = Company(**kwargs)
for i in range(n_steps):
@xalelax
xalelax / model.py
Created October 26, 2020 11:46
Model company for PRG model; selected methods
class Company(Model):
"""
Model of a company; pyramidal structure with n different levels;
workers at different levels weigh differently on the efficiency of the
company as a whole.
"""
def pick_for_promotion_from(self, source_level):
if self.promotion_strategy == 'best':
return max(source_level, key=lambda e: e.competency)
elif self.promotion_strategy == 'worst':