Skip to content

Update metrics - #321

Merged
VincentAuriau merged 18 commits into
mainfrom
Update_metrics
May 27, 2026
Merged

Update metrics#321
VincentAuriau merged 18 commits into
mainfrom
Update_metrics

Conversation

@VincentAuriau

Copy link
Copy Markdown
Collaborator

Description of the goal of the PR

Description:

Changes this PR introduces (fill it before implementation)

  • : Change 1
  • : Change 2

Checklist before requesting a review

  • I have commented my code, particularly in hard-to-understand areas
  • I have typed my code
  • I have created / updated the docstrings
  • I have updated the README, if relevant
  • I have updated the requirements files if a new package is used
  • I have tested my code
  • The CI pipeline passes
  • I have performed a self-review of my code

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request refactors several evaluation metrics (including MRR, HitRate, and NegativeLogLikelihood) to support averaging on a per-trip basis instead of per-batch, introduces a new MeanRank metric, and updates the basket dataset to calculate sample counts more efficiently. The review feedback highlights critical TensorFlow type mismatch issues in the MRR and MeanRank metrics that could lead to runtime errors, and suggests a more Pythonic implementation for the new get_n_samples method.

Comment thread choice_learn/utils/metrics.py Outdated
Comment on lines +171 to +173
else:
self.mrr.assign(self.mrr + tf.reduce_sum(mean_rank))
self.n_evals.assign(self.n_evals + tf.cast(tf.shape(y_true)[0], tf.float32))
self.mrr.assign(self.mrr + tf.reduce_sum(tf.cast(1 / item_ranks, dtype=tf.float32)))
self.n_evals.assign(self.n_evals + tf.shape(y_true)[0])

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

Using tf.shape(y_true)[0] directly without casting will raise a TypeError in TensorFlow due to type mismatch between the float32 variable self.n_evals and the int32/int64 tensor returned by tf.shape. Additionally, using assign_add is more efficient and standard for updating metric variables.

Suggested change
else:
self.mrr.assign(self.mrr + tf.reduce_sum(mean_rank))
self.n_evals.assign(self.n_evals + tf.cast(tf.shape(y_true)[0], tf.float32))
self.mrr.assign(self.mrr + tf.reduce_sum(tf.cast(1 / item_ranks, dtype=tf.float32)))
self.n_evals.assign(self.n_evals + tf.shape(y_true)[0])
else:
self.mrr.assign_add(tf.reduce_sum(tf.cast(1 / item_ranks, dtype=tf.float32)))
self.n_evals.assign_add(tf.cast(tf.shape(y_true)[0], self.n_evals.dtype))

Comment thread choice_learn/utils/metrics.py Outdated
Comment on lines +231 to +237
if batch is not None and self.average_on_trip:
int_batch = tf.cast(batch, tf.int32)
self.mr.assign(self.mr + tf.reduce_sum(tf.math.segment_mean(float_rank, int_batch)))
self.n_evals.assign(self.n_evals + tf.reduce_max(batch) + 1)
else:
self.mr.assign(self.mr + tf.reduce_sum(float_rank))
self.n_evals.assign(self.n_evals + tf.cast(tf.shape(y_true)[0], tf.float32))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The current implementation of MeanRank under average_on_trip has several issues:

  1. tf.math.segment_mean requires sorted segment IDs, which is not guaranteed for arbitrary batches.
  2. tf.reduce_max(batch) + 1 assumes batch indices are 0-indexed and contiguous, which is fragile and prone to bugs.
  3. Adding tf.reduce_max(batch) (int32) directly to self.n_evals (float32) will raise a TypeError in TensorFlow.

Using tf.unique and tf.math.unsorted_segment_mean with assign_add is much more robust, consistent with other metrics in this file, and avoids type mismatch errors.

        if batch is not None and self.average_on_trip:
            unique_trips, segment_ids = tf.unique(batch)
            trip_means = tf.math.unsorted_segment_mean(
                float_rank, segment_ids, tf.shape(unique_trips)[0]
            )
            self.mr.assign_add(tf.reduce_sum(trip_means))
            self.n_evals.assign_add(tf.cast(tf.shape(unique_trips)[0], self.n_evals.dtype))
        else:
            self.mr.assign_add(tf.reduce_sum(float_rank))
            self.n_evals.assign_add(tf.cast(tf.shape(y_true)[0], self.n_evals.dtype))

