Example #1
0
 def _enter_pass(self):
     print('Введите пароль:')
     password = input()
     if password == self.__user['pass']:
         print('Добро пожаловать в социальную сеть!\n')
         social_network = SocialNetwork(self.__start_page, self.__user)
         social_network.go_to_main_page()
     else:
         print('Неправильный пароль. Попробуйте еще раз.')
         self._enter_pass()
Example #2
0
    def fetch_social_networks(self):
        self.social_network = dict()

        for sn in self.social_networks:
            self.social_network[sn.name] = self.social_networks[sn]

        for sn in SocialNetwork.find_all():
            if sn.name not in self.social_network:
                self.social_network[sn.name] = ''
    def __init__(self, 
            main_params, 
            infection_dynamics_params,
            social_network_params,
            ct_params,
            at_params,
            st_params):
        self.n_agents = main_params['n_agents']
        self.agents = {idx:Agent(idx) for idx in range(self.n_agents)}
        self.infection = InfectionDynamics(self.agents, infection_dynamics_params)
        self.network = SocialNetwork(self.agents, social_network_params)
        self.quarantine = QuarantineManager(self.agents)
        self.use_ct = main_params['use_contact_tracing']
        self.ct = ContactTracing(self.agents, self.quarantine, ct_params)
        self.curr_day = 0

        self.use_at = main_params['use_adaptive_testing']
        self.at = AdaptiveTesting(self.agents, self.quarantine, self.network, self.infection, at_params)

        self.use_st = main_params['use_surveillance_testing']
        self.st = SurveillanceTesting(self.agents, self.infection, self.quarantine, st_params)
Example #4
0
 def __init__(self,
              languageFilter,
              ratingFilter,
              voteFilter,
              genreFilter,
              display=None):
     '''
     languageFilter must be a set of languages
     ratingFilter must be a tuple of two floats (lower bound, upper bound)
     voteFilter must be a tuple of two integers (lower bound, upper bound)
     genreFilter must be a tuple of two sets (genreSet, string), string
     should be either 'in' or 'out', if 'in': the genres in the genreSet
     will be taken into account; if 'out': genres other than the ones in
     the genreSet will be considered.
     '''
     self.languageFilter = languageFilter
     self.ratingFilter = ratingFilter
     self.voteFilter = voteFilter
     self.genreFilter = genreFilter
     self.movieList = []
     self.actorDict = {}
     SocialNetwork.__init__(self, display)
Example #5
0
def process_batch_input(batch_input_file, output_log, logger):
    global index

    #with open('.' + batch_input_file, 'r') as f:
    with open(batch_input_file, 'r') as f:
        for idx, line in enumerate(f.readlines()):
            #logger.info('File %s: Processing line numer: %d', batch_input_file, idx)
            line.rstrip()
            json_line = json.loads(line)
            if 'D' in json_line:
                degree = json_line['D']
                num_tracked = json_line['T']
                #logger.info('Degree of network = %s number of tracked purchases = %s', degree, num_tracked)
                social_network = SocialNetwork(output_log, degree, num_tracked)
            elif json_line['event_type'] == 'purchase':
                timestamp = json_line['timestamp']
                purchase_amount = float(json_line['amount'])
                purchase = (-1 * index, timestamp, purchase_amount)
                user_id = json_line['id']

                if not user_id in social_network.network:
                    # create user
                    user = User(user_id, num_tracked)
                    # add his purchase
                    user.add_purchase(purchase)
                    # add user to network
                    social_network.add_user(user)
                else:
                    user = social_network.network[user_id]
                    user.add_purchase(purchase)
            elif json_line['event_type'] in ['befriend', 'unfriend']:
                user1_id = json_line['id1']
                user2_id = json_line['id2']
                timestamp = json_line['timestamp']
                event_type = json_line['event_type']

                if event_type == 'befriend':
                    social_network.add_friend(user1_id, user2_id)
                elif event_type == 'unfriend':
                    social_network.remove_friend(user1_id, user2_id)
            index += 1
    return social_network
 def setUp(self):
     self.pandabook = SocialNetwork()
     self.ruja = Panda('Ruja', '*****@*****.**', 'female')
     self.martin = Panda('Martin', '*****@*****.**', 'male')
