Skip to content

Commit

Permalink
black style
Browse files Browse the repository at this point in the history
  • Loading branch information
leo-desbureaux-tellae committed Mar 7, 2024
1 parent 53fc567 commit 7f8ae51
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 43 deletions.
1 change: 1 addition & 0 deletions bhepop2/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,7 @@ def build_cross_table(pop: pd.DataFrame, names_attribute: list):

return table_percentage


# check functions


Expand Down
4 changes: 1 addition & 3 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,7 @@ def filosofi_distributions_nantes(test_insee_code):

@pytest.fixture(scope="session")
def filosofi_global_distribution_nantes(filosofi_distributions_nantes):
return filosofi_distributions_nantes[
filosofi_distributions_nantes["modality"] == "all"
]
return filosofi_distributions_nantes[filosofi_distributions_nantes["modality"] == "all"]


@pytest.fixture(scope="function")
Expand Down
15 changes: 11 additions & 4 deletions tests/sources/test_base.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
from bhepop2.sources.global_distribution import QuantitativeGlobalDistribution,QuantitativeAttributes
from bhepop2.sources.global_distribution import (
QuantitativeGlobalDistribution,
QuantitativeAttributes,
)

import pytest

Expand All @@ -9,8 +12,13 @@ def test_init(self, filosofi_global_distribution_nantes, mocker):
test_name = "test_name"

# mock feature values evaluation and validation
mocker.patch("bhepop2.sources.global_distribution.QuantitativeGlobalDistribution._validate_data")
mocker.patch("bhepop2.sources.global_distribution.QuantitativeGlobalDistribution._evaluate_feature_values", return_value=[1, 2, 3])
mocker.patch(
"bhepop2.sources.global_distribution.QuantitativeGlobalDistribution._validate_data"
)
mocker.patch(
"bhepop2.sources.global_distribution.QuantitativeGlobalDistribution._evaluate_feature_values",
return_value=[1, 2, 3],
)

source = QuantitativeGlobalDistribution(filosofi_global_distribution_nantes, name=test_name)

Expand Down Expand Up @@ -43,4 +51,3 @@ def test_delta_min_check(self):
with pytest.raises(AssertionError):
QuantitativeAttributes(delta_min=None)
QuantitativeAttributes(delta_min=-1)

19 changes: 9 additions & 10 deletions tests/sources/test_global_distribution.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ def test_init(self, filosofi_global_distribution_nantes, test_parameters):
relative_max = 1.5

global_distribution = QuantitativeGlobalDistribution(
filosofi_global_distribution_nantes, name=test_name, abs_minimum=abs_min, relative_maximum=relative_max
filosofi_global_distribution_nantes,
name=test_name,
abs_minimum=abs_min,
relative_maximum=relative_max,
)

# check class attributes
Expand All @@ -29,22 +32,18 @@ def test_validate_data(self, filosofi_global_distribution_nantes):

# DataFrame with missing decile should raise an error
with pytest.raises(AssertionError):
QuantitativeGlobalDistribution(
filosofi_global_distribution_nantes.drop(["D5"], axis=1)
)
QuantitativeGlobalDistribution(filosofi_global_distribution_nantes.drop(["D5"], axis=1))

# empty DataFrame should raise an error
with pytest.raises(AssertionError):
QuantitativeGlobalDistribution(
filosofi_global_distribution_nantes.drop([filosofi_global_distribution_nantes.index[0]], axis=0)
filosofi_global_distribution_nantes.drop(
[filosofi_global_distribution_nantes.index[0]], axis=0
)
)

def test_get_value_for_feature(self, filosofi_global_distribution_nantes, test_seed, mocker):
rng = default_rng(test_seed)
global_distribution = QuantitativeGlobalDistribution(
filosofi_global_distribution_nantes
)
global_distribution = QuantitativeGlobalDistribution(filosofi_global_distribution_nantes)

assert round(global_distribution.get_value_for_feature(5, rng), 2) == 23526.75


60 changes: 34 additions & 26 deletions tests/sources/test_marginal_distributions.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import pandas as pd

from bhepop2.sources.marginal_distributions import QuantitativeMarginalDistributions, QualitativeMarginalDistributions
from bhepop2.sources.marginal_distributions import (
QuantitativeMarginalDistributions,
QualitativeMarginalDistributions,
)
from bhepop2.analysis import QuantitativeAnalysis, QualitativeAnalysis
from bhepop2.functions import build_cross_table

