|
| 1 | +# Getting started |
| 2 | + |
| 3 | +This page shows how to set up a simple PHOTONAI project using |
| 4 | +`PhotonaiProject`, run analyses, perform permutation tests, and |
| 5 | +statistically compare different analyses. |
| 6 | + |
| 7 | +## Installation |
| 8 | + |
| 9 | +Install the package (and PHOTONAI) into your environment: |
| 10 | + |
| 11 | +```bash |
| 12 | +pip install photonai photonai-projects |
| 13 | +``` |
| 14 | + |
| 15 | +## Basic concepts |
| 16 | +A **PhotonaiProject** manages multiple PHOTONAI analyses in a single |
| 17 | +project folder. Each analysis has its own subfolder containing: |
| 18 | + |
| 19 | +- a hyperpipe constructor script (hyperpipe_constructor.py) |
| 20 | + |
| 21 | +- a metadata file (hyperpipe_meta.json) |
| 22 | + |
| 23 | +- a data/ folder with X.npy and y.npy |
| 24 | + |
| 25 | +- (optionally) a permutations/ folder for permutation tests |
| 26 | + |
| 27 | +The typical workflow is: |
| 28 | + |
| 29 | +1. Create a project with PhotonaiProject. |
| 30 | +2. Add analyses (data + hyperpipe constructor). |
| 31 | +3. Run analyses to train and evaluate the models. |
| 32 | +4. Run permutation tests to obtain null distributions. |
| 33 | +5. Compare analyses statistically. |
| 34 | + |
| 35 | +## Minimal example |
| 36 | +Below is a complete example using the breast cancer dataset from |
| 37 | +scikit-learn. We create three analyses using different feature sets, |
| 38 | +run them, run permutation tests, and then compare them statistically. |
| 39 | + |
| 40 | + |
| 41 | +```python |
| 42 | +from photonai_projects.project import PhotonaiProject |
| 43 | +from sklearn.datasets import load_breast_cancer |
| 44 | + |
| 45 | +# Load example data |
| 46 | +X, y = load_breast_cancer(return_X_y=True) |
| 47 | + |
| 48 | +# Split features into different sets |
| 49 | +X_1 = X[:, :3] |
| 50 | +X_2 = X[:, 3:6] |
| 51 | + |
| 52 | +# Create a project |
| 53 | +project = PhotonaiProject(project_folder="example_project") |
| 54 | + |
| 55 | +# --------------------------------------------------------------------- |
| 56 | +# 1) Register analyses |
| 57 | +# --------------------------------------------------------------------- |
| 58 | +for name, current_X in [ |
| 59 | + ("all_features", X), |
| 60 | + ("first_feature_set", X_1), |
| 61 | + ("second_feature_set", X_2), |
| 62 | +]: |
| 63 | + project.add( |
| 64 | + name=name, |
| 65 | + X=current_X, |
| 66 | + y=y, |
| 67 | + hyperpipe_script="path/to/hyperpipe_constructor.py", |
| 68 | + name_hyperpipe_constructor="create_hyperpipe", |
| 69 | + ) |
| 70 | + |
| 71 | +project.list_analyses() |
| 72 | + |
| 73 | +# --------------------------------------------------------------------- |
| 74 | +# 2) Run analyses |
| 75 | +# --------------------------------------------------------------------- |
| 76 | +for name in ["all_features", "first_feature_set", "second_feature_set"]: |
| 77 | + project.run(name=name) |
| 78 | + |
| 79 | +# --------------------------------------------------------------------- |
| 80 | +# 3) Run permutation tests (local example) |
| 81 | +# --------------------------------------------------------------------- |
| 82 | +# Use a small number of permutations for testing; increase for real studies. |
| 83 | +for name in ["all_features", "first_feature_set", "second_feature_set"]: |
| 84 | + project.run_permutation_test(name=name, n_perms=10, overwrite=True) |
| 85 | + |
| 86 | +# --------------------------------------------------------------------- |
| 87 | +# 4) Statistical comparison of analyses |
| 88 | +# --------------------------------------------------------------------- |
| 89 | +# For the Nadeau–Bengio test you must provide n_train and n_test as used |
| 90 | +# during cross-validation. Here we give a simple example. |
| 91 | +n_samples = X.shape[0] |
| 92 | +n_train = int(0.8 * n_samples) |
| 93 | +n_test = n_samples - n_train |
| 94 | + |
| 95 | +# Compare two analyses (Nadeau–Bengio corrected t-test) |
| 96 | +project.compare_analyses( |
| 97 | + first_analysis="first_feature_set", |
| 98 | + second_analysis="second_feature_set", |
| 99 | + method="nadeau-bengio", |
| 100 | + n_train=n_train, |
| 101 | + n_test=n_test, |
| 102 | +) |
| 103 | + |
| 104 | +# Compare two analyses (permutation-based) |
| 105 | +project.compare_analyses( |
| 106 | + first_analysis="all_features", |
| 107 | + second_analysis="second_feature_set", |
| 108 | + method="permutation", |
| 109 | + n_perms=10, |
| 110 | +) |
| 111 | + |
| 112 | +# Compare all pairs at once (optional) |
| 113 | +multi_results = project.compare_multiple_analyses( |
| 114 | + analyses=["all_features", "first_feature_set", "second_feature_set"], |
| 115 | + method="permutation", |
| 116 | + n_perms=10, |
| 117 | +) |
| 118 | +print(multi_results.head()) |
| 119 | +``` |
| 120 | + |
| 121 | +## Running permutation tests on a SLURM cluster |
| 122 | +For large numbers of permutations, you can distribute them across a |
| 123 | +SLURM array: |
| 124 | + |
| 125 | +```python |
| 126 | +project.prepare_slurm_permutation_test( |
| 127 | + name="second_feature_set", |
| 128 | + n_perms=1000, |
| 129 | + conda_env="my_photonai_env", |
| 130 | + memory_per_cpu=2, |
| 131 | + n_jobs=20, |
| 132 | + run_time="0-02:00:00", |
| 133 | + random_state=1, |
| 134 | +) |
| 135 | +``` |
| 136 | + |
| 137 | +This creates a slurm_job.cmd script in the analysis folder which you |
| 138 | +can submit with: |
| 139 | + |
| 140 | +```bash |
| 141 | +cd example_project/second_feature_set |
| 142 | +sbatch slurm_job.cmd |
| 143 | +``` |
| 144 | + |
| 145 | +Each array job will call the Typer CLI entry point run_perm_job and |
| 146 | +execute a subset of permutation runs. |
| 147 | + |
| 148 | +## Next steps |
| 149 | +See the Usage page for more details on: |
| 150 | + |
| 151 | +- how to design your hyperpipe constructor, |
| 152 | +- how metrics and scorers are handled, |
| 153 | +- how to interpret the comparison reports. |
| 154 | + |
| 155 | +See the API Reference for the full documentation of PhotonaiProject. |
0 commit comments