def test_create_plot_geolife(self, get_geolife_triplegs_with_modes):
     """Check if we can run the plot function with geolife data without error"""
     modal_split = calculate_modal_split(get_geolife_triplegs_with_modes,
                                         freq='d',
                                         per_user=False)
     plot_modal_split(modal_split)
     assert True
    def test_multi_user_error(self, get_test_triplegs_with_modes):
        """Create a modal split plot based on randomly generated test data"""
        modal_split = calculate_modal_split(get_test_triplegs_with_modes,
                                            freq='d',
                                            per_user=True,
                                            norm=True)
        with pytest.raises(ValueError):
            plot_modal_split(modal_split)

        # make sure that there is no error if the data was correctly created
        modal_split = calculate_modal_split(get_test_triplegs_with_modes,
                                            freq='d',
                                            per_user=False,
                                            norm=True)
        plot_modal_split(modal_split)

        assert True
    def test_run_modal_split_with_geolife(self, read_geolife_with_modes):
        """check if we can run all possible combinations with a small sample of the geolife data"""

        tpls = read_geolife_with_modes
        metric_list = ['duration', 'distance', 'count']
        freq_list = [None, 'D', 'W-MON']
        per_user_list = [False, True]
        norm_list = [True, False]

        for metric in metric_list:
            for freq in freq_list:
                for per_user in per_user_list:
                    for norm in norm_list:
                        calculate_modal_split(tpls, metric=metric, freq=freq, per_user=per_user, norm=norm)

        # we only check if it runs through successfully
        assert True
    def test_modal_split_total_distance(self, test_triplegs_modal_split):
        """Check distances per user and mode without temporal binning"""
        tpls = test_triplegs_modal_split
        modal_split = calculate_modal_split(tpls, metric='distance', per_user=True)

        # if the users would get merged, walk would be larger than bike
        assert modal_split.loc[0, 'bike'] > modal_split.loc[0, 'walk']
        assert modal_split.loc[0, 'walk'] > modal_split.loc[0, 'car']
    def test_modal_split_total_duration(self, test_triplegs_modal_split):
        """Check duration per user and mode without temporal binning"""
        tpls = test_triplegs_modal_split
        modal_split = calculate_modal_split(tpls, metric='duration', per_user=True)

        assert np.isclose(modal_split.loc[0, 'bike'], datetime.timedelta(minutes=1).total_seconds())
        assert np.isclose(modal_split.loc[0, 'car'], datetime.timedelta(hours=1).total_seconds())
        assert np.isclose(modal_split.loc[0, 'walk'], datetime.timedelta(hours=2).total_seconds())
        assert np.isclose(modal_split.loc[1, 'walk'], datetime.timedelta(hours=2).total_seconds())
    def test_modal_split_total_no_user_count(self, test_triplegs_modal_split):
        """Check counts without users and without temporal binning"""
        tpls = test_triplegs_modal_split
        modal_split = calculate_modal_split(tpls, metric='count', per_user=False)

        # if the user get merged, walk would be larger than bike
        assert modal_split.loc[0, 'bike'] == 1
        assert modal_split.loc[0, 'car'] == 1
        assert modal_split.loc[0, 'walk'] == 4
    def test_modal_split_total_count(self, test_triplegs_modal_split):
        """Check counts per user and mode without temporal binning"""
        tpls = test_triplegs_modal_split
        modal_split = calculate_modal_split(tpls, metric="count", per_user=True)

        # if the user get merged, walk would be larger than bike
        assert modal_split.loc[0, "bike"] == 1
        assert modal_split.loc[0, "car"] == 1
        assert modal_split.loc[0, "walk"] == 2
        assert modal_split.loc[1, "walk"] == 2
    def test_check_dtype_error(self, get_geolife_triplegs_with_modes):
        """Check if error is thrown correctly when index is not datetime

        freq=None calculates the modal split over the whole period
        """
        modal_split = calculate_modal_split(get_geolife_triplegs_with_modes,
                                            freq=None,
                                            per_user=False)
        with pytest.raises(ValueError):
            plot_modal_split(modal_split)
        assert True
    def test_modal_split_weekly_count(self, test_triplegs_modal_split):
        """Check counts per user and mode binned by week"""
        tpls = test_triplegs_modal_split
        modal_split = calculate_modal_split(tpls, metric='count', freq='W-MON', per_user=True)

        w_1 = pd.Timestamp('1970-01-05 00:00:00', tz='utc')  # data is aggregated to the next monday
        w_2 = pd.Timestamp('1970-01-12 00:00:00', tz='utc')

        assert modal_split.loc[[(0, w_1)], 'bike'][0] == 1
        assert modal_split.loc[[(0, w_1)], 'car'][0] == 1
        assert modal_split.loc[[(0, w_1)], 'walk'][0] == 1
        assert modal_split.loc[[(0, w_2)], 'walk'][0] == 1
        assert modal_split.loc[[(1, w_1)], 'walk'][0] == 2
    def test_modal_split_weekly_count(self, test_triplegs_modal_split):
        """Check counts per user and mode binned by week"""
        tpls = test_triplegs_modal_split
        modal_split = calculate_modal_split(tpls, metric="count", freq="W-MON", per_user=True)

        w_1 = pd.Timestamp("1970-01-05 00:00:00", tz="utc")  # data is aggregated to the next monday
        w_2 = pd.Timestamp("1970-01-12 00:00:00", tz="utc")

        assert modal_split.loc[[(0, w_1)], "bike"][0] == 1
        assert modal_split.loc[[(0, w_1)], "car"][0] == 1
        assert modal_split.loc[[(0, w_1)], "walk"][0] == 1
        assert modal_split.loc[[(0, w_2)], "walk"][0] == 1
        assert modal_split.loc[[(1, w_1)], "walk"][0] == 2
    def test_modal_split_daily_count(self, test_triplegs_modal_split):
        """Check counts per user and mode binned by day"""
        tpls = test_triplegs_modal_split
        modal_split = calculate_modal_split(tpls, metric='count', freq='D', per_user=True)

        t_1 = pd.Timestamp('1970-01-01 00:00:00', tz='utc')
        t_2 = pd.Timestamp('1970-01-02 00:00:00', tz='utc')
        t_8 = pd.Timestamp('1970-01-08 00:00:00', tz='utc')

        assert modal_split.loc[[(0, t_1)], 'bike'][0] == 1
        assert modal_split.loc[[(0, t_1)], 'car'][0] == 1
        assert modal_split.loc[[(0, t_2)], 'walk'][0] == 1
        assert modal_split.loc[[(0, t_8)], 'walk'][0] == 1
        assert modal_split.loc[[(1, t_1)], 'walk'][0] == 2
    def test_modal_split_daily_count(self, test_triplegs_modal_split):
        """Check counts per user and mode binned by day"""
        tpls = test_triplegs_modal_split
        modal_split = calculate_modal_split(tpls, metric="count", freq="D", per_user=True)

        t_1 = pd.Timestamp("1970-01-01 00:00:00", tz="utc")
        t_2 = pd.Timestamp("1970-01-02 00:00:00", tz="utc")
        t_8 = pd.Timestamp("1970-01-08 00:00:00", tz="utc")

        assert modal_split.loc[[(0, t_1)], "bike"][0] == 1
        assert modal_split.loc[[(0, t_1)], "car"][0] == 1
        assert modal_split.loc[[(0, t_2)], "walk"][0] == 1
        assert modal_split.loc[[(0, t_8)], "walk"][0] == 1
        assert modal_split.loc[[(1, t_1)], "walk"][0] == 2
    def test_create_plot_testdata(self, get_test_triplegs_with_modes):
        """Create a modal split plot based on randomly generated test data"""
        tmp_file = os.path.join('tests', 'data', 'modal_split_plot.png')

        modal_split = calculate_modal_split(get_test_triplegs_with_modes,
                                            freq='d',
                                            per_user=False,
                                            norm=True)

        modal_split = modal_split[['walk', 'bike', 'train', 'car', 'bus'
                                   ]]  # change order for the looks of the plot
        plot_modal_split(modal_split,
                         out_path=tmp_file,
                         date_fmt_x_axis='%d',
                         y_label='Percentage of daily count',
                         x_label='days')

        assert os.path.exists(tmp_file)
        os.remove(tmp_file)
        os.remove(tmp_file.replace('png', 'pdf'))
    def test_create_plot_testdata(self, get_test_triplegs_with_modes):
        """Create a modal split plot based on randomly generated test data"""
        tmp_file = os.path.join("tests", "data", "modal_split_plot.png")

        modal_split = calculate_modal_split(get_test_triplegs_with_modes,
                                            freq="d",
                                            per_user=False,
                                            norm=True)

        modal_split = modal_split[["walk", "bike", "train", "car", "bus"
                                   ]]  # change order for the looks of the plot
        plot_modal_split(modal_split,
                         out_path=tmp_file,
                         date_fmt_x_axis="%d",
                         y_label="Percentage of daily count",
                         x_label="days")

        assert os.path.exists(tmp_file)
        os.remove(tmp_file)
        os.remove(tmp_file.replace("png", "pdf"))