class TestSocialNetwork(unittest.TestCase):

    def setUp(self):
        self.pandabook = SocialNetwork()
        self.ruja = Panda('Ruja', '*****@*****.**', 'female')
        self.martin = Panda('Martin', '*****@*****.**', 'male')

    def test_social_network_add_panda(self):
        self.pandabook.add_panda(self.ruja)

        self.assertEqual(self.pandabook._get_graph()[self.ruja], set())

    def test_social_network_has_panda(self):
        self.pandabook.add_panda(self.martin)

        self.assertTrue(self.pandabook.has_panda(self.martin))
        self.assertFalse(self.pandabook.has_panda(self.ruja))

    def test_social_network_make_friends(self):
        self.pandabook.make_friends(self.ruja, self.martin)

        self.assertEqual(self.pandabook._get_graph()[self.ruja], {self.martin})
        self.assertEqual(self.pandabook._get_graph()[self.martin], {self.ruja})

    def test_social_netowrk_connection_level(self):
        random = Panda("Random", "*****@*****.**", "male")
        random1 = Panda("Random1", "*****@*****.**", "male")
        random2 = Panda("Random2", "*****@*****.**", "female")
        random3 = Panda("Random3", "*****@*****.**", "female")

        self.pandabook.make_friends(self.ruja, self.martin)
        self.pandabook.make_friends(self.ruja, random)
        self.pandabook.make_friends(self.martin, random1)
        self.pandabook.make_friends(self.martin, random2)
        self.pandabook.make_friends(random, random3)
        
        self.assertEqual(self.pandabook.connection_level(self.ruja, random3), 2)

        random_gay = Panda("RandomGay", "*****@*****.**", "female")
        self.pandabook.add_panda(random_gay)

        self.assertEqual(self.pandabook.connection_level(self.ruja, random_gay), -1)

    def test_social_network_are_connected(self):
        gosho = Panda("Gosho", "*****@*****.**", "male")
        krasi = Panda("Krasi", "*****@*****.**", "female")

        self.pandabook.make_friends(self.ruja, self.martin)
        self.pandabook.make_friends(self.martin, gosho)

        self.assertTrue(self.pandabook.are_connected(self.ruja, gosho))

        self.assertFalse(self.pandabook.are_connected(self.ruja, krasi))

    def test_social_network_how_many_gender_in_network(self):
        random = Panda("Random", "*****@*****.**", "male")
        random1 = Panda("Random1", "*****@*****.**", "male")
        random2 = Panda("Random2", "*****@*****.**", "female")
        random3 = Panda("Random3", "*****@*****.**", "female")

        self.pandabook.make_friends(self.ruja, self.martin)
        self.pandabook.make_friends(self.ruja, random)
        self.pandabook.make_friends(self.martin, random1)
        self.pandabook.make_friends(self.martin, random2)
        self.pandabook.make_friends(random, random3)

        self.assertEqual(self.pandabook.how_many_gender_in_network(1, self.ruja, "male"), 2)
        self.assertEqual(self.pandabook.how_many_gender_in_network(1, self.ruja, "female"), 1)
        self.assertEqual(self.pandabook.how_many_gender_in_network(2, self.ruja, "male"), 3)