Comment on lines +207 to +213
def get_n_samples(self) -> int:
"""Return the numbers of samples/transations of the TripDataset."""
n_samples = 0
for trip in self.trips:
n_samples += len(trip.purchases)

return n_samples

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The get_n_samples method can be written more pythonically and efficiently using a generator expression with sum(). Also, corrected typos in the docstring ('numbers' -> 'number', 'transations' -> 'transactions').

Suggested change
def get_n_samples(self) -> int:
"""Return the numbers of samples/transations of the TripDataset."""
n_samples = 0
for trip in self.trips:
n_samples += len(trip.purchases)
return n_samples
def get_n_samples(self) -> int:
"""Return the number of samples/transactions of the TripDataset."""
return sum(len(trip.purchases) for trip in self.trips)

@github-actions

github-actions Bot commented May 27, 2026

Copy link
Copy Markdown
Contributor

Coverage

Coverage Report for Python 3.10
FileStmtsMissCoverMissing
choice_learn
   __init__.py20100% 
   tf_ops.py62198%283
choice_learn/basket_models
   __init__.py40100% 
   alea_carta.py156398%111, 136, 326
   base_basket_model.py2482988%115–116, 127, 145, 189, 259, 381, 489, 589–591, 680, 785, 793, 803, 851, 854–864, 925–928, 967–968
   basic_attention_model.py89496%424, 427, 433, 440
   self_attention_model.py133993%71, 73, 75, 450–454, 651
   shopper.py184995%130, 159, 325, 345, 360, 363, 377, 489, 618
choice_learn/basket_models/data
   __init__.py20100% 
   basket_dataset.py1956268%74–77, 223–231, 299–301, 411, 544–580, 608–648, 671, 678–683, 753–764, 812
   preprocessing.py947817%43–45, 128–364
choice_learn/basket_models/datasets
   __init__.py30100% 
   badminton.py81693%62, 194–199, 247
   bakery.py38392%47, 51, 61
choice_learn/basket_models/utils
   __init__.py00100% 
   permutation.py22195%37
choice_learn/data
   __init__.py30100% 
   choice_dataset.py6493395%198, 250, 283, 421, 463–464, 589, 724, 738, 840, 842, 937, 957–961, 1140, 1159–1161, 1179–1181, 1209, 1214, 1223, 1240, 1281, 1293, 1307, 1346, 1361, 1366, 1395, 1408, 1443–1444
   indexer.py2412390%20, 31, 45, 60–67, 202–204, 219–230, 265, 291, 582
   storage.py161696%22, 33, 51, 56, 61, 71
   store.py72720%3–275
choice_learn/datasets
   __init__.py40100% 
   base.py400599%42–43, 153–154, 714
   expedia.py1028319%37–301
   tafeng.py490100% 
choice_learn/datasets/data
   __init__.py00100% 
choice_learn/models
   __init__.py14286%15–16
   base_model.py3353689%145, 187, 289, 297, 303, 312, 352, 356–357, 362, 391, 395–396, 413, 426, 434, 475–476, 485–486, 587, 589, 605, 609, 611, 734–735, 908, 935, 939–953
   baseline_models.py490100% 
   conditional_logit.py2692690%49, 52, 54, 85, 88, 91–95, 98–102, 136, 206, 212–216, 351, 388, 445, 520–526, 651, 685, 822, 826
   halo_mnl.py124298%186, 374
   latent_class_base_model.py2863986%55–61, 273–279, 288, 325–330, 497–500, 605, 624, 665–701, 715, 720, 751–752, 774–775, 869–870, 974
   latent_class_mnl.py62690%257–261, 296
   learning_mnl.py67396%157, 182, 188
   nested_logit.py2911296%55, 77, 160, 269, 351, 484, 530, 600, 679, 848, 900, 904
   reslogit.py132695%285, 360, 369, 374, 382, 432
   rumnet.py236399%748–751, 982
   simple_mnl.py139696%167, 275, 347, 355, 357, 359
   tastenet.py94397%142, 180, 188
