-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathexperiments.py
1240 lines (1106 loc) · 41.7 KB
/
experiments.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""Functions to perform machine learning/data mining experiments.
Results are saved a standardised format used by tsml.
"""
__author__ = ["TonyBagnall", "MatthewMiddlehurst"]
import os
import time
import warnings
from datetime import datetime
import numpy as np
import pandas as pd
from aeon.classification import BaseClassifier
from aeon.clustering import BaseClusterer
from aeon.forecasting.base import BaseForecaster
from aeon.regression.base import BaseRegressor
from aeon.transformations.collection import TimeSeriesScaler
from sklearn import preprocessing
from sklearn.base import BaseEstimator, is_classifier, is_regressor
from sklearn.metrics import (
accuracy_score,
mean_absolute_percentage_error,
mean_squared_error,
)
from sklearn.model_selection import cross_val_predict
from tsml.base import BaseTimeSeriesEstimator
from tsml.utils.validation import is_clusterer
from tsml_eval.estimators import (
SklearnToTsmlClassifier,
SklearnToTsmlClusterer,
SklearnToTsmlRegressor,
)
from tsml_eval.evaluation.metrics import clustering_accuracy_score
from tsml_eval.utils.experiments import (
estimator_attributes_to_file,
load_experiment_data,
resample_data,
stratified_resample_data,
timing_benchmark,
write_classification_results,
write_clustering_results,
write_forecasting_results,
write_regression_results,
)
from tsml_eval.utils.memory_recorder import record_max_memory
if os.getenv("MEMRECORD_INTERVAL") is not None:
MEMRECORD_INTERVAL = float(os.getenv("MEMRECORD_INTERVAL"))
else:
MEMRECORD_INTERVAL = 5.0
def run_classification_experiment(
X_train,
y_train,
X_test,
y_test,
classifier,
results_path,
row_normalise=False,
classifier_name=None,
dataset_name="N/A",
resample_id=None,
build_test_file=True,
build_train_file=False,
attribute_file_path=None,
att_max_shape=0,
benchmark_time=True,
):
"""Run a classification experiment and save the results to file.
Function to run a basic classification experiment for a
<dataset>/<classifier>/<resample> combination and write the results to csv file(s)
at a given location.
Parameters
----------
X_train : pd.DataFrame or np.array todo
The data to train the classifier.
y_train : np.array
Training data class labels.
X_test : pd.DataFrame or np.array
The data used to test the trained classifier.
y_test : np.array
Testing data class labels.
classifier : BaseClassifier
Classifier to be used in the experiment.
results_path : str
Location of where to write results. Any required directories will be created.
row_normalise : bool, default=False
Whether to normalise the data rows (time series) prior to fitting and
predicting.
classifier_name : str or None, default=None
Name of classifier used in writing results. If None, the name is taken from
the classifier.
dataset_name : str, default="N/A"
Name of dataset.
resample_id : int or None, default=None
Seed for resampling. If set to 0, the default train/test split from file is
used. Also used in output file name.
build_test_file : bool, default=True:
Whether to generate test files or not. If the classifier can generate its own
train probabilities, the classifier will be built but no file will be output.
build_train_file : bool, default=False
Whether to generate train files or not. If true, it performs a 10-fold
cross-validation on the train data and saves. If the classifier can produce its
own estimates, those are used instead.
benchmark_time : bool, default=True
Whether to benchmark the hardware used with a simple function and write the
results. This will typically take ~2 seconds, but is hardware dependent.
"""
if not build_test_file and not build_train_file:
raise ValueError(
"Both test_file and train_file are set to False. "
"At least one must be written."
)
if classifier_name is None:
classifier_name = type(classifier).__name__
if isinstance(classifier, BaseClassifier) or (
isinstance(classifier, BaseTimeSeriesEstimator) and is_classifier(classifier)
):
pass
elif isinstance(classifier, BaseEstimator) and is_classifier(classifier):
classifier = SklearnToTsmlClassifier(
classifier=classifier,
pad_unequal=True,
concatenate_channels=True,
clone_estimator=False,
random_state=classifier.random_state
if hasattr(classifier, "random_state")
else None,
)
else:
raise TypeError("classifier must be a tsml, aeon or sklearn classifier.")
if row_normalise:
scaler = TimeSeriesScaler()
X_train = scaler.fit_transform(X_train)
X_test = scaler.fit_transform(X_test)
le = preprocessing.LabelEncoder()
y_train = le.fit_transform(y_train)
y_test = le.transform(y_test)
encoder_dict = {label: i for i, label in enumerate(le.classes_)}
n_classes = len(np.unique(y_train))
classifier_train_probs = build_train_file and callable(
getattr(classifier, "_get_train_probs", None)
)
fit_time = -1
mem_usage = -1
benchmark = -1
if benchmark_time:
benchmark = timing_benchmark(random_state=resample_id)
first_comment = (
"Generated by run_classification_experiment on "
f"{datetime.now().strftime('%m/%d/%Y, %H:%M:%S')}. "
f"Encoder dictionary: {str(encoder_dict)}"
)
second = str(classifier.get_params()).replace("\n", " ").replace("\r", " ")
if build_test_file or classifier_train_probs:
mem_usage, fit_time = record_max_memory(
classifier.fit,
args=(X_train, y_train),
interval=MEMRECORD_INTERVAL,
return_func_time=True,
)
fit_time += int(round(getattr(classifier, "_fit_time_milli", 0)))
if attribute_file_path is not None:
estimator_attributes_to_file(
classifier, attribute_file_path, max_list_shape=att_max_shape
)
if build_test_file:
start = int(round(time.time() * 1000))
test_probs = classifier.predict_proba(X_test)
test_time = (
int(round(time.time() * 1000))
- start
+ int(round(getattr(classifier, "_predict_time_milli", 0)))
)
test_preds = classifier.classes_[np.argmax(test_probs, axis=1)]
test_acc = accuracy_score(y_test, test_preds)
write_classification_results(
test_preds,
test_probs,
y_test,
classifier_name,
dataset_name,
results_path,
full_path=False,
split="TEST",
resample_id=resample_id,
time_unit="MILLISECONDS",
first_line_comment=first_comment,
parameter_info=second,
accuracy=test_acc,
fit_time=fit_time,
predict_time=test_time,
benchmark_time=benchmark,
memory_usage=mem_usage,
n_classes=n_classes,
)
if build_train_file:
start = int(round(time.time() * 1000))
if classifier_train_probs: # Normally can only do this if test has been built
train_probs = classifier._get_train_probs(X_train, y_train)
else:
cv_size = 10
_, counts = np.unique(y_train, return_counts=True)
min_class = max(2, np.min(counts))
if min_class < cv_size:
cv_size = min_class
train_probs = cross_val_predict(
classifier, X_train, y=y_train, cv=cv_size, method="predict_proba"
)
train_time = int(round(time.time() * 1000)) - start
train_preds = classifier.classes_[np.argmax(train_probs, axis=1)]
train_acc = accuracy_score(y_train, train_preds)
write_classification_results(
train_preds,
train_probs,
y_train,
classifier_name,
dataset_name,
results_path,
full_path=False,
split="TRAIN",
resample_id=resample_id,
time_unit="MILLISECONDS",
first_line_comment=first_comment,
parameter_info=second,
accuracy=train_acc,
fit_time=fit_time,
benchmark_time=benchmark,
n_classes=n_classes,
train_estimate_time=train_time,
fit_and_estimate_time=fit_time + train_time,
)
def load_and_run_classification_experiment(
problem_path,
results_path,
dataset,
classifier,
row_normalise=False,
classifier_name=None,
resample_id=0,
build_train_file=False,
write_attributes=False,
att_max_shape=0,
benchmark_time=True,
overwrite=False,
predefined_resample=False,
):
"""Load a dataset and run a classification experiment.
Function to load a dataset, run a basic classification experiment for a
<dataset>/<classifier>/<resample> combination, and write the results to csv file(s)
at a given location.
Parameters
----------
problem_path : str
Location of problem files, full path.
results_path : str
Location of where to write results. Any required directories will be created.
dataset : str
Name of problem. Files must be <problem_path>/<dataset>/<dataset>+"_TRAIN.ts",
same for "_TEST.ts".
classifier : BaseClassifier
Classifier to be used in the experiment.
row_normalise : bool, default=False
Whether to normalise the data rows (time series) prior to fitting and
predicting.
classifier_name : str or None, default=None
Name of classifier used in writing results. If None, the name is taken from
the classifier.
resample_id : int, default=0
Seed for resampling. If set to 0, the default train/test split from file is
used. Also used in output file name.
build_train_file : bool, default=False
Whether to generate train files or not. If true, it performs a 10-fold
cross-validation on the train data and saves. If the classifier can produce its
own estimates, those are used instead.
benchmark_time : bool, default=True
Whether to benchmark the hardware used with a simple function and write the
results. This will typically take ~2 seconds, but is hardware dependent.
overwrite : bool, default=False
If set to False, this will only build results if there is not a result file
already present. If True, it will overwrite anything already there.
predefined_resample : bool, default=False
Read a predefined resample from file instead of performing a resample. If True
the file format must include the resample_id at the end of the dataset name i.e.
<problem_path>/<dataset>/<dataset>+<resample_id>+"_TRAIN.ts".
"""
if classifier_name is None:
classifier_name = type(classifier).__name__
build_test_file, build_train_file = _check_existing_results(
results_path,
classifier_name,
dataset,
resample_id,
overwrite,
True,
build_train_file,
)
if not build_test_file and not build_train_file:
warnings.warn("All files exist and not overwriting, skipping.", stacklevel=1)
return
X_train, y_train, X_test, y_test, resample = load_experiment_data(
problem_path, dataset, resample_id, predefined_resample
)
if resample:
X_train, y_train, X_test, y_test = stratified_resample_data(
X_train, y_train, X_test, y_test, random_state=resample_id
)
if write_attributes:
attribute_file_path = f"{results_path}/{classifier_name}/Workspace/{dataset}/"
else:
attribute_file_path = None
run_classification_experiment(
X_train,
y_train,
X_test,
y_test,
classifier,
results_path,
row_normalise=row_normalise,
classifier_name=classifier_name,
dataset_name=dataset,
resample_id=resample_id,
build_test_file=build_test_file,
build_train_file=build_train_file,
attribute_file_path=attribute_file_path,
att_max_shape=att_max_shape,
benchmark_time=benchmark_time,
)
def run_regression_experiment(
X_train,
y_train,
X_test,
y_test,
regressor,
results_path,
row_normalise=False,
regressor_name=None,
dataset_name="",
resample_id=None,
build_test_file=True,
build_train_file=False,
attribute_file_path=None,
att_max_shape=0,
benchmark_time=True,
):
"""Run a regression experiment and save the results to file.
Function to run a basic regression experiment for a
<dataset>/<regressor>/<resample> combination and write the results to csv file(s)
at a given location.
Parameters
----------
X_train : pd.DataFrame or np.array
The data to train the regressor.
y_train : np.array
Training data labels.
X_test : pd.DataFrame or np.array
The data used to test the trained regressor.
y_test : np.array
Testing data labels.
regressor : BaseRegressor
Regressor to be used in the experiment.
results_path : str
Location of where to write results. Any required directories will be created.
row_normalise : bool, default=False
Whether to normalise the data rows (time series) prior to fitting and
predicting.
regressor_name : str or None, default=None
Name of regressor used in writing results. If None, the name is taken from
the regressor.
dataset_name : str, default="N/A"
Name of dataset.
resample_id : int or None, default=None
Seed for resampling. If set to 0, the default train/test split from file is
used. Also used in output file name.
build_test_file : bool, default=True:
Whether to generate test files or not. If the regressor can generate its own
train predictions, the classifier will be built but no file will be output.
build_train_file : bool, default=False
Whether to generate train files or not. If true, it performs a 10-fold
cross-validation on the train data and saves. If the regressor can produce its
own estimates, those are used instead.
benchmark_time : bool, default=True
Whether to benchmark the hardware used with a simple function and write the
results. This will typically take ~2 seconds, but is hardware dependent.
"""
if not build_test_file and not build_train_file:
raise ValueError(
"Both test_file and train_file are set to False. "
"At least one must be written."
)
if regressor_name is None:
regressor_name = type(regressor).__name__
if isinstance(regressor, BaseRegressor) or (
isinstance(regressor, BaseTimeSeriesEstimator) and is_regressor(regressor)
):
pass
elif isinstance(regressor, BaseEstimator) and is_regressor(regressor):
regressor = SklearnToTsmlRegressor(
regressor=regressor,
pad_unequal=True,
concatenate_channels=True,
clone_estimator=False,
random_state=regressor.random_state
if hasattr(regressor, "random_state")
else None,
)
else:
raise TypeError("regressor must be a tsml, aeon or sklearn regressor.")
if row_normalise:
scaler = TimeSeriesScaler()
X_train = scaler.fit_transform(X_train)
X_test = scaler.fit_transform(X_test)
regressor_train_preds = build_train_file and callable(
getattr(regressor, "_get_train_preds", None)
)
fit_time = -1
mem_usage = -1
benchmark = -1
if benchmark_time:
benchmark = timing_benchmark(random_state=resample_id)
first_comment = (
"Generated by run_regression_experiment on "
f"{datetime.now().strftime('%m/%d/%Y, %H:%M:%S')}"
)
second = str(regressor.get_params()).replace("\n", " ").replace("\r", " ")
if build_test_file or regressor_train_preds:
mem_usage, fit_time = record_max_memory(
regressor.fit,
args=(X_train, y_train),
interval=MEMRECORD_INTERVAL,
return_func_time=True,
)
fit_time += int(round(getattr(regressor, "_fit_time_milli", 0)))
if attribute_file_path is not None:
estimator_attributes_to_file(regressor, attribute_file_path)
if build_test_file:
start = int(round(time.time() * 1000))
test_preds = regressor.predict(X_test)
test_time = (int(round(time.time() * 1000)) - start) + int(
round(getattr(regressor, "_predict_time_milli", 0))
)
test_mse = mean_squared_error(y_test, test_preds)
write_regression_results(
test_preds,
y_test,
regressor_name,
dataset_name,
results_path,
full_path=False,
split="TEST",
resample_id=resample_id,
time_unit="MILLISECONDS",
first_line_comment=first_comment,
parameter_info=second,
mse=test_mse,
fit_time=fit_time,
predict_time=test_time,
benchmark_time=benchmark,
memory_usage=mem_usage,
)
if build_train_file:
start = int(round(time.time() * 1000))
if regressor_train_preds: # Normally can only do this if test has been built
train_preds = regressor._get_train_preds(X_train, y_train)
else:
cv_size = min(10, len(y_train))
train_preds = cross_val_predict(regressor, X_train, y=y_train, cv=cv_size)
train_time = int(round(time.time() * 1000)) - start
train_mse = mean_squared_error(y_train, train_preds)
write_regression_results(
train_preds,
y_train,
regressor_name,
dataset_name,
results_path,
full_path=False,
split="TRAIN",
resample_id=resample_id,
time_unit="MILLISECONDS",
first_line_comment=first_comment,
parameter_info=second,
mse=train_mse,
fit_time=fit_time,
benchmark_time=benchmark,
train_estimate_time=train_time,
fit_and_estimate_time=fit_time + train_time,
)
def load_and_run_regression_experiment(
problem_path,
results_path,
dataset,
regressor,
row_normalise=False,
regressor_name=None,
resample_id=0,
build_train_file=False,
write_attributes=False,
att_max_shape=0,
benchmark_time=True,
overwrite=False,
predefined_resample=False,
):
"""Load a dataset and run a regression experiment.
Function to load a dataset, run a basic regression experiment for a
<dataset>/<regressor>/<resample> combination, and write the results to csv file(s)
at a given location.
Parameters
----------
problem_path : str
Location of problem files, full path.
results_path : str
Location of where to write results. Any required directories will be created.
dataset : str
Name of problem. Files must be <problem_path>/<dataset>/<dataset>+"_TRAIN.ts",
same for "_TEST.ts".
regressor : BaseRegressor
Regressor to be used in the experiment.
row_normalise : bool, default=False
Whether to normalise the data rows (time series) prior to fitting and
predicting.
regressor_name : str or None, default=None
Name of regressor used in writing results. If None, the name is taken from
the regressor.
resample_id : int, default=0
Seed for resampling. If set to 0, the default train/test split from file is
used. Also used in output file name.
build_train_file : bool, default=False
Whether to generate train files or not. If true, it performs a 10-fold
cross-validation on the train data and saves. If the regressor can produce its
own estimates, those are used instead.
benchmark_time : bool, default=True
Whether to benchmark the hardware used with a simple function and write the
results. This will typically take ~2 seconds, but is hardware dependent.
overwrite : bool, default=False
If set to False, this will only build results if there is not a result file
already present. If True, it will overwrite anything already there.
predefined_resample : bool, default=False
Read a predefined resample from file instead of performing a resample. If True
the file format must include the resample_id at the end of the dataset name i.e.
<problem_path>/<dataset>/<dataset>+<resample_id>+"_TRAIN.ts".
"""
if regressor_name is None:
regressor_name = type(regressor).__name__
build_test_file, build_train_file = _check_existing_results(
results_path,
regressor_name,
dataset,
resample_id,
overwrite,
True,
build_train_file,
)
if not build_test_file and not build_train_file:
warnings.warn("All files exist and not overwriting, skipping.", stacklevel=1)
return
X_train, y_train, X_test, y_test, resample = load_experiment_data(
problem_path, dataset, resample_id, predefined_resample
)
if resample:
X_train, y_train, X_test, y_test = resample_data(
X_train, y_train, X_test, y_test, random_state=resample_id
)
if write_attributes:
attribute_file_path = f"{results_path}/{regressor_name}/Workspace/{dataset}/"
else:
attribute_file_path = None
# Ensure labels are floats
y_train = y_train.astype(float)
y_test = y_test.astype(float)
run_regression_experiment(
X_train,
y_train,
X_test,
y_test,
regressor,
results_path,
row_normalise=row_normalise,
regressor_name=regressor_name,
dataset_name=dataset,
resample_id=resample_id,
build_test_file=build_test_file,
build_train_file=build_train_file,
attribute_file_path=attribute_file_path,
att_max_shape=att_max_shape,
benchmark_time=benchmark_time,
)
def run_clustering_experiment(
X_train,
y_train,
clusterer,
results_path,
X_test=None,
y_test=None,
row_normalise=False,
n_clusters=None,
clusterer_name=None,
dataset_name="N/A",
resample_id=None,
build_test_file=False,
build_train_file=True,
attribute_file_path=None,
att_max_shape=0,
benchmark_time=True,
):
"""Run a clustering experiment and save the results to file.
Function to run a basic clustering experiment for a
<dataset>/<clusterer>/<resample> combination and write the results to csv file(s)
at a given location.
Parameters
----------
X_train : pd.DataFrame or np.array
The data to train the clusterer.
y_train : np.array
Training data class labels (used for evaluation).
clusterer : BaseClusterer
Clusterer to be used in the experiment.
results_path : str
Location of where to write results. Any required directories will be created.
X_test : pd.DataFrame or np.array, default=None
The data used to test the fitted clusterer.
y_test : np.array, default=None
Testing data class labels.
row_normalise : bool, default=False
Whether to normalise the data rows (time series) prior to fitting and
predicting.
n_clusters : int or None, default=None
Number of clusters to use if the clusterer has an `n_clusters` parameter.
If None, the clusterers default is used. If -1, the number of classes in the
dataset is used.
This may not work as intended for pipelines currently.
clusterer_name : str or None, default=None
Name of clusterer used in writing results. If None, the name is taken from
the clusterer.
dataset_name : str, default="N/A"
Name of dataset.
resample_id : int or None, default=None
Seed for resampling. If set to 0, the default train/test split from file is
used. Also used in output file name.
build_test_file : bool, default=False:
Whether to generate test files or not. If True, X_test and y_test must be
provided.
build_train_file : bool, default=True
Whether to generate train files or not. The clusterer is fit using train data
regardless of input.
benchmark_time : bool, default=True
Whether to benchmark the hardware used with a simple function and write the
results. This will typically take ~2 seconds, but is hardware dependent.
"""
if not build_test_file and not build_train_file:
raise ValueError(
"Both test_file and train_file are set to False. "
"At least one must be written."
)
if clusterer_name is None:
clusterer_name = type(clusterer).__name__
if isinstance(clusterer, BaseClusterer) or (
isinstance(clusterer, BaseTimeSeriesEstimator) and is_clusterer(clusterer)
):
pass
elif isinstance(clusterer, BaseEstimator) and is_clusterer(clusterer):
clusterer = SklearnToTsmlClusterer(
clusterer=clusterer,
pad_unequal=True,
concatenate_channels=True,
clone_estimator=False,
random_state=clusterer.random_state
if hasattr(clusterer, "random_state")
else None,
)
else:
raise TypeError("clusterer must be a tsml, aeon or sklearn clusterer.")
if build_test_file and (X_test is None or y_test is None):
raise ValueError("Test data and labels not provided, cannot build test file.")
if row_normalise:
scaler = TimeSeriesScaler()
X_train = scaler.fit_transform(X_train)
if build_test_file:
X_test = scaler.fit_transform(X_test)
le = preprocessing.LabelEncoder()
y_train = le.fit_transform(y_train)
if build_test_file:
y_test = le.transform(y_test)
encoder_dict = {label: i for i, label in enumerate(le.classes_)}
n_classes = len(np.unique(y_train))
benchmark = -1
if benchmark_time:
benchmark = timing_benchmark(random_state=resample_id)
first_comment = (
"Generated by run_clustering_experiment on "
f"{datetime.now().strftime('%m/%d/%Y, %H:%M:%S')}. "
f"Encoder dictionary: {str(encoder_dict)}"
)
second = str(clusterer.get_params()).replace("\n", " ").replace("\r", " ")
if isinstance(n_clusters, int):
try:
if n_clusters == -1:
n_clusters = n_classes
if isinstance(clusterer, SklearnToTsmlClusterer):
clusterer.set_params(clusterer__n_clusters=n_clusters)
else:
clusterer.set_params(n_clusters=n_clusters)
except ValueError:
warnings.warn(
f"{clusterer_name} does not have a n_clusters parameter, "
"so it cannot be set.",
stacklevel=1,
)
n_clusters = None
elif n_clusters is not None:
raise ValueError("n_clusters must be an int or None.")
mem_usage, fit_time = record_max_memory(
clusterer.fit,
args=(X_train,),
interval=MEMRECORD_INTERVAL,
return_func_time=True,
)
fit_time += int(round(getattr(clusterer, "_fit_time_milli", 0)))
if attribute_file_path is not None:
estimator_attributes_to_file(clusterer, attribute_file_path)
start = int(round(time.time() * 1000))
if callable(getattr(clusterer, "predict_proba", None)):
train_probs = clusterer.predict_proba(X_train)
train_preds = np.argmax(train_probs, axis=1)
else:
train_preds = (
clusterer.labels_
if hasattr(clusterer, "labels_")
else clusterer.predict(X_train)
)
train_probs = np.zeros(
(
len(train_preds),
n_clusters if n_clusters is not None else len(np.unique(train_preds)),
)
)
train_probs[np.arange(len(train_preds)), train_preds] = 1
train_time = int(round(time.time() * 1000)) - start
if build_train_file:
train_acc = clustering_accuracy_score(y_train, train_preds)
write_clustering_results(
train_preds,
train_probs,
y_train,
clusterer_name,
dataset_name,
results_path,
full_path=False,
split="TRAIN",
resample_id=resample_id,
time_unit="MILLISECONDS",
first_line_comment=first_comment,
parameter_info=second,
clustering_accuracy=train_acc,
fit_time=fit_time,
predict_time=train_time,
benchmark_time=benchmark,
memory_usage=mem_usage,
n_classes=n_classes,
n_clusters=len(train_probs[0]),
)
if build_test_file:
start = int(round(time.time() * 1000))
if callable(getattr(clusterer, "predict_proba", None)):
test_probs = clusterer.predict_proba(X_test)
test_preds = np.argmax(test_probs, axis=1)
else:
test_preds = clusterer.predict(X_test)
test_probs = np.zeros(
(
len(test_preds),
n_clusters
if n_clusters is not None
else len(np.unique(train_preds)),
)
)
test_probs[np.arange(len(test_preds)), test_preds] = 1
test_time = (
int(round(time.time() * 1000))
- start
+ int(round(getattr(clusterer, "_predict_time_milli", 0)))
)
test_acc = clustering_accuracy_score(y_test, test_preds)
write_clustering_results(
test_preds,
test_probs,
y_test,
clusterer_name,
dataset_name,
results_path,
full_path=False,
split="TEST",
resample_id=resample_id,
time_unit="MILLISECONDS",
first_line_comment=first_comment,
parameter_info=second,
clustering_accuracy=test_acc,
fit_time=fit_time,
predict_time=test_time,
benchmark_time=benchmark,
memory_usage=mem_usage,
n_classes=n_classes,
n_clusters=len(test_probs[0]),
)
def load_and_run_clustering_experiment(
problem_path,
results_path,
dataset,
clusterer,
row_normalise=False,
n_clusters=None,
clusterer_name=None,
resample_id=0,
build_test_file=False,
write_attributes=False,
att_max_shape=0,
benchmark_time=True,
overwrite=False,
predefined_resample=False,
combine_train_test_split=False,
):
"""Load a dataset and run a clustering experiment.
Function to load a dataset, run a basic clustering experiment for a
<dataset>/<clusterer>/<resample> combination, and write the results to csv file(s)
at a given location.
Parameters
----------
problem_path : str
Location of problem files, full path.
results_path : str
Location of where to write results. Any required directories will be created.
dataset : str
Name of problem. Files must be <problem_path>/<dataset>/<dataset>+"_TRAIN.ts",
same for "_TEST.ts".
clusterer : BaseClusterer
Clusterer to be used in the experiment.
row_normalise : bool, default=False
Whether to normalise the data rows (time series) prior to fitting and
predicting.
n_clusters : int or None, default=None
Number of clusters to use if the clusterer has an `n_clusters` parameter.
If None, the clusterers default is used. If -1, the number of classes in the
dataset is used.
clusterer_name : str or None, default=None
Name of clusterer used in writing results. If None, the name is taken from
the clusterer.
resample_id : int, default=0
Seed for resampling. If set to 0, the default train/test split from file is
used. Also used in output file name.
build_test_file : bool, default=False
Whether to generate test files or not. If true, the clusterer will assign
clusters to the loaded test data.
benchmark_time : bool, default=True
Whether to benchmark the hardware used with a simple function and write the
results. This will typically take ~2 seconds, but is hardware dependent.
overwrite : bool, default=False
If set to False, this will only build results if there is not a result file
already present. If True, it will overwrite anything already there.
predefined_resample : bool, default=False
Read a predefined resample from file instead of performing a resample. If True
the file format must include the resample_id at the end of the dataset name i.e.
<problem_path>/<dataset>/<dataset>+<resample_id>+"_TRAIN.ts".
combine_train_test_split: bool, default=False
Whether the train/test split should be combined. If True then
the train/test split is combined into a single train set. If False then the
train/test split is used as normal.
"""
if clusterer_name is None:
clusterer_name = type(clusterer).__name__
if combine_train_test_split:
build_test_file = False
build_test_file, build_train_file = _check_existing_results(
results_path,
clusterer_name,
dataset,
resample_id,
overwrite,
build_test_file,
True,
)
if not build_test_file and not build_train_file:
warnings.warn("All files exist and not overwriting, skipping.", stacklevel=1)
return
X_train, y_train, X_test, y_test, resample = load_experiment_data(
problem_path, dataset, resample_id, predefined_resample
)
if resample:
X_train, y_train, X_test, y_test = stratified_resample_data(
X_train, y_train, X_test, y_test, random_state=resample_id
)
if write_attributes:
attribute_file_path = f"{results_path}/{clusterer_name}/Workspace/{dataset}/"
else:
attribute_file_path = None
if combine_train_test_split:
y_train = np.concatenate((y_train, y_test), axis=None)
X_train = (
np.concatenate([X_train, X_test], axis=0)
if isinstance(X_train, np.ndarray)
else X_train + X_test
)