-
Notifications
You must be signed in to change notification settings - Fork 5
Added replicability check #24
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
Open
cchatzis
wants to merge
3
commits into
tensorly:main
Choose a base branch
from
cchatzis:replicability
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+319
−0
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
|
|
||
| :orphan: | ||
|
|
||
| .. _sphx_glr_sg_execution_times: | ||
|
|
||
|
|
||
| Computation times | ||
| ================= | ||
| **01:57.453** total execution time for 9 files **from all galleries**: | ||
|
|
||
| .. container:: | ||
|
|
||
| .. raw:: html | ||
|
|
||
| <style scoped> | ||
| <link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/5.3.0/css/bootstrap.min.css" rel="stylesheet" /> | ||
| <link href="https://cdn.datatables.net/1.13.6/css/dataTables.bootstrap5.min.css" rel="stylesheet" /> | ||
| </style> | ||
| <script src="https://code.jquery.com/jquery-3.7.0.js"></script> | ||
| <script src="https://cdn.datatables.net/1.13.6/js/jquery.dataTables.min.js"></script> | ||
| <script src="https://cdn.datatables.net/1.13.6/js/dataTables.bootstrap5.min.js"></script> | ||
| <script type="text/javascript" class="init"> | ||
| $(document).ready( function () { | ||
| $('table.sg-datatable').DataTable({order: [[1, 'desc']]}); | ||
| } ); | ||
| </script> | ||
|
|
||
| .. list-table:: | ||
| :header-rows: 1 | ||
| :class: table table-striped sg-datatable | ||
|
|
||
| * - Example | ||
| - Time | ||
| - Mem (MB) | ||
| * - :ref:`sphx_glr_auto_examples_plot_replicability_analysis.py` (``../examples/plot_replicability_analysis.py``) | ||
| - 01:57.453 | ||
| - 0.0 | ||
| * - :ref:`sphx_glr_auto_examples_plot_bike_plotly.py` (``../examples/plot_bike_plotly.py``) | ||
| - 00:00.000 | ||
| - 0.0 | ||
| * - :ref:`sphx_glr_auto_examples_plot_core_consistency.py` (``../examples/plot_core_consistency.py``) | ||
| - 00:00.000 | ||
| - 0.0 | ||
| * - :ref:`sphx_glr_auto_examples_plot_labelled_decompositions.py` (``../examples/plot_labelled_decompositions.py``) | ||
| - 00:00.000 | ||
| - 0.0 | ||
| * - :ref:`sphx_glr_auto_examples_plot_optimisation_diagnostic.py` (``../examples/plot_optimisation_diagnostic.py``) | ||
| - 00:00.000 | ||
| - 0.0 | ||
| * - :ref:`sphx_glr_auto_examples_plot_outlier_detection.py` (``../examples/plot_outlier_detection.py``) | ||
| - 00:00.000 | ||
| - 0.0 | ||
| * - :ref:`sphx_glr_auto_examples_plot_selecting_aminoacids_components.py` (``../examples/plot_selecting_aminoacids_components.py``) | ||
| - 00:00.000 | ||
| - 0.0 | ||
| * - :ref:`sphx_glr_auto_examples_plot_split_half_analysis.py` (``../examples/plot_split_half_analysis.py``) | ||
| - 00:00.000 | ||
| - 0.0 | ||
| * - :ref:`sphx_glr_auto_examples_plot_working_with_xarray.py` (``../examples/plot_working_with_xarray.py``) | ||
| - 00:00.000 | ||
| - 0.0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,215 @@ | ||
| """ | ||
| .. _replicability_analysis: | ||
|
|
||
| Replicability analysis | ||
| ---------------- | ||
|
|
||
| This example desrcibes how replicability of patterns can be used to guide the component selection process for PARAFAC models :cite:p:`reprref1, reprref2, reprref3`. | ||
|
|
||
| This process evaluates the consistency of the uncovered patterns by fitting the model to different subsets of the data. The rationale is that if the appropriate number of components is used, the uncovered patterns should be consistent. This can be seen as an extension of `split-half analysis <https://tensorly.org/viz/stable/auto_examples/plot_split_half_analysis.html>`_ where a higher number of smaller subsets of the input are removed. | ||
| """ | ||
|
|
||
| ############################################################################### | ||
| # Imports and utilities | ||
| # ^^^^^^^^^^^^^^^^^^^^^ | ||
|
|
||
| import matplotlib.pyplot as plt | ||
| import numpy as np | ||
| import tensorly as tl | ||
| from tensorly.decomposition import parafac | ||
|
|
||
| import sklearn | ||
| from sklearn.model_selection import RepeatedKFold | ||
|
|
||
| import tlviz | ||
|
|
||
| rng = np.random.default_rng(1) | ||
|
|
||
| ############################################################################### | ||
| # To fit PARAFAC models, we need to solve a non-convex optimization problem, possibly with local minima. It is | ||
| # therefore useful to fit several models with the same number of components using many different random | ||
| # initialisations. | ||
|
|
||
|
|
||
| def fit_many_parafac(X, num_components, num_inits=5): | ||
| return [ | ||
| parafac( | ||
| X, | ||
| num_components, | ||
| n_iter_max=1000, | ||
| tol=1e-8, | ||
| init="random", | ||
| linesearch=True, | ||
| random_state=i, | ||
| ) | ||
| for i in range(num_inits) | ||
| ] | ||
|
|
||
|
|
||
| ############################################################################### | ||
| # Creating simulated data | ||
| # ^^^^^^^^^^^^^^^^^^^^^^^ | ||
| # | ||
| # We start with some simulated data, since then, we know exactly how many components there are in the data. | ||
|
|
||
| cp_tensor, dataset = tlviz.data.simulated_random_cp_tensor((30, 40, 25), 3, noise_level=0.3, labelled=True) | ||
|
|
||
| ############################################################################### | ||
| # .. figure:: /_static/notebook_figures/replicability.jpg | ||
| # Illustration of the replicability check, taken from :cite:p:`reprref3`. | ||
| # | ||
|
|
||
| ############################################################################### | ||
| # The replicability analysis boils down to the following steps: | ||
cchatzis marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| # | ||
| # 1. Split the data in a (user-chosen) mode into :math:`N` folds (user-chosen). | ||
| # 2. Create :math:`N` subsets by subtracting each fold from the complete dataset. | ||
| # 3. Fit multiple initializations to each subset and choose the *best* run | ||
| # according to lowest loss (total of :math:`N` *best* runs). | ||
| # 4. Compare, in terms of FMS, the best runs across the different subsets | ||
| # to evaluate the replicability of the uncovered patterns (:math:`\binom{N}{2}` comparisons). | ||
| # 5. Repeat the above process :math:`M` times (user-chosen), to find a total of | ||
| # :math:`M \binom{N}{2}` comparisons. | ||
|
|
||
|
|
||
| ############################################################################### | ||
| # Splitting the data | ||
| # ^^^^^^^^^^^^^^^^^^ | ||
| # | ||
|
|
||
| splits = 5 # N | ||
| repeats = 10 # M | ||
|
|
||
| models = {} | ||
| split_indices = {} # Keeps track of which indices are used in each subset | ||
|
|
||
| for rank in [2, 3, 4, 5]: | ||
|
|
||
| print(f"{rank} components") | ||
|
|
||
| rskf = RepeatedKFold(n_splits=splits, n_repeats=repeats, random_state=1) | ||
|
|
||
| models[rank] = {} | ||
| split_indices[rank] = {} | ||
|
|
||
| for split_no, (train_index, _) in enumerate(rskf.split(dataset)): | ||
| repeat_no = split_no // splits | ||
|
|
||
| # Sort rows for consistent ordering (not necessary) | ||
|
|
||
| sorted_train_index = sorted(train_index) | ||
| train = dataset[sorted_train_index] | ||
|
|
||
| train = train / tl.norm(train) # Pre-process the tensor without leaking info from other folds | ||
|
|
||
| current_models = fit_many_parafac(train.data, rank) | ||
| current_model = tlviz.multimodel_evaluation.get_model_with_lowest_error(current_models, train) | ||
|
|
||
| if repeat_no not in models[rank].keys(): | ||
| models[rank][repeat_no] = [] | ||
|
|
||
| models[rank][repeat_no].append(current_model) | ||
|
|
||
| if repeat_no not in split_indices[rank].keys(): | ||
| split_indices[rank][repeat_no] = [] | ||
|
|
||
| split_indices[rank][repeat_no].append(sorted_train_index) | ||
|
|
||
|
|
||
| ############################################################################### | ||
| # Often, the mode one will be splitting within refers to different samples | ||
| # Depending on the use-case, it might be deemed reasonable to retain the | ||
| # distributions of some properties in each subset. For this goal, | ||
| # `RepeatedStratifiedKFold <https://scikit-learn.org/stable/modules/generated/sklearn.model_selection.RepeatedStratifiedKFold.html#sklearn.model_selection.RepeatedStratifiedKFold>`_ | ||
| # can be used. | ||
| # | ||
| # Each subset might require certain pre-processing. It is important to pre-process | ||
| # each subset in isolation to avoid leaking information from the omitted part of the input. | ||
| # For example, in this case we normalize each subset to unit norm independently. | ||
| # Also, notice that ``for train_index, _ in rskf.split(dataset):`` is embarrassingly parallel. | ||
|
|
||
| ############################################################################### | ||
| # Computing and plotting factor similarity | ||
| # ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| # Here, we are skipping the mode we split (``mode=0``). | ||
|
|
||
| replicability_stability = {} | ||
| for rank in [2, 3, 4, 5]: | ||
| replicability_stability[rank] = [] | ||
| for repeat_no in models[rank].keys(): | ||
| for i, cp_i in enumerate(models[rank][repeat_no]): | ||
| for j, cp_j in enumerate(models[rank][repeat_no]): | ||
| if i < j: # include every pair only once and omit i == j | ||
| fms = tlviz.factor_tools.factor_match_score(cp_i, cp_j, consider_weights=False, skip_mode=0) | ||
| replicability_stability[rank].append(fms) | ||
|
|
||
| ranks = sorted(replicability_stability.keys()) | ||
cchatzis marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| data = [np.ravel(replicability_stability[r]) for r in ranks] | ||
|
|
||
| fig, ax = plt.subplots() | ||
| ax.axhline(0.9, linestyle="--", color="gray") | ||
| ax.boxplot(data, positions=ranks) | ||
| ax.set_xlabel("Number of components") | ||
| ax.set_ylabel("Replicability stability") | ||
| plt.show() | ||
|
|
||
| ############################################################################### | ||
| # Here, we can observe that over-estimating the number of components | ||
| # results in not replicable patterns, indicated by low FMS. | ||
|
|
||
| ############################################################################### | ||
| # Computing and plotting factor similarity (alt.) | ||
| # ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| # There is an alternative way to estimate the replicability of the uncovered patterns | ||
| # that includes the mode we are splitting within :cite:p:`reprref4`. When comparing two factorizations in | ||
| # terms of FMS, we can include the previously skipped factor by using only the indices | ||
| # present in both subsets. | ||
|
|
||
| replicability_stability_alt = {} | ||
| for rank in [2, 3, 4, 5]: | ||
| replicability_stability_alt[rank] = [] | ||
| for repeat_no in models[rank].keys(): | ||
| for i, cp_i in enumerate(models[rank][repeat_no]): | ||
| for j, cp_j in enumerate(models[rank][repeat_no]): | ||
| if i < j: # include every pair only once and omit i == j | ||
|
|
||
| weights_i, (A_i, B_i, C_i) = cp_i | ||
| weights_j, (A_j, B_j, C_j) = cp_j | ||
|
|
||
| indices_subset_i = sorted(split_indices[rank][repeat_no][i]) | ||
| indices_subset_j = sorted(split_indices[rank][repeat_no][j]) | ||
|
|
||
| common_indices = sorted(list(set(indices_subset_i).intersection(set(indices_subset_j)))) | ||
|
|
||
| indices2use_i = [] | ||
| indices2use_j = [] | ||
|
|
||
| for common_idx in common_indices: | ||
| indices2use_i.append(indices_subset_i.index(common_idx)) | ||
| indices2use_j.append(indices_subset_j.index(common_idx)) | ||
|
|
||
| A_i = A_i[indices2use_i, :] | ||
| A_j = A_j[indices2use_j, :] | ||
|
|
||
| fms = tlviz.factor_tools.factor_match_score( | ||
| (weights_i, (A_i, B_i, C_i)), (weights_j, (A_j, B_j, C_j)), consider_weights=False | ||
| ) | ||
| replicability_stability_alt[rank].append(fms) | ||
|
|
||
| ranks = sorted(replicability_stability_alt.keys()) | ||
| data = [np.ravel(replicability_stability_alt[r]) for r in ranks] | ||
|
|
||
| fig, ax = plt.subplots() | ||
| ax.axhline(0.9, linestyle="--", color="gray") | ||
| ax.boxplot(data, positions=ranks) | ||
| ax.set_xlabel("Number of components") | ||
| ax.set_ylabel("Replicability stability") | ||
| plt.show() | ||
|
|
||
| ############################################################################### | ||
| # ``common_indices`` contains the indices (e.g. samples) present in both subsets, | ||
| # but since the position of each index can change (e.g. sample no 3 is not guaranteeed at | ||
| # the third position in all subsets as the first and second samples might be omitted) we need to | ||
| # utilize the indices in the original tensor input. | ||
| # | ||
| # Similar results can be also observed here in terms of the replicability of the patterns. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -37,6 +37,7 @@ docs = | |
| tensorly-sphinx-theme | ||
| plotly>=4.12 | ||
| torch | ||
| scikit-learn | ||
|
|
||
| test = | ||
| pytest | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.