choice_learn/toolbox
   __init__.py00100% 
   assortment_optimizer.py27678%28–30, 93–95, 160–162
   gurobi_opt.py2382380%3–675
   or_tools_opt.py2301195%103, 107, 296–305, 315, 319, 607, 611
choice_learn/utils
   metrics.py116794%73, 151–153, 219–221, 287–289
TOTAL570383385% 

Tests Skipped Failures Errors Time
228 0 💤 1 ❌ 0 🔥 6m 40s ⏱️

@github-actions

github-actions Bot commented May 27, 2026

Copy link
Copy Markdown
Contributor

Coverage

Coverage Report for Python 3.12
FileStmtsMissCoverMissing
choice_learn
   __init__.py20100% 
   tf_ops.py62198%283
choice_learn/basket_models
   __init__.py40100% 
   alea_carta.py156398%111, 136, 326
   base_basket_model.py2482988%115–116, 127, 145, 189, 259, 381, 489, 589–591, 680, 785, 793, 803, 851, 854–864, 925–928, 967–968
   basic_attention_model.py89496%424, 427, 433, 440
   self_attention_model.py133993%71, 73, 75, 450–454, 651
   shopper.py184995%130, 159, 325, 345, 360, 363, 377, 489, 618
choice_learn/basket_models/data
   __init__.py20100% 
   basket_dataset.py1956268%74–77, 223–231, 299–301, 411, 544–580, 608–648, 671, 678–683, 753–764, 812
   preprocessing.py947817%43–45, 128–364
choice_learn/basket_models/datasets
   __init__.py30100% 
   badminton.py81693%62, 194–199, 247
   bakery.py38392%47, 53, 61
choice_learn/basket_models/utils
   __init__.py00100% 
   permutation.py22195%37
choice_learn/data
   __init__.py30100% 
   choice_dataset.py6493395%198, 250, 283, 421, 463–464, 589, 724, 738, 840, 842, 937, 957–961, 1140, 1159–1161, 1179–1181, 1209, 1214, 1223, 1240, 1281, 1293, 1307, 1346, 1361, 1366, 1395, 1408, 1443–1444
   indexer.py2412390%20, 31, 45, 60–67, 202–204, 219–230, 265, 291, 582
   storage.py161696%22, 33, 51, 56, 61, 71
   store.py72720%3–275
choice_learn/datasets
   __init__.py40100% 
   base.py400599%42–43, 153–154, 714
   expedia.py1028319%37–301
   tafeng.py490100% 
choice_learn/datasets/data
   __init__.py00100% 
choice_learn/models
   __init__.py14286%15–16
   base_model.py3353590%145, 187, 289, 297, 303, 312, 352, 356–357, 362, 391, 395–396, 413, 426, 434, 475–476, 485–486, 587, 589, 605, 609, 611, 734–735, 935, 939–953
   baseline_models.py490100% 
   conditional_logit.py2692690%49, 52, 54, 85, 88, 91–95, 98–102, 136, 206, 212–216, 351, 388, 445, 520–526, 651, 685, 822, 826
   halo_mnl.py1241885%186, 341, 360, 364–380
   latent_class_base_model.py2863986%55–61, 273–279, 288, 325–330, 497–500, 605, 624, 665–701, 715, 720, 751–752, 774–775, 869–870, 974
   latent_class_mnl.py62690%257–261, 296
   learning_mnl.py67396%157, 182, 188
   nested_logit.py2911296%55, 77, 160, 269, 351, 484, 530, 600, 679, 848, 900, 904
   reslogit.py132695%285, 360, 369, 374, 382, 432
   rumnet.py236399%748–751, 982
   simple_mnl.py139696%167, 275, 347, 355, 357, 359
   tastenet.py94397%142, 180, 188
