def test_actor_filter():
    data_file = reader.InputDataFile()
    data_file.parse_rows([
        [types.PartialActor.starts_with, 'Actor #1', 'description'],
        [types.PartialActor.starts_with, 'Actor #2', 'description'],
        [types.PartialActor.starts_with, 'Actor #3', 'description'],
    ])

    factory = ModelFactory(data_file, actor_whitelist=['Actor #2', 'Actor #3'])

    assert len(factory.filter_actors()) == 2
    assert 'Actor #1' not in factory.filter_actors()
def test_issue_filter():
    data_file = reader.InputDataFile()
    data_file.parse_rows([
        [types.PartialIssue.starts_with, 'Issue #1', 'description'],
        [types.PartialIssue.starts_with, 'Issue #2', 'description'],
        [types.PartialIssue.starts_with, 'Issue #3', 'description'],
    ])

    factory = ModelFactory(data_file, issue_whitelist=['Issue #2', 'Issue #3'])

    assert len(factory.filter_issues()) == 2
    assert 'Issue #1' not in factory.filter_issues()
def sample_model():
    date_file = InputDataFile.open(
        os.path.join(input_folder, 'sample_data.txt'))

    factory = ModelFactory(date_file=date_file)

    return factory(EqualGainModel)
示例#4
0
def model_factory():
    input_data = """'#A'	'Actor-1'	'Actor-1'	''	
'#A'	'Actor-2'	'Actor-2'	''	
'#P'	'Issue-1'	'Issue-1'	'Issue-1'	
'#M'	'Issue-1'	0	
'#M'	'Issue-1'	100	
'#P'	'Issue-2'	'Issue-2'	'Issue-2'	
'#M'	'Issue-2'	0	
'#M'	'Issue-2'	100	
'#D'	'Actor-1'	'Issue-1'	0.0	10	1	
'#D'	'Actor-1'	'Issue-2'	0.0	90	1	
'#D'	'Actor-2'	'Issue-1'	100.0	60	1	
'#D'	'Actor-2'	'Issue-2'	100.0	50	1"""

    reader = csv.reader(input_data.splitlines(), delimiter="\t", quotechar="'")

    data_file = InputDataFile.open_reader(reader)

    assert len(data_file.actors) == 2
    assert len(data_file.actor_issues) == 4

    model_factory = ModelFactory(data_file)

    return model_factory
def test_actor_issue_filter():
    actor_1 = 'Actor #1'
    actor_2 = 'Actor #2'
    actor_3 = 'Actor #3'

    actors = [actor_1, actor_2, actor_3]
    actor_whitelist = actors[1:]

    issue_1 = 'Issue #1'
    issue_2 = 'Issue #2'
    issue_3 = 'Issue #3'

    issues = [issue_1, issue_2, issue_3]
    issue_whitelist = issues[1:]

    data = []

    for issue in issues:
        data.append([types.PartialIssue.starts_with, issue, issue])

    for actor in actors:
        data.append([types.PartialActor.starts_with, actor, actor])

        for issue in issues:
            data.append(
                [types.ActorIssue.starts_with, actor, issue, 100, 1, 1])

    data_file = reader.InputDataFile()
    data_file.parse_rows(data)
    data_file.update_issues_with_positions()

    assert len(data_file.errors) == 0
    assert len(data_file.actors) == 3
    assert len(data_file.issues) == 3
    assert len(data_file.actor_issues) == 9

    factory = ModelFactory(date_file=data_file,
                           actor_whitelist=actor_whitelist,
                           issue_whitelist=issue_whitelist)

    filtered_issues = factory.filter_issues()
    filtered_actors = factory.filter_actors()

    assert len(filtered_issues) == 2
    assert len(filtered_actors) == 2

    filtered_actor_issues = factory.filter_actor_issues(
        filtered_actors, filtered_issues)

    assert len(filtered_actor_issues) == 4

    model = factory(EqualGainModel)

    assert len(model.issues) == len(filtered_issues)
    assert len(model.actors) == len(filtered_actors)
    assert len(model.actor_issues) == 2

    count = sum(len(x) for x in model.actor_issues.values())

    assert count == len(filtered_actor_issues)
    assert isinstance(model, EqualGainModel)
