Beispiel #1
0
    def test_deviation_angle(self, input_df, expected_angle):
        """Verifies the deviation angle function in three different cases"""
        input_df["relative_angle"] = AgentMovement.vector_angles(
            input_df, ["x_relative", "y_relative"])
        angle = AgentMovement.deviation_angle(input_df)

        assert round(angle, 12) == round(expected_angle, 12)
Beispiel #2
0
    def test_update_velocities_group_field_and_indexes_not_None(
            self, fixture_update_velocities):
        """
        Comparison of the angles of the agents after applying update
        velocities through a Kolmogorov-smirnov test. `indexes` and
        `group_field` are set not None.
        """
        distribution = pytest.distrib
        sample_indexes = 500
        delta_angles = pytest.delta_angles
        df = DataFrame(pytest.data_large_sample)
        df["mobility_profile"] = "MG_1"
        mobility_group_slice = 300
        df.loc[0:mobility_group_slice - 1, "mobility_profile"] = "MG_2"
        angles_before = AgentMovement.vector_angles(df, ['vx', 'vy'])
        indexes = [i for i in range(sample_indexes + 1)]

        df = AgentMovement.update_velocities(df, distribution,
                                             pytest.angle_variance, indexes,
                                             "mobility_profile", "MG_2")
        angles_after = AgentMovement.vector_angles(df, ['vx', 'vy'])
        angle_variation_1 = angles_after[:mobility_group_slice] - \
            angles_before[:mobility_group_slice]
        k, p_1 = kstest(angle_variation_1.values, delta_angles)
        angle_variation_2 = angles_after[mobility_group_slice:] - \
            angles_before[mobility_group_slice:]
        k, p_2 = kstest(angle_variation_2.values, delta_angles)

        cond_1 = True if p_1 > 0.05 else False
        cond_2 = True if p_2 < 0.05 else False

        assert cond_1 and cond_2
Beispiel #3
0
    def test_movement_function_field_existence(self, fixture_raise_errors):
        """
        Raises an exception when the input DataFrame does not have any of
        the columns: 'x', 'y', 'vx' and 'vy'.
        """
        df = DataFrame(pytest.data_without_x)

        with pytest.raises(KeyError):
            AgentMovement.move_agents(df, pytest.box_size, pytest.dt)
Beispiel #4
0
 def test_stop_agents_raise_ValueError(self, fixture_stop_agents,
                                       fixture_raise_errors):
     """
     Raises an exception when the input DataFrame columns `vx` or `vy`
     does not exist.
     """
     df = DataFrame(pytest.data_wrong_name_columns)
     with pytest.raises(ValueError):
         AgentMovement.stop_agents(df, pytest.list_index_stopped)
Beispiel #5
0
    def test_set_velocities_raise_error(self, fixture_set_velocities):
        """
        Raises a ValueError when `vy` column is not in the input
        DataFrame.
        """
        distribution = fixture_set_velocities[0]

        df = DataFrame({"vx": [1, 2, 0, -1, -1]})

        with pytest.raises(ValueError):
            AgentMovement.set_velocities(df, distribution, 0.1)
Beispiel #6
0
    def test_initialize_velocities_ValueError_indexes_and_group_field_are_None(
            self, fixture_initialize_velocities):
        """
        Raises a ValueError when `vx` column is not in the input DataFrame,
        `indexes` and `group_field` are None.
        """
        distribution = fixture_initialize_velocities[0]
        angle_dist = fixture_initialize_velocities[1]

        df = DataFrame({"vx": random.randint(9, size=9)})

        with pytest.raises(ValueError):
            AgentMovement.initialize_velocities(df, distribution, angle_dist)
Beispiel #7
0
    def test_initialize_velocities_only_indexes_is_not_None(
            self, fixture_initialize_velocities):
        """
        Verifies whether `initialize_velocities` does not applies
        `set_velocities` when `group_label` is not in `group_field` column.
        """
        distribution = fixture_initialize_velocities[2]
        angle_dist = fixture_initialize_velocities[3]
        indexes = fixture_initialize_velocities[4]

        input_df = DataFrame({
            "vx":
            random.randint(9, size=9),
            "vy":
            random.randint(9, size=9),
            "mobility_group": [
                "MG_1", "MG_1", "MG_1", "MG_2", "MG_2", "MG_2", "MG_2", "MG_2",
                "MG_2"
            ]
        })
        df = AgentMovement.initialize_velocities(input_df, distribution,
                                                 angle_dist, indexes, None,
                                                 None)

        assert all(df["vx"][:5] == 1) & all(df["vy"][:5] == 0)
        assert all(input_df["vx"][5:] == df["vx"][5:]) & all(
            input_df["vy"][5:] == df["vy"][5:])
