|
| 1 | +import sys |
| 2 | + |
| 3 | +import numpy as np |
| 4 | + |
| 5 | +from tsml_eval.experiments import ( |
| 6 | + run_clustering_experiment as tsml_clustering_experiment, |
| 7 | +) |
| 8 | +from tsml_eval.publications.clustering.kasba._model_configuration import ( |
| 9 | + EXPERIMENT_MODELS, |
| 10 | +) |
| 11 | +from tsml_eval.publications.clustering.kasba._utils import ( |
| 12 | + _parse_command_line_bool, |
| 13 | + check_experiment_results_exist, |
| 14 | + load_dataset_from_file, |
| 15 | +) |
| 16 | + |
| 17 | + |
| 18 | +def run_threaded_clustering_experiment( |
| 19 | + dataset: str, |
| 20 | + clusterer_name: str, |
| 21 | + dataset_path: str, |
| 22 | + results_path: str, |
| 23 | + combine_test_train: bool, |
| 24 | + resample_id: int, |
| 25 | +): |
| 26 | + """Run clustering experiment. |
| 27 | +
|
| 28 | + Parameters |
| 29 | + ---------- |
| 30 | + dataset : str |
| 31 | + Dataset name. |
| 32 | + distance : str |
| 33 | + Distance string (assumed correct and final), e.g.: |
| 34 | + "msm", "dtw", "soft_msm", "soft_dtw", |
| 35 | + "soft_divergence_msm", "soft_divergence_dtw". |
| 36 | + clusterer_str : str |
| 37 | + Free-form label used only for naming/logging (not logic). |
| 38 | + dataset_path : str |
| 39 | + Path to the dataset. |
| 40 | + results_path : str |
| 41 | + Path to the results. |
| 42 | + averaging_method : str |
| 43 | + One of: "soft", "kasba", "petitjean_ba", "subgradient_ba". |
| 44 | + combine_test_train : bool, default=False |
| 45 | + Boolean indicating if data should be combined for test and train. |
| 46 | + resample_id : int, default=0 |
| 47 | + Integer indicating the resample id. |
| 48 | + n_jobs : int default=-1 |
| 49 | + Integer indicating the number of jobs to run in parallel. |
| 50 | + """ |
| 51 | + if clusterer_name not in EXPERIMENT_MODELS: |
| 52 | + raise ValueError(f"Unknown clusterer_name '{clusterer_name}'") |
| 53 | + |
| 54 | + # Skip if results already exist |
| 55 | + if check_experiment_results_exist( |
| 56 | + model_name=clusterer_name, |
| 57 | + dataset=dataset, |
| 58 | + combine_test_train=combine_test_train, |
| 59 | + path_to_results=results_path, |
| 60 | + resample_id=resample_id, |
| 61 | + ): |
| 62 | + return ( |
| 63 | + f"[SKIP] {clusterer_name} (resample {resample_id}): " |
| 64 | + f"results already exist." |
| 65 | + ) |
| 66 | + |
| 67 | + X_train, y_train, X_test, y_test = load_dataset_from_file( |
| 68 | + dataset, |
| 69 | + dataset_path, |
| 70 | + normalize=True, |
| 71 | + combine_test_train=combine_test_train, |
| 72 | + resample_id=0, |
| 73 | + ) |
| 74 | + n_clusters = np.unique(y_train).size |
| 75 | + |
| 76 | + factory = EXPERIMENT_MODELS[clusterer_name] |
| 77 | + clusterer = factory( |
| 78 | + n_clusters=n_clusters, |
| 79 | + random_state=resample_id, |
| 80 | + n_jobs=1, |
| 81 | + ) |
| 82 | + |
| 83 | + tsml_clustering_experiment( |
| 84 | + X_train=X_train, |
| 85 | + y_train=y_train, |
| 86 | + clusterer=clusterer, |
| 87 | + results_path=results_path, |
| 88 | + X_test=X_test, |
| 89 | + y_test=y_test, |
| 90 | + n_clusters=n_clusters, |
| 91 | + clusterer_name=clusterer_name, |
| 92 | + dataset_name=dataset, |
| 93 | + resample_id=resample_id, |
| 94 | + data_transforms=None, |
| 95 | + build_test_file=not combine_test_train, |
| 96 | + build_train_file=True, |
| 97 | + benchmark_time=True, |
| 98 | + ) |
| 99 | + print(f"[DONE] {clusterer_name} (resample {resample_id})") |
| 100 | + |
| 101 | + |
| 102 | +# Boolean to toggle if running locally or via command line. |
| 103 | +RUN_LOCALLY = True |
| 104 | + |
| 105 | +if __name__ == "__main__": |
| 106 | + """NOTE: To run with command line arguments, set RUN_LOCALLY to False.""" |
| 107 | + if RUN_LOCALLY: |
| 108 | + print("RUNNING WITH TEST CONFIG") |
| 109 | + |
| 110 | + dataset = "GunPoint" |
| 111 | + clusterer_name = "KASBA" |
| 112 | + combine_test_train = True |
| 113 | + |
| 114 | + dataset_path = ( |
| 115 | + "/Users/chrisholder/Documents/Research/datasets/UCR/Univariate_ts" |
| 116 | + ) |
| 117 | + results_path = "/Users/chrisholder/projects/kasba-experiments/full_results" |
| 118 | + run_threaded_clustering_experiment( |
| 119 | + dataset=dataset, |
| 120 | + clusterer_name=clusterer_name, |
| 121 | + dataset_path=dataset_path, |
| 122 | + results_path=results_path, |
| 123 | + combine_test_train=combine_test_train, |
| 124 | + resample_id=0, |
| 125 | + ) |
| 126 | + |
| 127 | + else: |
| 128 | + if len(sys.argv) != 6: |
| 129 | + print( |
| 130 | + "Usage: python _clustering_experiment_all.py " |
| 131 | + "<dataset> <clusterer_name> <dataset_path> <result_path> " |
| 132 | + "<combine_test_train>" |
| 133 | + ) |
| 134 | + sys.exit(1) |
| 135 | + |
| 136 | + dataset = str(sys.argv[1]) |
| 137 | + clusterer_name = str(sys.argv[2]) |
| 138 | + dataset_path = str(sys.argv[3]) |
| 139 | + results_path = str(sys.argv[4]) |
| 140 | + combine_test_train = _parse_command_line_bool(sys.argv[5]) |
| 141 | + |
| 142 | + run_threaded_clustering_experiment( |
| 143 | + dataset=dataset, |
| 144 | + clusterer_name=clusterer_name, |
| 145 | + dataset_path=dataset_path, |
| 146 | + results_path=results_path, |
| 147 | + combine_test_train=combine_test_train, |
| 148 | + resample_id=1, |
| 149 | + ) |
0 commit comments