This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class FakeContextManager: | |
| def __enter__(self): | |
| return self | |
| def __exit__(self, exc_type, exc_val, exc_tb): | |
| pass | |
| with FakeContextManager() as context: | |
| print("Hello") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # %% [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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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'] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| test_hypergraph = { | |
| 'nodes': ['a', 'b', 'c', 'd','e','f'], | |
| 'edges': [ | |
| ('a', 'b'), | |
| ('b', 'c'), | |
| ('c', 'd'), | |
| ('a', 'c'), | |
| ('b', 'f'), | |
| ('f', 'c'), | |
| ('e', 'f'), |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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': |
NewerOlder