Example #8
0
def main():
    pandabook = SocialNetwork()
    martin = Panda("Martin", "*****@*****.**", "male")
    rado = Panda("Rado", "*****@*****.**", "male")
    lubo = Panda("Lubo", "*****@*****.**", "male")
    dimitar = Panda("Dimitar", "*****@*****.**", "male")
    stanislav = Panda("Stanislav", "*****@*****.**", "male")
    denitsa = Panda("Denitsa", "*****@*****.**", "female")
    zornitsa = Panda("Zornitsa", "*****@*****.**", "female")
    iva = Panda("Iva", "*****@*****.**", "female")
    dora = Panda("Dora", "*****@*****.**", "female")
    aneliya = Panda("Aneliya", "*****@*****.**", "female")

    pandabook.make_friends(martin, lubo)
    pandabook.make_friends(martin, rado)
    pandabook.make_friends(rado, denitsa)
    pandabook.make_friends(rado, dimitar)
    pandabook.make_friends(denitsa, zornitsa)
    pandabook.make_friends(denitsa, iva)
    pandabook.make_friends(denitsa, dora)
    pandabook.make_friends(lubo, dimitar)
    pandabook.make_friends(lubo, stanislav)
    pandabook.make_friends(stanislav, aneliya)

    pandabook.save()
class MultiAgentSim:

    def __init__(self, 
            main_params, 
            infection_dynamics_params,
            social_network_params,
            ct_params,
            at_params,
            st_params):
        self.n_agents = main_params['n_agents']
        self.agents = {idx:Agent(idx) for idx in range(self.n_agents)}
        self.infection = InfectionDynamics(self.agents, infection_dynamics_params)
        self.network = SocialNetwork(self.agents, social_network_params)
        self.quarantine = QuarantineManager(self.agents)
        self.use_ct = main_params['use_contact_tracing']
        self.ct = ContactTracing(self.agents, self.quarantine, ct_params)
        self.curr_day = 0

        self.use_at = main_params['use_adaptive_testing']
        self.at = AdaptiveTesting(self.agents, self.quarantine, self.network, self.infection, at_params)

        self.use_st = main_params['use_surveillance_testing']
        self.st = SurveillanceTesting(self.agents, self.infection, self.quarantine, st_params)


    def step(self):
        self.curr_day += 1

        # step 1: simulate contacts & transmission dynamics for the day

        # iterate over all free & infected agents
        inf_agent_ids = self.infection.get_infected_agent_ids(self.curr_day)
        for agent_id in inf_agent_ids:
            if self.quarantine.is_agent_isolated(agent_id):
                continue

            # sample other agents w/ whom agent_id comes into contact
            contact_ids = self.network.sample_contacts(agent_id)

            # sample subset of contact agents who become infected based on their interaction w/ agent_id
            infectee_ids = self.infection.sample_transmissions(agent_id, contact_ids, self.curr_day)

            # record contacts for contact tracing
            if self.use_ct:
                self.ct.record_contacts(agent_id, contact_ids, self.curr_day)

            # start infection dynamics for newly infected agents
            for infectee_id in infectee_ids:
                if not self.quarantine.is_agent_isolated(infectee_id):
                    self.infection.infect_agent(infectee_id, self.curr_day)

        # next, sample self-reports from each agent
        self_report_ids = self.infection.sample_self_report_isolations(inf_agent_ids, self.curr_day)
        for self_report_id in self_report_ids:

            # when an agent self-reports they immediately go into quarantine
            if not self.quarantine.is_agent_isolated(self_report_id):
                self.quarantine.isolate_agent(self_report_id, self.curr_day)

            # we also begin the contact trace process for them
            if self.use_ct:
                self.ct.add_agent_to_trace_queue(self_report_id, self.curr_day)

            if self.use_at:
                self.at.trigger_adaptive_test(self_report_id, self.curr_day)

        # run surveillance tests
        st_positive_ids = self.st.step_surveillance_tests(self.curr_day)

        if self.use_ct:
            for agent_id in st_positive_ids:
                self.ct.add_agent_to_trace_queue(agent_id, self.curr_day)

        if self.use_at:
            for agent_id in st_positive_ids:
                self.at.trigger_adaptive_test(agent_id, self.curr_day)

        # process quarantine removals
        self.quarantine.step_isolation_removals(self.curr_day)

        # process contact-traces for the day
        if self.use_ct:
            self.ct.step_trace_queue(self.curr_day)

        if self.use_at:
            self.at.step_adaptive_tests(self.curr_day)