Expand All @@ -15,18 +18,15 @@ def test_init(self, filosofi_distributions_nantes, test_modalities):
attribute_selection = list(test_modalities.keys())

source = QuantitativeMarginalDistributions(
filosofi_distributions_nantes,
attribute_selection=attribute_selection
filosofi_distributions_nantes, attribute_selection=attribute_selection
)

# check class attributes
assert source.attribute_selection == attribute_selection
assert source.modalities == test_modalities

def test_get_modality_distribution(self, filosofi_distributions_nantes):
source = QuantitativeMarginalDistributions(
filosofi_distributions_nantes
)
source = QuantitativeMarginalDistributions(filosofi_distributions_nantes)

modality_distribution = source.get_modality_distribution("ownership", "Tenant")

Expand All @@ -39,19 +39,25 @@ class TestQuantitativeMarginalDistributions:

@pytest.fixture(scope="class")
def quantitative_marginal_distribution(self, filosofi_distributions_nantes):
return QuantitativeMarginalDistributions(
filosofi_distributions_nantes,
delta_min=1000
)
return QuantitativeMarginalDistributions(filosofi_distributions_nantes, delta_min=1000)

def test_feature_values(self, quantitative_marginal_distribution):

assert len(quantitative_marginal_distribution.feature_values) == 50
assert round(sum(quantitative_marginal_distribution.feature_values)/len(quantitative_marginal_distribution.feature_values), 2) == 40296.2
assert (
round(
sum(quantitative_marginal_distribution.feature_values)
/ len(quantitative_marginal_distribution.feature_values),
2,
)
== 40296.2
)

def test_compute_feature_prob(self, quantitative_marginal_distribution):

feature_prob = quantitative_marginal_distribution.compute_feature_prob("ownership", "Tenant")
feature_prob = quantitative_marginal_distribution.compute_feature_prob(
"ownership", "Tenant"
)

assert len(feature_prob) == quantitative_marginal_distribution.nb_feature_values
assert set(feature_prob.columns) == {"feature", "prob"}
Expand All @@ -64,16 +70,15 @@ def test_get_value_for_feature(self, quantitative_marginal_distribution, test_se

assert round(value, 2) == 10589.92

def test_compare_with_populations(self, quantitative_marginal_distribution, test_feature_name, mocker):
def test_compare_with_populations(
self, quantitative_marginal_distribution, test_feature_name, mocker
):
# avoid the analysis table evaluation, we just want the class instance
mocker.patch("bhepop2.analysis.PopulationAnalysis._evaluate_analysis_table")
enriched = pd.DataFrame({
test_feature_name: [10, 20]
})
enriched = pd.DataFrame({test_feature_name: [10, 20]})

analysis = quantitative_marginal_distribution.compare_with_populations(
{"pop": enriched},
feature_name=test_feature_name
{"pop": enriched}, feature_name=test_feature_name
)

assert isinstance(analysis, QuantitativeAnalysis)
Expand Down Expand Up @@ -105,7 +110,12 @@ def qualitative_marginal_distribution(self, car_distributions):
)

def test_feature_values(self, qualitative_marginal_distribution):
assert qualitative_marginal_distribution.feature_values == ['0voit', '1voit', '2voit', '3voit']
assert qualitative_marginal_distribution.feature_values == [
"0voit",
"1voit",
"2voit",
"3voit",
]

def test_compute_feature_prob(self, qualitative_marginal_distribution):

Expand All @@ -120,22 +130,20 @@ def test_get_value_for_feature(self, qualitative_marginal_distribution, test_see

assert value == qualitative_marginal_distribution.feature_values[2]

def test_compare_with_populations(self, qualitative_marginal_distribution, test_feature_name, mocker):
def test_compare_with_populations(
self, qualitative_marginal_distribution, test_feature_name, mocker
):
# avoid the analysis table evaluation, we just want the class instance
mocker.patch("bhepop2.analysis.PopulationAnalysis._evaluate_analysis_table")
enriched = pd.DataFrame({
test_feature_name: [10, 20]
})
enriched = pd.DataFrame({test_feature_name: [10, 20]})

analysis = qualitative_marginal_distribution.compare_with_populations(
{"pop": enriched},
feature_name=test_feature_name
{"pop": enriched}, feature_name=test_feature_name
)

assert isinstance(analysis, QualitativeAnalysis)



# def test_qualitative_enrich(
# pop_synt_men_nantes,
# distributions,
Expand Down

0 comments on commit 7f8ae51

Please sign in to comment.