choice_learn/toolbox
   __init__.py00100% 
   assortment_optimizer.py27678%28–30, 93–95, 160–162
   gurobi_opt.py2382380%3–675
   or_tools_opt.py2301195%103, 107, 296–305, 315, 319, 607, 611
choice_learn/utils
   metrics.py116794%73, 151–153, 219–221, 287–289
TOTAL570384885% 

Tests Skipped Failures Errors Time
228 0 💤 1 ❌ 0 🔥 6m 36s ⏱️

@github-actions

github-actions Bot commented May 27, 2026

Copy link
Copy Markdown
Contributor

Coverage

Coverage Report for Python 3.9
FileStmtsMissCoverMissing
choice_learn
   __init__.py20100% 
   tf_ops.py62198%283
choice_learn/basket_models
   __init__.py40100% 
   alea_carta.py156398%111, 136, 326
   base_basket_model.py2482988%115–116, 127, 145, 189, 259, 381, 489, 589–591, 680, 785, 793, 803, 851, 854–864, 925–928, 967–968
   basic_attention_model.py89496%424, 427, 433, 440
   self_attention_model.py133993%71, 73, 75, 450–454, 651
   shopper.py184995%130, 159, 325, 345, 360, 363, 377, 489, 618
choice_learn/basket_models/data
   __init__.py20100% 
   basket_dataset.py1956268%74–77, 223–231, 299–301, 411, 544–580, 608–648, 671, 678–683, 753–764, 812
   preprocessing.py947817%43–45, 128–364
choice_learn/basket_models/datasets
   __init__.py30100% 
   badminton.py81693%62, 194–199, 247
   bakery.py38392%47, 51, 61
choice_learn/basket_models/utils
   __init__.py00100% 
   permutation.py22195%37
choice_learn/data
   __init__.py30100% 
   choice_dataset.py6493395%198, 250, 283, 421, 463–464, 589, 724, 738, 840, 842, 937, 957–961, 1140, 1159–1161, 1179–1181, 1209, 1214, 1223, 1240, 1281, 1293, 1307, 1346, 1361, 1366, 1395, 1408, 1443–1444
   indexer.py2412390%20, 31, 45, 60–67, 202–204, 219–230, 265, 291, 582
   storage.py161696%22, 33, 51, 56, 61, 71
   store.py72720%3–275
choice_learn/datasets
   __init__.py40100% 
   base.py400599%42–43, 153–154, 714
   expedia.py1028319%37–301
   tafeng.py490100% 
choice_learn/datasets/data
   __init__.py00100% 
choice_learn/models
   __init__.py14286%15–16
   base_model.py3353490%145, 187, 289, 297, 303, 312, 352, 356–357, 362, 391, 395–396, 413, 426, 434, 475–476, 485–486, 587, 589, 605, 609, 734–735, 935, 939–953
   baseline_models.py490100% 
   conditional_logit.py2692690%49, 52, 54, 85, 88, 91–95, 98–102, 136, 206, 212–216, 351, 388, 445, 520–526, 651, 685, 822, 826
   halo_mnl.py124298%186, 374
   latent_class_base_model.py2863986%55–61, 273–279, 288, 325–330, 497–500, 605, 624, 665–701, 715, 720, 751–752, 774–775, 869–870, 974
   latent_class_mnl.py62690%257–261, 296
   learning_mnl.py67396%157, 182, 188
   nested_logit.py2911296%55, 77, 160, 269, 351, 484, 530, 600, 679, 848, 900, 904
   reslogit.py132795%122, 285, 360, 369, 374, 382, 432
   rumnet.py236399%748–751, 982
   simple_mnl.py139696%167, 275, 347, 355, 357, 359
   tastenet.py94397%142, 180, 188
choice_learn/toolbox
   __init__.py00100% 
   assortment_optimizer.py27678%28–30, 93–95, 160–162
   gurobi_opt.py2362360%3–675
   or_tools_opt.py2301195%103, 107, 296–305, 315, 319, 607, 611
choice_learn/utils
   metrics.py1167337%71–105, 115, 149–173, 183, 217–240, 250, 285–309, 319
TOTAL570189684% 