Beispiel #8
0
    def test_initialize_velocities_preserve_dtypes_dict(
            self, fixture_initialize_velocities):
        """
        Verifies whether `initialize_velocities` set the data types when
        a `preserve_dtypes_dict` is passed.
        """
        angle_dist = fixture_initialize_velocities[3]
        distribution = fixture_initialize_velocities[2]
        indexes = fixture_initialize_velocities[4]

        input_df = DataFrame({
            "vx":
            random.randint(9, size=9),
            "vy":
            random.randint(9, size=9),
            "mobility_group": [
                "MG_1", "MG_1", "MG_1", "MG_2", "MG_2", "MG_2", "MG_2", "MG_2",
                "MG_2"
            ]
        })
        preserve_dtypes_dict = {"vx": str, "vy": str}
        df = AgentMovement.initialize_velocities(input_df, distribution,
                                                 angle_dist, indexes,
                                                 "mobility_group", "MG_1",
                                                 preserve_dtypes_dict)

        assert all(df["vx"].apply(lambda vx: isinstance(vx, str)))
        assert all(df["vy"].apply(lambda vy: isinstance(vy, str)))
Beispiel #9
0
    def test_initialize_velocities_group_label_is_not_in_group_field(
            self, fixture_initialize_velocities):
        """
        Verifies whether `initialize_velocities` does not applies
        `set_velocities` when `group_label` is not in `group_field`.
        """
        angle_dist = fixture_initialize_velocities[3]
        distribution = fixture_initialize_velocities[2]
        indexes = fixture_initialize_velocities[4]

        input_df = DataFrame({
            "vx":
            random.randint(9, size=9),
            "vy":
            random.randint(9, size=9),
            "mobility_group": [
                "MG_1", "MG_1", "MG_1", "MG_2", "MG_2", "MG_2", "MG_2", "MG_2",
                "MG_2"
            ]
        })
        df = AgentMovement.initialize_velocities(input_df, distribution,
                                                 angle_dist, indexes,
                                                 "mobility_group", "MG_3")

        assert all(input_df["vx"][:].eq(df["vx"][:])) & all(
            input_df["vy"][:].eq(df["vy"][:]))
Beispiel #10
0
    def test_initialize_velocities_indexes_is_empty(
            self, fixture_initialize_velocities):
        """
        Verifies whether `initialize_velocities` does not applies
        `set_velocities` when `indexes` is an empty list.
        """
        angle_dist = fixture_initialize_velocities[3]
        distribution = fixture_initialize_velocities[2]

        input_df = DataFrame({
            "vx":
            random.randint(9, size=9),
            "vy":
            random.randint(9, size=9),
            "mobility_group": [
                "MG_1", "MG_1", "MG_1", "MG_2", "MG_2", "MG_2", "MG_2", "MG_2",
                "MG_2"
            ]
        })
        indexes = []
        df = AgentMovement.initialize_velocities(input_df, distribution,
                                                 angle_dist, indexes,
                                                 "mobility_group", "MG_1")

        assert all(input_df["vx"][:] == df["vx"][:]) & all(
            input_df["vy"][:] == df["vy"][:])
Beispiel #11
0
    def test_initialize_velocities_indexes_and_group_field_not_None_two_groups(
            self, fixture_initialize_velocities):
        """
        Verifies whether `initialize_velocities` applies `set_velocities` only
        to the agents in the indexes list passed and in the `group_field`
        specified.
        """
        angle_dist = fixture_initialize_velocities[3]
        distribution = fixture_initialize_velocities[2]
        indexes = fixture_initialize_velocities[4]

        input_df = DataFrame({
            "vx":
            random.randint(9, size=9),
            "vy":
            random.randint(9, size=9),
            "mobility_group": [
                "MG_1", "MG_1", "MG_1", "MG_2", "MG_2", "MG_2", "MG_2", "MG_2",
                "MG_2"
            ]
        })
        df = AgentMovement.initialize_velocities(input_df, distribution,
                                                 angle_dist, indexes,
                                                 "mobility_group", "MG_1")

        assert all(df["vx"][0:3] == 1) & all(df["vy"][0:3] == 0)
        assert all(input_df["vx"][3:] == df["vx"][3:]) & all(
            input_df["vy"][3:] == df["vy"][3:])
Beispiel #12
0
    def test_stop_agents(self, fixture_stop_agents):
        """Stops agent movement with a correct list of index."""
        df = DataFrame(pytest.data)
        df = AgentMovement.stop_agents(df, pytest.list_index_stopped)

        for i in range(len(pytest.list_index_stopped)):
            assert df['vx'][i] == df['vy'][i] == 0
