From 79e11f301fe0334ba2b23605b965a018e0eaebe4 Mon Sep 17 00:00:00 2001
From: Tiago Freitas Pereira <tiagofrepereira@gmail.com>
Date: Tue, 22 Dec 2020 15:27:21 +0100
Subject: [PATCH] Fixing the temp directory path for legacy algorithms IF
 checkpointing is off

---
 bob/bio/face/config/baseline/gabor_graph.py | 11 ++----
 bob/bio/face/config/baseline/lda.py         | 21 ++++--------
 bob/bio/face/config/baseline/lgbphs.py      | 37 ++++++++++++---------
 3 files changed, 29 insertions(+), 40 deletions(-)

diff --git a/bob/bio/face/config/baseline/gabor_graph.py b/bob/bio/face/config/baseline/gabor_graph.py
index 847ae7a9..1f1a7860 100644
--- a/bob/bio/face/config/baseline/gabor_graph.py
+++ b/bob/bio/face/config/baseline/gabor_graph.py
@@ -70,16 +70,9 @@ def get_pipeline(face_cropper, transform_extra_arguments):
     )
 
     # Set default temporary directory
-    default_temp = (
-        os.path.join("/idiap", "temp", os.environ["USER"])
-        if "USER" in os.environ
-        else "~/temp"
+    tempdir = bob.bio.base.pipelines.vanilla_biometrics.legacy.get_temp_directory(
+        "gabor_graph"
     )
-    if os.path.exists(default_temp):
-        tempdir = os.path.join(default_temp, "bob_bio_base_tmp")
-    else:
-        # if /idiap/temp/<USER> does not exist, use /tmp/tmpxxxxxxxx
-        tempdir = tempfile.TemporaryDirectory().name
 
     algorithm = BioAlgorithmLegacy(gabor_jet, base_dir=tempdir)
     return VanillaBiometricsPipeline(transformer, algorithm)
diff --git a/bob/bio/face/config/baseline/lda.py b/bob/bio/face/config/baseline/lda.py
index 4c45fcc3..1343f51a 100644
--- a/bob/bio/face/config/baseline/lda.py
+++ b/bob/bio/face/config/baseline/lda.py
@@ -38,18 +38,10 @@ def load(annotation_type, fixed_positions=None):
         face_cropper=face_cropper, dtype=np.float64
     )
 
-
     #### FEATURE EXTRACTOR ######
 
     # Set default temporary directory
-    user_env_var = os.getenv("USER", None)
-    if user_env_var:
-        default_temp = os.path.join("/idiap","temp",user_env_var)
-    if user_env_var and os.path.exists(default_temp):
-        tempdir = os.path.join(default_temp, "bob_bio_base_tmp", "lda")
-    else:
-        # if /idiap/temp/<USER> does not exist, use /tmp/tmpxxxxxxxx
-        tempdir = tempfile.TemporaryDirectory().name
+    tempdir = bob.bio.base.pipelines.vanilla_biometrics.legacy.get_temp_directory("lda")
 
     lda = bob.bio.base.algorithm.LDA(use_pinv=True, pca_subspace_dimension=0.90)
 
@@ -57,26 +49,25 @@ def load(annotation_type, fixed_positions=None):
         lda, projector_file=os.path.join(tempdir, "Projector.hdf5")
     )
 
-
     transformer = make_pipeline(
         wrap(
-            ["sample"], preprocessor, transform_extra_arguments=transform_extra_arguments,
+            ["sample"],
+            preprocessor,
+            transform_extra_arguments=transform_extra_arguments,
         ),
         SampleLinearize(),
         wrap(["sample"], lda_transformer),
     )
 
-
     ### BIOMETRIC ALGORITHM
 
     algorithm = BioAlgorithmLegacy(
-        lda,
-        base_dir=tempdir,
-        projector_file=os.path.join(tempdir, "Projector.hdf5"),
+        lda, base_dir=tempdir, projector_file=os.path.join(tempdir, "Projector.hdf5"),
     )
 
     return VanillaBiometricsPipeline(transformer, algorithm)
 
+
 pipeline = load(annotation_type, fixed_positions)
 
 transformer = pipeline.transformer
diff --git a/bob/bio/face/config/baseline/lgbphs.py b/bob/bio/face/config/baseline/lgbphs.py
index 8b2ee181..eebcbba5 100644
--- a/bob/bio/face/config/baseline/lgbphs.py
+++ b/bob/bio/face/config/baseline/lgbphs.py
@@ -9,7 +9,6 @@ import numpy as np
 import bob.bio.face
 from sklearn.pipeline import make_pipeline
 from bob.pipelines import wrap
-import tempfile
 import bob.math
 
 
@@ -21,6 +20,7 @@ else:
     annotation_type = None
     fixed_positions = None
 
+
 def get_cropper(annotation_type, fixed_positions=None):
     # Cropping
     face_cropper, transform_extra_arguments = crop_80x64(
@@ -28,6 +28,7 @@ def get_cropper(annotation_type, fixed_positions=None):
     )
     return face_cropper, transform_extra_arguments
 
+
 def get_pipeline(face_cropper, transform_extra_arguments):
     preprocessor = bob.bio.face.preprocessor.TanTriggs(
         face_cropper=face_cropper, dtype=np.float64
@@ -37,40 +38,44 @@ def get_pipeline(face_cropper, transform_extra_arguments):
 
     lgbphs = bob.bio.face.extractor.LGBPHS(
         # block setup
-        block_size = 8,
-        block_overlap = 0,
+        block_size=8,
+        block_overlap=0,
         # Gabor parameters
-        gabor_sigma = math.sqrt(2.) * math.pi,
+        gabor_sigma=math.sqrt(2.0) * math.pi,
         # LBP setup (we use the defaults)
-
         # histogram setup
-        sparse_histogram = True
+        sparse_histogram=True,
     )
 
     transformer = make_pipeline(
         wrap(
-            ["sample"], preprocessor, transform_extra_arguments=transform_extra_arguments,
+            ["sample"],
+            preprocessor,
+            transform_extra_arguments=transform_extra_arguments,
         ),
         wrap(["sample"], lgbphs),
     )
 
-
     ### BIOMETRIC ALGORITHM
     histogram = bob.bio.face.algorithm.Histogram(
-        distance_function = bob.math.histogram_intersection,
-        is_distance_function = False
+        distance_function=bob.math.histogram_intersection, is_distance_function=False
     )
 
-
-    tempdir = tempfile.TemporaryDirectory()
-    algorithm = BioAlgorithmLegacy(histogram, base_dir=tempdir.name)
+    tempdir = bob.bio.base.pipelines.vanilla_biometrics.legacy.get_temp_directory(
+        "LGBPHS"
+    )
+    algorithm = BioAlgorithmLegacy(histogram, base_dir=tempdir)
 
     return VanillaBiometricsPipeline(transformer, algorithm)
 
+
 def load(annotation_type, fixed_positions=None):
     ####### SOLVING THE FACE CROPPER TO BE USED ##########
-    face_cropper, transform_extra_arguments = get_cropper(annotation_type, fixed_positions)
+    face_cropper, transform_extra_arguments = get_cropper(
+        annotation_type, fixed_positions
+    )
     return get_pipeline(face_cropper, transform_extra_arguments)
-   
+
+
 pipeline = load(annotation_type, fixed_positions)
-transformer = pipeline.transformer
\ No newline at end of file
+transformer = pipeline.transformer
-- 
GitLab