Tests Skipped Failures Errors Time
228 0 💤 0 ❌ 0 🔥 5m 12s ⏱️

@github-actions

github-actions Bot commented May 27, 2026

Copy link
Copy Markdown
Contributor

Coverage

Coverage Report for Python 3.11
FileStmtsMissCoverMissing
choice_learn
   __init__.py20100% 
   tf_ops.py62198%283
choice_learn/basket_models
   __init__.py40100% 
   alea_carta.py156398%111, 136, 326
   base_basket_model.py2482988%115–116, 127, 145, 189, 259, 381, 489, 589–591, 680, 785, 793, 803, 851, 854–864, 925–928, 967–968
   basic_attention_model.py89496%424, 427, 433, 440
   self_attention_model.py133993%71, 73, 75, 450–454, 651
   shopper.py184995%130, 159, 325, 345, 360, 363, 377, 489, 618
choice_learn/basket_models/data
   __init__.py20100% 
   basket_dataset.py1956268%74–77, 223–231, 299–301, 411, 544–580, 608–648, 671, 678–683, 753–764, 812
   preprocessing.py947817%43–45, 128–364
choice_learn/basket_models/datasets
   __init__.py30100% 
   badminton.py81693%62, 194–199, 247
   bakery.py38392%47, 51, 61
choice_learn/basket_models/utils
   __init__.py00100% 
   permutation.py22195%37
choice_learn/data
   __init__.py30100% 
   choice_dataset.py6493395%198, 250, 283, 421, 463–464, 589, 724, 738, 840, 842, 937, 957–961, 1140, 1159–1161, 1179–1181, 1209, 1214, 1223, 1240, 1281, 1293, 1307, 1346, 1361, 1366, 1395, 1408, 1443–1444
   indexer.py2412390%20, 31, 45, 60–67, 202–204, 219–230, 265, 291, 582
   storage.py161696%22, 33, 51, 56, 61, 71
   store.py72720%3–275
choice_learn/datasets
   __init__.py40100% 
   base.py400599%42–43, 153–154, 714
   expedia.py1028319%37–301
   tafeng.py490100% 
choice_learn/datasets/data
   __init__.py00100% 
choice_learn/models
   __init__.py14286%15–16
   base_model.py3353590%145, 187, 289, 297, 303, 312, 352, 356–357, 362, 391, 395–396, 413, 426, 434, 475–476, 485–486, 587, 589, 605, 609, 611, 734–735, 935, 939–953
   baseline_models.py490100% 
   conditional_logit.py2692690%49, 52, 54, 85, 88, 91–95, 98–102, 136, 206, 212–216, 351, 388, 445, 520–526, 651, 685, 822, 826
   halo_mnl.py1241885%186, 341, 360, 364–380
   latent_class_base_model.py2863986%55–61, 273–279, 288, 325–330, 497–500, 605, 624, 665–701, 715, 720, 751–752, 774–775, 869–870, 974
   latent_class_mnl.py62690%257–261, 296
   learning_mnl.py67396%157, 182, 188
   nested_logit.py2911296%55, 77, 160, 269, 351, 484, 530, 600, 679, 848, 900, 904
   reslogit.py132695%285, 360, 369, 374, 382, 432
   rumnet.py236399%748–751, 982
   simple_mnl.py139696%167, 275, 347, 355, 357, 359
   tastenet.py94397%142, 180, 188
choice_learn/toolbox
   __init__.py00100% 
   assortment_optimizer.py27678%28–30, 93–95, 160–162
   gurobi_opt.py2382380%3–675
   or_tools_opt.py2301195%103, 107, 296–305, 315, 319, 607, 611
choice_learn/utils
   metrics.py116794%73, 151–153, 219–221, 287–289
TOTAL570384885% 

Tests Skipped Failures Errors Time
228 0 💤 2 ❌ 0 🔥 5m 29s ⏱️

@VincentAuriau
VincentAuriau merged commit f82b3f9 into main May 27, 2026
8 checks passed
@VincentAuriau
VincentAuriau deleted the Update_metrics branch May 27, 2026 14:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants