Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ADD: Model tests #209

Merged
merged 53 commits into from
Jan 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
53 commits
Select commit Hold shift + click to select a range
92fd73d
Merge branch 'main' of github.com:artefactory/choice-learn-private
VincentAuriau May 17, 2024
8e8bca4
Merge branch 'main' of github.com:artefactory/choice-learn-private
VincentAuriau May 23, 2024
b3455e2
Merge branch 'main' of github.com:artefactory/choice-learn-private
VincentAuriau May 28, 2024
aa35f2f
Merge branch 'main' of github.com:artefactory/choice-learn-private
VincentAuriau May 30, 2024
5fbbaca
Merge branch 'main' of github.com:artefactory/choice-learn-private
VincentAuriau Jun 25, 2024
c8ec7c2
Merge branch 'main' of github.com:artefactory/choice-learn-private
VincentAuriau Jul 2, 2024
18d6c25
ENH: logos in ReadMe
VincentAuriau Jul 3, 2024
19f04af
Merge branch 'main' of github.com:artefactory/choice-learn-private
VincentAuriau Jul 4, 2024
03c5b35
merge
VincentAuriau Jul 8, 2024
57bdf4b
poetry lock [--no-update]Merge branch 'main' of github.com:artefactor…
VincentAuriau Jul 31, 2024
1266ad9
Merge branch 'main' of github.com:artefactory/choice-learn-private
VincentAuriau Aug 21, 2024
053977d
Merge branch 'main' of github.com:artefactory/choice-learn-private
VincentAuriau Sep 11, 2024
52b2356
Merge branch 'main' of github.com:artefactory/choice-learn-private
VincentAuriau Oct 21, 2024
78517ec
Merge branch 'main' of github.com:artefactory/choice-learn-private
VincentAuriau Oct 22, 2024
3ff9d54
Merge branch 'main' of github.com:artefactory/choice-learn-private
VincentAuriau Dec 23, 2024
1597ca3
Merge branch 'main' of github.com:artefactory/choice-learn-private
VincentAuriau Dec 23, 2024
4bb4b7c
Merge branch 'main' of github.com:artefactory/choice-learn-private
VincentAuriau Dec 27, 2024
63816d7
FIX: split features
VincentAuriau Dec 27, 2024
d7f1c8e
ADD: datasets tests
VincentAuriau Dec 27, 2024
cc7cf5e
FIX: pre-commits
VincentAuriau Dec 27, 2024
1e34764
FIX: pre-commits, again
VincentAuriau Dec 27, 2024
3c3a8d4
FIX: pre-commits, again
VincentAuriau Dec 27, 2024
a311224
FIX: pre-commits, again
VincentAuriau Dec 27, 2024
cd9a7d4
ADD: additional data tests
VincentAuriau Dec 28, 2024
7fdb59b
ADD: additional data tests
VincentAuriau Dec 28, 2024
d1c3b43
FIX: choice_format
VincentAuriau Dec 28, 2024
2aaa74e
FIX: linting
VincentAuriau Dec 28, 2024
f104688
ADD: basic unit tools tests
VincentAuriau Dec 28, 2024
7434773
ADD: basic unit tools tests
VincentAuriau Dec 28, 2024
a6b1cc7
ADD: ortools in requirements
VincentAuriau Dec 28, 2024
eadf632
ADD: errors raised tests
VincentAuriau Dec 28, 2024
7054e33
ADD: errors raised tests
VincentAuriau Dec 28, 2024
b19e639
ADD: additional tests for Assortment Optimizers
VincentAuriau Dec 28, 2024
9606de1
ADD: additional tests for Assortment Optimizers
VincentAuriau Dec 28, 2024
72004a5
FIX&ADD: parametrizations of optimization algorithms
VincentAuriau Dec 28, 2024
1631b2b
FIX&ADD: parametrizations of optimization algorithms
VincentAuriau Dec 28, 2024
64d5311
ADD: basic unit tastenet tests
VincentAuriau Dec 29, 2024
104c6f7
merge
VincentAuriau Jan 3, 2025
7308371
ADD: more ResNet tests
VincentAuriau Jan 3, 2025
5364db2
ADD: few tests
VincentAuriau Jan 3, 2025
9dcaee5
pre-commits
VincentAuriau Jan 3, 2025
e114be7
pre-commits
VincentAuriau Jan 3, 2025
6a3e6f4
pre-commits
VincentAuriau Jan 3, 2025
a99aeff
pre-commits
VincentAuriau Jan 3, 2025
beba758
FIX test
VincentAuriau Jan 3, 2025
8a3c6a7
FIX & use of Path
VincentAuriau Jan 3, 2025
3a84ca2
fix precommits
VincentAuriau Jan 3, 2025
a38b302
fix precommits
VincentAuriau Jan 3, 2025
ed6e0b8
fix: compatibility
VincentAuriau Jan 3, 2025
f074ef6
fix
VincentAuriau Jan 3, 2025
9586cb9
fix
VincentAuriau Jan 3, 2025
11edb76
ADD: few tests
VincentAuriau Jan 3, 2025
fed1211
fix
VincentAuriau Jan 3, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions choice_learn/datasets/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import gzip
import os
from importlib import resources
from pathlib import Path

import numpy as np
import pandas as pd
Expand All @@ -12,7 +13,7 @@
from choice_learn.data.choice_dataset import ChoiceDataset

OS_DATA_MODULE = os.path.join(os.path.abspath(".."), "choice_learn", "datasets", "data")
DATA_MODULE = "choice_learn.datasets.data"
DATA_MODULE = "choice_learn/datasets/data"


def get_path(data_file_name, module=DATA_MODULE):
Expand All @@ -35,10 +36,12 @@ def get_path(data_file_name, module=DATA_MODULE):
import sys

if sys.version >= "3.9":
return resources.files(module) / data_file_name
return resources.files(module.replace("/", ".")) / data_file_name

with resources.path(module, data_file_name) as path:
return path
# with resources.path(module, data_file_name) as path:
# return path
path = Path(module).resolve() / data_file_name
return path.as_posix()


def load_csv(data_file_name, data_module=DATA_MODULE, encoding="utf-8"):
Expand Down Expand Up @@ -140,7 +143,7 @@ def download_from_url(url):
full_path = get_path(local_filename, module=DATA_MODULE)

# Check that the file is not already downloaded in the DATA_MODULE directory
if not os.path.isfile(full_path):
if not Path.is_file(Path(full_path)):
print(f"Downloading {local_filename} from {url}")
try:
with requests.get(url, stream=True, timeout=20) as r:
Expand All @@ -152,7 +155,7 @@ def download_from_url(url):
print(f"Couldn't download automatically the dataset from {url}")

# Move the downloaded file to the DATA_MODULE directory
os.rename(local_filename, full_path)
Path(local_filename).rename(full_path)
print(f"Download completed. File saved as {local_filename} in {full_path}")

return local_filename
Expand Down
6 changes: 3 additions & 3 deletions choice_learn/datasets/expedia.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
from choice_learn.data.storage import OneHotStorage
from choice_learn.datasets.base import get_path

DATA_MODULE = "choice_learn.datasets.data"
CACHE_MODULE = "choice_learn.datasets.cache"
DATA_MODULE = "choice_learn/datasets/data"
CACHE_MODULE = "choice_learn/datasets/cache"