Beispiel #13
0
    def test_crash_with_boundary_wall_(self, fixture_crash_with_wall):
        """Crash of one agent with each of the four box boundary wall."""
        df = DataFrame(pytest.data)
        df = AgentMovement.move_agents(df, pytest.box_size, pytest.dt)
        expected_df = DataFrame(pytest.expected_data)

        assert all(df == expected_df)
Beispiel #14
0
    def test_angle(self, x, y, expected_angle):
        """
        Verifies the standardized angle returned by the angle method
        of the agents with position components `x` and `y`.
        """
        angles = AgentMovement.angle(x, y)

        assert round(angles, decimals=10) == round(expected_angle, decimals=10)
Beispiel #15
0
    def test_update_velocities_angle_variance_zero_indexes_group_field_None(
            self, fixture_update_velocities):
        """
        Verifies whether there are no changes in the velocities components when
        the standard deviation of the numpy normal distribution is set to zero
        `angle_variance=0.0`. `indexes` and `group_field` is se to None.
        """
        df = DataFrame(pytest.data_large_sample)
        angles_before = AgentMovement.vector_angles(df, ['vx', 'vy'])

        df = AgentMovement.update_velocities(df, pytest.distrib, 0.0)
        angles_after = AgentMovement.vector_angles(df, ['vx', 'vy'])

        not_angle_variation = all(
            round(angles_before.head() - angles_after.head(), 7))

        assert not_angle_variation == 0
Beispiel #16
0
    def test_initialize_velocities_ValueError_indexes_and_group_field_not_None(
            self, fixture_initialize_velocities):
        """
        Raises a ValueError when `vx` column is not in the input DataFrame,
        `indexes` and `group_field` are not None.
        """
        distribution = fixture_initialize_velocities[0]
        angle_dist = fixture_initialize_velocities[1]

        df = DataFrame({
            "vy": random.randint(9, size=9),
            "mobility_group": ["MG_1" for i in range(9)]
        })

        with pytest.raises(ValueError):
            AgentMovement.initialize_velocities(df, distribution, angle_dist,
                                                "mobility_group", "MG_1")
Beispiel #17
0
    def test_vector_angles(self, input_df, expected_angle):
        """
        Calculates the angle position of the agents with components
        `x` and `y` of the input DataFrame.
        """
        angle = AgentMovement.vector_angles(input_df, ['x', 'y'])

        assert round(angle[0], decimals=10) == \
            round(expected_angle, decimals=10)
Beispiel #18
0
    def test_update_velocities_comparison_KS_test(self,
                                                  fixture_update_velocities):
        """
        Comparison of the angles of the agents after applying update
        velocities through a Kolmogorov-smirnov test. `indexes` and
        `group_field` are set to None.
        """
        df = DataFrame(pytest.data_large_sample)
        angles_before = AgentMovement.vector_angles(df, ['vx', 'vy'])
        df = AgentMovement.update_velocities(df, pytest.distrib,
                                             pytest.angle_variance)
        angles_after = AgentMovement.vector_angles(df, ['vx', 'vy'])
        angle_variation = angles_after - angles_before
        k, p = kstest(angle_variation.values, pytest.delta_angles)

        cond = True if p > 0.05 else False

        assert cond
Beispiel #19
0
    def test_avoid_agents_one_avoids_two(self,
                                         fixture_avoid_agents_same_rel_angles):
        """One agent avoids two agents with the same relative angles."""
        df = DataFrame(pytest.data)
        df_to_avoid = DataFrame(pytest.data_avoid)
        df = AgentMovement.avoid_agents(df, df_to_avoid)
        expected_angle = 5 * pi / 4

        assert round(df.vx[0], 12) == round(cos(expected_angle), 12)
        assert round(df.vy[0], 12) == round(sin(expected_angle), 12)
Beispiel #20
0
    def test_avoid_agents_raise_error(self, fixture_avoid_agents_raise_error):
        """
        Raises an exception when the input DataFrame column `agent`
        does not exist.
        """
        df = DataFrame(pytest.data_without_agent)
        df_to_avoid = DataFrame(pytest.data_avoid)

        with pytest.raises(ValueError):
            assert AgentMovement.avoid_agents(df, df_to_avoid)
Beispiel #21
0
    def test_avoid_agents_one_avoids_four_in_each_axis(
            self, fixture_avoid_agents_one_avoids_four_in_each_axis):
        """One agent avoids four agents in each axis."""
        df = DataFrame(pytest.data)
        df_to_avoid = DataFrame(pytest.data_avoid)
        df = AgentMovement.avoid_agents(df, df_to_avoid)
        expected_angle = pi / 4

        assert round(abs(df.vx[0]), 12) == round(cos(expected_angle), 12)
        assert round(abs(df.vy[0]), 12) == round(sin(expected_angle), 12)
