# <NOTEBOOK_PATH> <OUTPUT_PATH>
PAPERMILL := papermill --cwd ./notebooks
# NBX := jupyter nbconvert --ExecutePreprocessor.timeout=-1 --execute --to notebook --inplace

.PHONY: clean-datasets clean-models clean-all mkdir-results models test all
.PHONY: mlp-clustering mlp-lesion mlp-double-lesion make mlp-plots

clean-datasets:
	rm -rf datasets

clean-models:
	rm -rf training_runs_dir models

clean-all: clean-datasets clean-models

mkdir-results:
	mkdir -p results


# Running `pytest src` causes to the weird `sacred` and `tensorflow` import error:
# ImportError: Error while finding loader for 'tensorflow' (<class 'ValueError'>: tensorflow.__spec__ is None)
# https://github.com/IDSIA/sacred/issues/493
test:
	pytest src/tests/test_lesion.py
	pytest src/tests/test_utils.py
	pytest src/tests/test_cnn.py
	pytest src/tests/test_spectral_clustering.py

all: datasets models test mlp-analysis

mlp-clustering: mkdir-results
	$(PAPERMILL) ./notebooks/mlp-clustering.ipynb ./notebooks/mlp-clustering.ipynb

mlp-clustering-stability: mkdir-results
	$(PAPERMILL) ./notebooks/mlp-clustering-stability.ipynb ./notebooks/mlp-clustering-stability.ipynb

mlp-clustering-stability-n-clusters: mkdir-results
	$(PAPERMILL) ./notebooks/mlp-clustering-stability-different-n_clusters.ipynb ./notebooks/mlp-clustering-stability-different-n_clusters-K=2.ipynb -p N_CLUSTERS 2
	$(PAPERMILL) ./notebooks/mlp-clustering-stability-different-n_clusters.ipynb ./notebooks/mlp-clustering-stability-different-n_clusters-K=7.ipynb -p N_CLUSTERS 7
	$(PAPERMILL) ./notebooks/mlp-clustering-stability-different-n_clusters.ipynb ./notebooks/mlp-clustering-stability-different-n_clusters-K=10.ipynb -p N_CLUSTERS 10
    
mlp-learning-curve: mkdir-results
	$(PAPERMILL) ./notebooks/mlp-learning-curve.ipynb ./notebooks/mlp-learning-curve.ipynb

# Using 10 clusters
mlp-lesion-TEN: mkdir-results
	$(PAPERMILL) ./notebooks/mlp-lesion-test-TEN.ipynb ./notebooks/mlp-lesion-test-TEN.ipynb

mlp-double-lesion: mkdir-results
	$(PAPERMILL) ./notebooks/mlp-double-lesion-test.ipynb ./notebooks/mlp-double-lesion-test-FASHION+DROPOUT.ipynb -p MODEL_TAG FASHION+DROPOUT

mlp-plots:
	$(PAPERMILL) ./notebooks/mlp-plots.ipynb ./notebooks/mlp-plots.ipynb

mlp-analysis: mlp-clustering mlp-clustering-stability mlp-clustering-stability-n-clusters mlp-learning-curve mlp-lesion mlp-double-lesion mlp-plots

cnn-clustering: mkdir-results
	$(PAPERMILL) ./notebooks/cnn-clustering.ipynb ./notebooks/cnn-clustering.ipynb

lucid-make-dataset-mlp: mkdir-results
	$(PAPERMILL) ./notebooks/lucid_make_dataset_mlp.ipynb ./notebooks/lucid_make_dataset_mlp.ipynb

lucid-make-dataset-cnn: mkdir-results
	$(PAPERMILL) ./notebooks/lucid_make_dataset_cnn.ipynb ./notebooks/lucid_make_dataset_cnn.ipynb

lucid-make-dataset-cnn-vgg: mkdir-results
	$(PAPERMILL) ./notebooks/lucid_make_dataset_cnn_vgg.ipynb ./notebooks/lucid_make_dataset_cnn_vgg.ipynb

lucid-make-dataset-imagenet: mkdir-results
	$(PAPERMILL) ./notebooks/lucid_make_dataset_imagenet.ipynb ./notebooks/lucid_make_dataset_imagenet.ipynb

lucid-results-all: mkdir-results
	$(PAPERMILL) ./notebooks/lucid_results_all.ipynb ./notebooks/lucid_results_all.ipynb

lesion-results-mlp: mkdir-results
	$(PAPERMILL) ./notebooks/lesion_results_mlp.ipynb ./notebooks/lesion_results_mlp.ipynb

lesion-results-cnn: mkdir-results
	$(PAPERMILL) ./notebooks/lesion_results_cnn.ipynb ./notebooks/lesion_results_cnn.ipynb

lesion-results-cnn-vgg: mkdir-results
	$(PAPERMILL) ./notebooks/lesion_results_cnn_vgg.ipynb ./notebooks/lesion_results_cnn_vgg.ipynb

lesion-results-imagenet: mkdir-results
	$(PAPERMILL) ./notebooks/lesion_results_imagenet.ipynb ./notebooks/lesion_results_imagenet.ipynb

lucid-prep-imagenet: mkdir-results
	$(PAPERMILL) ./notebooks/lucid_prep_imagenet.ipynb ./notebooks/lucid_prep_imagenet.ipynb

clustering-factors-mlp: mkdir-results
	$(PAPERMILL) ./notebooks/clustering_factors_mlp.ipynb ./notebooks/clustering_factors_mlp.ipynb

clustering-factors-clust-grad: mkdir-results
	$(PAPERMILL) ./notebooks/clustering_factors_clust_grad.ipynb ./notebooks/clustering_factors_clust_grad.ipynb

clustering-factors-cnn: mkdir-results
	$(PAPERMILL) ./notebooks/clustering_factors_cnn.ipynb ./notebooks/clustering_factors_cnn.ipynb

clustering-factors-cnn-vgg: mkdir-results
	$(PAPERMILL) ./notebooks/clustering_factors_cnn_vgg.ipynb ./notebooks/clustering_factors_cnn_vgg.ipynb

clustering-factors-imagenet: mkdir-results
	$(PAPERMILL) ./notebooks/clustering_factors_imagenet.ipynb ./notebooks/clustering_factors_imagenet.ipynb

plotting-main: mkdir-results
	$(PAPERMILL) ./notebooks/plotting_main.ipynb ./notebooks/plotting_main.ipynb

random-init-ncuts: mkdir-results
	$(PAPERMILL) ./notebooks/random_init_ncuts.ipynb ./notebooks/random_init_ncuts.ipynb