def test_simulate(self): env = Environment(self.agents) statements = env.simulate(10) self.assertGreaterEqual(statements[-1]["timestamp"], 8, msg="The environment generated an unexpected" "low amount of data for a agent")
def test_statements_added_to_agent(self): env = Environment(self.agents, self.statements) student = PoissonStudent(name='John', lam=2) env.add_agent(student) self.assertEqual(len(env._agents[student.name].dt), len(env._statements[student.name]), msg="The agent statements and dt have different " "sizes")
def test_add_agent(self): env = Environment(self.agents, self.statements) student = PoissonStudent(name='John', lam=2) env.add_agent(student) self.assertEqual(len(env.agents), 2, msg="Adding an agent didn't change the number of" + "agent in the environment")
def test_fit(self): statement = { 'actor': u'123456789-1234-1234-1234-12345678901234', 'verb': u'http://adlnet.gov/expapi/verbs/completed', 'timestamp': 1519862425.0 } user_name = '123456789-1234-1234-1234-12345678901234' lam = Uniform('lam', lower=0, upper=1) s1 = PoissonStudent(user_name, lam=lam) env = Environment([s1], [statement]) env.add_agent(s1) res = env.fit([lam], method='mcmc') self.assertIsInstance(res, MCMC, msg="The output of fit is not an PYMC instance")
def test_environment(self): env = Environment(self.agents, self.statements) self.assertEqual(len(env._statements.keys()), 3, msg="The environment created a different" " number of agent than there is in" " the statements")
def main2(): data = load_file("statements-brneac3-20180301-20180531.json") statements = load_statements(data) user_name = "2890ebd9-1147-4f16-8a65-b7239bd54bd0" user_name = "2890ebd9-1147-4f16-8a65-b7239bd54bd0" lam = Uniform('lam', lower=0, upper=1) s1 = PoissonStudent(user_name, lam=lam) ## Creating environment and fitting data env = Environment([s1], statements) env.add_student(s1) res = env.fit([lam], method='mcmc') print(res) ## plotting. can be integrated in env hist(res.trace('lambda_{}'.format(user_name))[:]) show()
def test_filter_by_users(self): statements = load_statements(self.base_path + '/data/test/statements_sample.json') agents = load_agents(self.base_path + '/data/test/agents_sample.json') env = Environment(agents, statements) nodes, adjancy = env.nodes, env.structure active_agents = get_active_agents(statements) nodes, adjancy = filter_by_users(nodes, adjancy, active_agents)
def test_get_structure(self): g = [{ 'id': 1, 'label': 'level 2', 'type': 'level' }, { 'id': 2, 'label': 'CM2', 'type': 'class' }, { 'id': 3, 'label': 'CM1', 'type': 'class' }, { 'id': 4, 'label': 'Jules Ferry', 'type': 'school' }, { 'id': 5, 'label': 'Jean Jaures', 'type': 'school' }] t1 = Agent(name='t1', role=Role.TEACHER, groups=[g[3], g[1], g[2]]) t2 = Agent(name='t2', role=Role.TEACHER, groups=[g[0], g[4]]) s1 = Agent(name='s1', role=Role.STUDENT, groups=[g[1]]) s2 = Agent(name='s2', role=Role.STUDENT, groups=[g[1]]) s3 = Agent(name='s3', role=Role.STUDENT, groups=[g[1]]) s4 = Agent(name='s4', role=Role.STUDENT, groups=[g[2]]) s5 = Agent(name='s5', role=Role.STUDENT, groups=[g[2]]) s6 = Agent(name='s6', role=Role.STUDENT, groups=[g[0]]) agents = [t1, t2, s1, s2, s3, s4, s5, s6] env = Environment(agents) _, adjancy = env._get_structure(agents) expected_adjancy = { 4: {'t1'}, 't1': {2, 3}, 't2': {1}, 5: {'t2'}, 2: {'s1', 's2', 's3'}, 3: {'s5', 's4'}, 1: {'s6'} } self.assertEqual(adjancy, expected_adjancy)
def test_build_graph_from_data(self): agents = load_agents(self.base_path + '/data/test/agents_sample.json') env = Environment(agents) nodes, adjancy = env.nodes, env.structure tnodes = { 'a', 1, 2, 3, 5480, 17177, 19272, 30207, 30367, 'b', 'c', 'd', 'e', 102, '0951099D', 'f', 116, '0060138T', 'g', 123, 'h', 'i', 130 } self.assertEqual(set(nodes.keys()), tnodes) self.assertEqual( set(nodes.keys()).union(set(adjancy.keys())), set(nodes.keys()))
def main(): s1 = PoissonStudent("arnaud", 1) s2 = PoissonStudent("francois", 1) s3 = PoissonStudent("david", 0.5) students = [s1, s2, s3] env = Environment(students) statements = env.simulate(1000, verbose=True) student_names = set(s['actor'] for s in statements) lam = Uniform('lam', lower=0, upper=1) students = [PoissonStudent(name=name, lam=lam) for name in student_names] env = Environment(students, statements) params = [lam] for s in students: params.extend(s.params) m = MCMC(params) m.sample(iter=10000, burn=1000, thin=10) hist(m.trace('lambda_david')[:]) show()
def main(): filename = 'data/statements-brneac3-20180301-20180531.json' statements = load_file(filename) statements = load_statements(statements) filename = '../data/accounts-brneac3-0-20180630.json' data = load_agent_data(filename) agent, adjancy = get_structure(data) active_agents = get_active_agents(statements) clean_agent, clean_stuct = filter_by_users(agent, adjancy, active_agents) students = [ PoissonStudent(k, 1) for k, v in list(clean_agent.items())[:10] if v == 'user:eleve' ] env = Environment(agents=students, statements=statements, nodes=clean_agent, structure=clean_stuct) env.plot_group_activity('0680111B')
def test_initialization(self): names = ['a', 'b', 'c', 'd'] g = [{ 'id': 1, 'label': 'level 2', 'type': 'level' }, { 'id': 2, 'label': 'CM2', 'type': 'class' }, { 'id': 3, 'label': 'Jules Ferry', 'type': 'school' }] agents = [ Agent(name, groups=[g[i % 3]]) for i, name in enumerate(names) ] env = Environment(agents) self.assertEqual(env.groups, {group['id']: group for group in g})
'parameters') with open('./data/etab.json') as f: etab = json.load(f) with open('./data/params.json') as f: _params = json.load(f) statements = load_statements( '../open-student-environment/data' '/statements-brneac3-20180301-20180531' '.json') agents = load_agents( '../open-student-environment/data' '/accounts-brneac3-0-20180630.json') env = Environment(agents, statements) nodes, adjacency = env.nodes, env.structure active_agents = get_active_agents(statements[:25000]) nodes, adjacency = filter_by_users(nodes, adjacency, active_agents) adjacency = {str(k): v for k, v in adjacency.items()} etab = [e for e in etab if e['numero_uai'] in nodes] def create_nodes(adj, nds, func_name,env=env): schools = ["Annexe E.N.", "Lucie Aubrac", "Auriacombe", "Georges Bastide 1", "Georges Bastide 2", "Camille Claudel (anciennement Bellefontaine)", "Le Béarnais", "Maurice Bécanne", "Paul Bert", "Étienne Billières", "Bonnefoy", "Borderouge Nord", "Léonce Bourliaguet", "Buffon", "Buissonnière", "Calas", "Château d'Ancely", "Château de l'Hers", "Jean Chaubet", "Courrège", "Pierre et Marie Curie", "Cuvier", "André Daste", "Didier Daurat", "Sylvain Dauriac", "Jean Dieuzaide", "Françoise Dolto", "Armand Duportal", "Gaston Dupouy", "Fabre", "Clément Falcucci", "Falguière", "Daniel Faucher", "Jules Ferry", "Fleurance", "Fontaine Bayonne", "Fontaine Casselardit", "Alain Fournier", "Alexandre Fourtanier", "Anatole France", "Jean Gallia 1", "Jean Gallia 2", "Ginestous", "La Gloire", "Grand Selve", "Henri Guillaumet", "Hameau 47", "Victor Hugo", "Georges Hyon", "Maurice Jacquier", "Jean Jaurès", "Jolimont", "Jules Julien", "La Juncasse", "Lac (École du)", "Léo Lagrange", "Lakanal", "Lamartine", "Lapujade", "Lardenne", "Ferdinand de Lesseps", "Armand Leygue", "Limayrac-Jérôme Pugens", "Littré", "Jean Macé", "La Maourine", "Marengo-Périole", "Marengo-Reille", "Matabiau", "Merly", "Louise Michel", "Michelet", "Michoun A", "Michoun B", "Molière", "Monge", "Jean Monnet", "Montaudran", "Jean Moulin", "Moulis Croix-Bénite", "Alfred de Musset", "Nègreneys", "Niboul", "Les Oustalous", "Papus", "Patte-d'Oie", "Pech David", "Petit Gragnague", "Les Pinhous", "Polygone", "Port Garaud", "Pouvourville", "Rangueil", "Ernest Renan", "Ricardie", "Ronsard", "Saouzelong", "Sarrat", "Sept Deniers", "Hyacinthe Sermet", "Ariane Soupetard", "Tabar", "La Terrasse", "Les Tibaous", "Toec", "Elsa Triolet", "Les Vergers", "Viollet Le Duc"] locations = ['Paris', 'Créteil', 'Versaille', 'Cean'] grades = ['CM1', 'CM2', '6eme', '5eme'] def generate_school(): return choice(schools) def generate_grade():
def test_to_gdf(self): agents = load_agents(self.base_path + '/data/test/agents_sample.json') env = Environment(agents) env.to_gdf(filename=self.base_path + '/data/test-output.csv') os.remove(self.base_path + '/data/test-output.csv')
def test_get_students(self): agents = load_agents(self.base_path + '/data/test/agents_sample.json') env = Environment(agents) students = env._get_students('a', keep_inactive=True) self.assertEqual(students, {'b', 'c', 'h', 'd'})