Beispiel #22
0
    def test_update_velocities_under_field_indexes_None(
            self, fixture_update_velocities):
        """Updates velocities only for a group of agents under a field."""
        df = DataFrame(pytest.data_field)
        df = AgentMovement.update_velocities(df, pytest.distrib,
                                             pytest.angle_variance_field, None,
                                             "field", "field_1")
        velocities_field_vx = df.loc[1, 'vx'] != 2.0
        velocities_field_vy = df.loc[1, 'vy'] != 2.0

        assert velocities_field_vx and velocities_field_vy
Beispiel #23
0
    def test_update_velocities_raise_error_indexes_and_group_field_None(
            self, fixture_update_velocities):
        """
        Raises an exception when the input DataFrame columns `vx` and `vy`
        does not exist. `indexes` and `group_field` is se to None.
        """
        df = DataFrame(pytest.data_wrong_name_columns)

        with pytest.raises(ValueError):
            assert AgentMovement.update_velocities(df, pytest.distrib,
                                                   pytest.angle_variance)
Beispiel #24
0
    def test_avoid_agents_two_avoid_one_different_rel_angles(
            self, fixture_avoid_agents_different_rel_angles):
        """Two agents avoid one agent with different relative angles."""
        df = DataFrame(pytest.data)
        df_to_avoid = DataFrame(pytest.data_avoid)
        df = AgentMovement.avoid_agents(df, df_to_avoid)
        expected_angles = [3 * pi / 2, 7 * pi / 4]

        for i in range(len(expected_angles)):
            assert round(df.vx[i], 12) == round(cos(expected_angles[i]), 12)
            assert round(df.vy[i], 12) == round(sin(expected_angles[i]), 12)
Beispiel #25
0
    def test_avoid_agents_two_avoid_one(
            self, fixture_avoid_agents_two_agents_avoid_one):
        """Two agents avoid one agent with the same relative angle."""
        df = DataFrame(pytest.data)
        df_to_avoid = DataFrame(pytest.data_avoid)
        df = AgentMovement.avoid_agents(df, df_to_avoid)
        expected_angles = [pi / 2, pi / 2]

        for i in range(len(expected_angles)):
            assert round(df.vx[i], 12) == round(cos(expected_angles[i]), 12)
            assert round(df.vy[i], 12) == round(sin(expected_angles[i]), 12)
Beispiel #26
0
    def test_vector_angles_raise_error_correct_list(
            self, fixture_vector_angles_raise_error):
        """
        Raises an exception when the input DataFrame columns are not in the
        list, `vx` and `vy` in this case.
        """
        df = DataFrame(pytest.wrong_data)
        correct_list = pytest.correct_list

        with pytest.raises(ValueError):
            assert AgentMovement.vector_angles(df, correct_list)
Beispiel #27
0
    def test_vector_angles_raise_error_wrong_list(
            self, fixture_vector_angles_raise_error):
        """
        Raises an exception when the input list does not have correct columns
        names, `vx` and `vy` in this case.
        """
        df = DataFrame(pytest.data)
        wrong_list = pytest.wrong_list

        with pytest.raises(ValueError):
            assert AgentMovement.vector_angles(df, wrong_list)
Beispiel #28
0
    def test_avoid_agents_one_avoids_two_different_rel_angles(
            self, fixture_avoid_agents_one_avoids_two):
        """
            One agent avoids two agents with different relative angles."""
        df = DataFrame(pytest.data)
        df_to_avoid = DataFrame(pytest.data_avoid)
        new_df = AgentMovement.avoid_agents(df, df_to_avoid)
        condition = round(new_df.vx[new_df.agent == 1][0] - -1.0, 7) == round(
            new_df.vy[new_df.agent == 1][0] - 0, 7) == 0

        assert condition
Beispiel #29
0
    def test_set_velocities(self, fixture_set_velocities):
        """
        Verifies whether set_velocities method assigns correctly new
        velocities to the agents of a  DataFrame, using a constant distribution
        for the norm of the velocities and a None `angle_distribution`.
        """
        distribution = fixture_set_velocities[0]

        df = DataFrame({"vx": [1, 2, 0, -1, -1], "vy": [0, 2, 1, 0, 1]})
        df = AgentMovement.set_velocities(df, distribution, 0.1)
        expected_df = DataFrame({"vx": pytest.vx, "vy": pytest.vy})

        assert all(round(df, 10) == round(expected_df, 10))
Beispiel #30
0
    def test_replace_velocities_norm_equal_zero(
            self, fixture_replace_velocities_norm_equal_to_zero):
        """
        Verifies the replace velocity function when one agent has its
        velocity norm equal to zero.
        """
        new_angle = Series(pytest.new_angle)
        df = DataFrame(pytest.data)
        df = df.apply(
            lambda row: AgentMovement.replace_velocities(row, new_angle),
            axis=1)

        assert df.vx[0] == df.vy[0] == 0