def load_expedia(as_frame=False, preprocessing="rumnet"):
Expand All @@ -26,7 +26,7 @@ def load_expedia(as_frame=False, preprocessing="rumnet"):
"""
filename = "expedia.csv"
data_path = get_path(filename, module=DATA_MODULE)
if not Path.exists(data_path):
if not Path.exists(Path(data_path)):
print("In order to use the Expedia dataset, please download it from:")
print("https://www.kaggle.com/c/expedia-personalized-sort")
print("and save it in the following location:")
Expand Down
2 changes: 1 addition & 1 deletion choice_learn/datasets/tafeng.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from choice_learn.data.choice_dataset import ChoiceDataset
from choice_learn.datasets.base import get_path

DATA_MODULE = "choice_learn.datasets.data"
DATA_MODULE = "choice_learn/datasets/data"


def load_tafeng(as_frame=False, return_desc=False, preprocessing=None):
Expand Down
11 changes: 11 additions & 0 deletions tests/integration_tests/models/test_reslogit.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
"""Tests ResLogit."""

import numpy as np
import pytest
import tensorflow as tf

from choice_learn.datasets import load_swissmetro

# from choice_learn.models import ResLogit, SimpleMNL
from choice_learn.models import ResLogit
from choice_learn.models.reslogit import ResLayer

dataset = load_swissmetro()
dataset = dataset[:100] # Reduce the dataset size for faster testing
Expand Down Expand Up @@ -352,3 +354,12 @@ def test_that_endpoints_run():
model.evaluate(dataset, mode="optim")
model.predict_probas(dataset)
assert True


def test_activation():
"""Tests ResLogit activation."""
layer = ResLayer()
for act in ["linear", "relu", "-relu", "softplus", "tanh", "sigmoid"]:
_ = layer.get_activation_function(act)
with pytest.raises(ValueError):
layer.get_activation_function("abc")
9 changes: 9 additions & 0 deletions tests/unit_tests/datasets/test_download.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
"""Test the file downloading function."""

from choice_learn.datasets.base import download_from_url


def test_download():
"""Tests downloading a dummy csv file."""
url = "https://github.com/artefactory/choice-learn/blob/main/tests/data/test_data.csv"
download_from_url(url)
146 changes: 146 additions & 0 deletions tests/unit_tests/models/test_tastenet.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
"""Simple tests for the TasteNet model."""

import pytest

from choice_learn.datasets import load_swissmetro
from choice_learn.models import TasteNet

customers_id, dataset = load_swissmetro(preprocessing="tastenet", as_frame=False)
dataset = dataset[:20]

taste_net_layers = []
taste_net_activation = "relu"
items_features_by_choice_parametrization = [
[-1.0, "-exp", "-exp", 0.0, "linear", 0.0, 0.0],
[-1.0, "-exp", "-exp", "linear", 0.0, "linear", 0.0],
[-1.0, "-exp", 0.0, 0.0, 0.0, 0.0, 0.0],
]


def test_activation():
"""Tests TasteNet activation."""
tastenet = TasteNet(
taste_net_layers=taste_net_layers,
taste_net_activation=taste_net_activation,
items_features_by_choice_parametrization=items_features_by_choice_parametrization,
optimizer="Adam",
epochs=40,
lr=0.001,
batch_size=32,
)
for act in ["linear", "relu", "-relu", "exp", "-exp", "tanh", "sigmoid"]:
_ = tastenet.get_activation_function(act)
with pytest.raises(ValueError):
tastenet.get_activation_function("abc")


def test_fit_adam_and_predict():
"""Test fit with Gradient Descent."""
tastenet = TasteNet(
taste_net_layers=taste_net_layers,
taste_net_activation=taste_net_activation,
items_features_by_choice_parametrization=items_features_by_choice_parametrization,
optimizer="Adam",
epochs=5,
lr=0.001,
batch_size=32,
)
_ = tastenet.fit(dataset)
_ = tastenet.predict_tastes(dataset.shared_features_by_choice)
assert True


def test_fit_lbfgs():
"""Test fit with Gradient Descent."""
tastenet = TasteNet(
taste_net_layers=[4],
taste_net_activation=taste_net_activation,
items_features_by_choice_parametrization=items_features_by_choice_parametrization,
optimizer="lbfgs",
epochs=5,
lr=0.001,
batch_size=32,
)
_ = tastenet.fit(dataset)
assert True


def test_errors_raised():
"""Test diverse errors that should be raised."""
with pytest.raises(ValueError):
tastenet = TasteNet(
taste_net_layers=taste_net_layers,
taste_net_activation=taste_net_activation,
items_features_by_choice_parametrization=[
[-1.0, "-exp", "-exp", 0.0, "linear", 0.0, 0.0],
[-1.0, "-exp", "-exp", "linear", 0.0, "linear", 0.0],
],
optimizer="Adam",
epochs=5,
lr=0.001,
batch_size=32,
)
_ = tastenet.fit(dataset)

with pytest.raises(ValueError):
tastenet = TasteNet(
taste_net_layers=taste_net_layers,
taste_net_activation=taste_net_activation,
items_features_by_choice_parametrization=[
[-1.0, "-exp", "-exp", 0.0, "linear", 0.0, 0.0, 0.0],
[-1.0, "-exp", "-exp", "linear", 0.0, "linear", 0.0, 0.0],
[-1.0, "-exp", 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
],
optimizer="Adam",
epochs=5,
lr=0.001,
batch_size=32,
)
_ = tastenet.fit(dataset)

with pytest.raises(ValueError):
tastenet = TasteNet(
taste_net_layers=taste_net_layers,
taste_net_activation=taste_net_activation,
items_features_by_choice_parametrization=[
[-1.0, "-exp", "-exp", 0.0, "linear", 0.0, 0.0],
[-1.0, "-exp", "-exp", "linear", 0.0, "linear", 0.0],
],
optimizer="lbfgs",
epochs=5,
lr=0.001,
batch_size=32,
)
_ = tastenet.fit(dataset)

with pytest.raises(ValueError):
tastenet = TasteNet(
taste_net_layers=taste_net_layers,
taste_net_activation=taste_net_activation,
items_features_by_choice_parametrization=[
[-1.0, "-exp", "-exp", 0.0, "linear", 0.0, 0.0, 0.0],
[-1.0, "-exp", "-exp", "linear", 0.0, "linear", 0.0, 0.0],
[-1.0, "-exp", 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
],
optimizer="lbfgs",
epochs=5,
lr=0.001,
batch_size=32,
)
_ = tastenet.fit(dataset)

with pytest.raises(ValueError):
tastenet = TasteNet(
taste_net_layers=taste_net_layers,
taste_net_activation=taste_net_activation,
items_features_by_choice_parametrization=[
[-1.0, "-exp", "-exp", 0.0, "linear", 0.0, 0.0],
[-1.0, "-exp", "-exp", "linear", 0.0, "linear", 0.0, 0.0],
[-1.0, "-exp", 0.0, 0.0, 0.0, 0.0, 0.0],
],
optimizer="lbfgs",
epochs=5,
lr=0.001,
batch_size=32,
)
_ = tastenet.fit(dataset)
Loading