示例#6
0
def main():
    args = parse_arguments()

    p_values = p_values_param(args)
    issues = issues_param(args)
    actors = actors_param(args)

    data_file = InputDataFile.open(args.input_file)

    data_set_name = os.path.splitext(
        os.path.basename(args.name or args.input_file))[0]

    factory = ModelFactory(data_file,
                           actor_whitelist=actors,
                           issue_whitelist=issues)
    """
        Initial the right model from the given arguments
        """

    if args.model == "equal":
        model_klass = equalgain.EqualGainModel
    else:
        model_klass = randomrate.RandomRateModel

    for p in p_values:

        start_time = datetime.now()  # for timing operations

        model = factory(model_klass=model_klass, randomized_value=p)

        output_directory = init_output_directory(
            args.output_dir,
            data_set_name,
            p,
        )

        # The event handlers for logging and writing the results to the disk.
        event_handler = init_event_handlers(
            model=model,
            output_directory=output_directory,
            database_file=args.database,
            write_csv=True,
        )

        event_handler.log(
            message="Start calculation at {0}".format(start_time))

        event_handler.log(message="Parsed file".format(args.input_file))

        event_handler.before_repetitions(repetitions=args.repetitions,
                                         iterations=args.iterations)

        for repetition in range(args.repetitions):

            model = factory(model_klass=model_klass, randomized_value=p)

            event_handler.update_model_ref(model)

            model_loop = ModelLoop(model, event_handler, repetition)

            event_handler.before_iterations(repetition)

            for iteration_number in range(args.iterations):
                logging.info("round {0}.{1}".format(repetition,
                                                    iteration_number))
                model_loop.loop()

            event_handler.after_iterations(repetition)

        event_handler.after_repetitions()

        event_handler.log(message="Finished in {0}".format(datetime.now() -
                                                           start_time))

    logging.info("Done")
示例#7
0
    def run_model(self):
        settings = self.settings

        factory = ModelFactory(
            date_file=InputDataFile.open(settings.input_filename),
            actor_whitelist=settings.selected_actors,
            issue_whitelist=settings.selected_issues,
        )

        parent_output_directory = init_output_directory(*settings.output_path)

        model = factory(model_klass=EqualGainModel, randomized_value=settings.model_variations[0])

        safe_model_as_input(model, os.path.join(parent_output_directory, "input.csv"))
        safe_settings_as_csv(self.settings, os.path.join(parent_output_directory, "settings.csv"))

        event_handler = init_event_handlers(model, parent_output_directory, settings)

        event_handler.before_model()

        model_variations = list(settings.model_variations)

        for variation, p in enumerate(model_variations, 0):

            output_directory = init_output_directory(
                parent_output_directory,
                p
            )

            event_handler.update_output_directory(output_directory)

            event_handler.before_repetitions(
                repetitions=settings.repetitions,
                iterations=settings.iterations,
                randomized_value=p,
            )

            start_time = time.time()

            for repetition in range(settings.repetitions):

                model = factory(model_klass=EqualGainModel, randomized_value=p)

                event_handler.update_model_ref(model)

                model_loop = ModelLoop(model, event_handler, repetition)

                event_handler.before_iterations(repetition)

                for iteration_number in range(settings.iterations):

                    if self.break_loop:
                        break

                    self.update.emit(variation, repetition, iteration_number, start_time)

                    model_loop.loop()

                event_handler.after_iterations(repetition)

                if self.break_loop:
                    break

            if not self.break_loop:
                event_handler.after_repetitions()

                logging.info('tie count is {}'.format(model.tie_count))

        if not self.break_loop:
            event_handler.update_output_directory(parent_output_directory)
            event_handler.after_model()
            print(len(AbstractModel.eui))
            print(statistics.variance(AbstractModel.eui))
        self.finished.emit(parent_output_directory)