Skip to content

Commit

Permalink
Use tempfile.TemporaryDirectory instead of own implementation (#83)
Browse files Browse the repository at this point in the history
The old implementation was only because python 2.7 didn't
have some features that I needed. But pysaliency has long
given up python 2.7 compatibility.

Signed-off-by: Matthias Kümmerer <[email protected]>
  • Loading branch information
matthias-k authored Oct 28, 2024
1 parent c2ea6de commit 7313880
Show file tree
Hide file tree
Showing 20 changed files with 91 additions and 205 deletions.
1 change: 0 additions & 1 deletion pysaliency/external_datasets/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

from ..datasets import FixationTrains
from ..utils import (
TemporaryDirectory,
download_and_check,
atomic_directory_setup,
)
Expand Down
25 changes: 10 additions & 15 deletions pysaliency/external_datasets/cat2000.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,20 @@
from __future__ import absolute_import, print_function, division
from __future__ import absolute_import, division, print_function

import zipfile
import glob
import os
import warnings
import glob
import zipfile
from tempfile import TemporaryDirectory

import numpy as np
from scipy.io import loadmat
from natsort import natsorted
from pkg_resources import resource_string
from scipy.io import loadmat
from tqdm import tqdm

from ..datasets import FixationTrains
from ..utils import (
TemporaryDirectory,
filter_files,
run_matlab_cmd,
download_and_check,
atomic_directory_setup)

from .utils import create_stimuli, _load
from ..utils import atomic_directory_setup, download_and_check, filter_files, run_matlab_cmd
from .utils import _load, create_stimuli


def get_cat2000_test(location=None):
Expand Down Expand Up @@ -47,7 +42,7 @@ def get_cat2000_test(location=None):
return stimuli
os.makedirs(location)
with atomic_directory_setup(location):
with TemporaryDirectory(cleanup=True) as temp_dir:
with TemporaryDirectory() as temp_dir:
download_and_check('http://saliency.mit.edu/testSet.zip',
os.path.join(temp_dir, 'testSet.zip'),
'903ec668df2e5a8470aef9d8654e7985')
Expand Down Expand Up @@ -170,7 +165,7 @@ def _get_cat2000_train(name, location):
return stimuli, fixations
os.makedirs(location)
with atomic_directory_setup(location):
with TemporaryDirectory(cleanup=True) as temp_dir:
with TemporaryDirectory() as temp_dir:
download_and_check('http://saliency.mit.edu/trainSet.zip',
os.path.join(temp_dir, 'trainSet.zip'),
'56ad5c77e6c8f72ed9ef2901628d6e48')
Expand Down Expand Up @@ -298,7 +293,7 @@ def _get_cat2000_train_v1_1(name, location):
return stimuli, fixations
os.makedirs(location)
with atomic_directory_setup(location):
with TemporaryDirectory(cleanup=True) as temp_dir:
with TemporaryDirectory() as temp_dir:
download_and_check('http://saliency.mit.edu/trainSet.zip',
os.path.join(temp_dir, 'trainSet.zip'),
'56ad5c77e6c8f72ed9ef2901628d6e48')
Expand Down
5 changes: 3 additions & 2 deletions pysaliency/external_datasets/coco_freeview.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import json
import os
import zipfile
from tempfile import TemporaryDirectory

import numpy as np
from tqdm import tqdm

from ..datasets import ScanpathFixations, Scanpaths, create_subset
from ..utils import TemporaryDirectory, atomic_directory_setup, download_and_check, filter_files
from ..utils import atomic_directory_setup, download_and_check, filter_files
from .coco_search18 import _prepare_stimuli
from .utils import _load, create_stimuli

Expand Down Expand Up @@ -159,7 +160,7 @@ def get_COCO_Freeview(location=None, test_data=None):
os.makedirs(location)

with atomic_directory_setup(location):
with TemporaryDirectory(cleanup=True) as temp_dir:
with TemporaryDirectory() as temp_dir:
download_and_check('http://vision.cs.stonybrook.edu/~cvlab_download/COCOSearch18-images-TP.zip',
os.path.join(temp_dir, 'COCOSearch18-images-TP.zip'),
'4a815bb591cb463ab77e5ba0c68fedfb')
Expand Down
5 changes: 3 additions & 2 deletions pysaliency/external_datasets/coco_search18.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@
import shutil
import zipfile
from hashlib import md5
from tempfile import TemporaryDirectory

import numpy as np
from PIL import Image
from tqdm import tqdm

from ..datasets import ScanpathFixations, Scanpaths, create_subset
from ..utils import TemporaryDirectory, atomic_directory_setup, download_and_check, filter_files
from ..utils import atomic_directory_setup, download_and_check, filter_files
from .utils import _load, create_stimuli

condition_mapping = {
Expand Down Expand Up @@ -80,7 +81,7 @@ def get_COCO_Search18(location=None, split=1, merge_tasks=True, unique_images=Tr
os.makedirs(location)

with atomic_directory_setup(location):
with TemporaryDirectory(cleanup=True) as temp_dir:
with TemporaryDirectory() as temp_dir:
download_and_check('http://vision.cs.stonybrook.edu/~cvlab_download/COCOSearch18-images-TP.zip',
os.path.join(temp_dir, 'COCOSearch18-images-TP.zip'),
'4a815bb591cb463ab77e5ba0c68fedfb')
Expand Down
4 changes: 2 additions & 2 deletions pysaliency/external_datasets/dut_omrom.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import glob
import os
import zipfile
from tempfile import TemporaryDirectory
from typing import Tuple

import numpy as np
Expand All @@ -11,7 +12,6 @@

from ..datasets import ScanpathFixations, Scanpaths, Stimuli
from ..utils import (
TemporaryDirectory,
atomic_directory_setup,
download_and_check,
)
Expand Down Expand Up @@ -53,7 +53,7 @@ def get_DUT_OMRON(location=None) -> Tuple[Stimuli, ScanpathFixations]:
n_fixations = 0

with atomic_directory_setup(location):
with TemporaryDirectory(cleanup=True) as temp_dir:
with TemporaryDirectory() as temp_dir:

download_and_check('http://saliencydetection.net/dut-omron/download/DUT-OMRON-image.zip',
os.path.join(temp_dir, 'DUT-OMRON-image.zip'),
Expand Down
19 changes: 9 additions & 10 deletions pysaliency/external_datasets/figrim.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
from __future__ import absolute_import, print_function, division
from __future__ import absolute_import, division, print_function

import zipfile
import os
import glob
import os
import zipfile
from tempfile import TemporaryDirectory

import numpy as np
from scipy.io import loadmat
from natsort import natsorted
from boltons.fileutils import mkdir_p
from natsort import natsorted
from scipy.io import loadmat

from ..datasets import FixationTrains
from ..utils import (
TemporaryDirectory,
download_and_check,
atomic_directory_setup,
download_and_check,
)

from .utils import create_stimuli, _load
from .utils import _load, create_stimuli


def _load_FIGRIM_data(filename, stimuli_indices, stimulus_type):
Expand Down Expand Up @@ -94,7 +93,7 @@ def get_FIGRIM(location=None):
return stimuli, fixations
os.makedirs(location)
with atomic_directory_setup(location):
with TemporaryDirectory(cleanup=True) as temp_dir:
with TemporaryDirectory() as temp_dir:
download_and_check('http://figrim.mit.edu/Fillers.zip',
os.path.join(temp_dir, 'Fillers.zip'),
'dc0bc9561b5bc90e158ec32074dd1060')
Expand Down
16 changes: 6 additions & 10 deletions pysaliency/external_datasets/isun.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
from __future__ import absolute_import, print_function, division
from __future__ import absolute_import, division, print_function

import zipfile
import os
import zipfile
from tempfile import TemporaryDirectory

from scipy.io import loadmat

from ..datasets import FixationTrains
from ..utils import (
TemporaryDirectory,
filter_files,
download_and_check,
atomic_directory_setup)

from .utils import create_stimuli, _load
from ..utils import atomic_directory_setup, download_and_check, filter_files
from .utils import _load, create_stimuli


# TODO: Add test
Expand Down Expand Up @@ -44,7 +40,7 @@ def get_iSUN(location=None):
return stimuli_training, stimuli_validation, stimuli_testing, fixations_training, fixations_validation
os.makedirs(location)
with atomic_directory_setup(location):
with TemporaryDirectory(cleanup=True) as temp_dir:
with TemporaryDirectory() as temp_dir:
download_and_check('http://lsun.cs.princeton.edu/challenge/2015/eyetracking/data/training.mat',
os.path.join(temp_dir, 'training.mat'),
'5a8b15134b17c7a3f69b087845db1363')
Expand Down
14 changes: 6 additions & 8 deletions pysaliency/external_datasets/koehler.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
from __future__ import absolute_import, print_function, division
from __future__ import absolute_import, division, print_function

import zipfile
import itertools
import os
import zipfile
from tempfile import TemporaryDirectory

import itertools
import numpy as np
from scipy.io import loadmat

from boltons.fileutils import mkdir_p
from tqdm import tqdm

from ..datasets import FixationTrains
from ..utils import (
TemporaryDirectory,
atomic_directory_setup,
check_file_hash,
)
)
from .utils import _load, create_stimuli

from .utils import create_stimuli, _load

def _get_koehler_fixations(data, task, n_stimuli):
tasks = {'freeviewing': 'freeview',
Expand Down
7 changes: 4 additions & 3 deletions pysaliency/external_datasets/mit.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import glob
import os
import zipfile
from tempfile import TemporaryDirectory

import numpy as np
from natsort import natsorted
Expand All @@ -11,7 +12,7 @@
from scipy.io import loadmat

from ..datasets import ScanpathFixations, Scanpaths
from ..utils import TemporaryDirectory, atomic_directory_setup, build_padded_2d_array, download_and_check, filter_files, run_matlab_cmd
from ..utils import atomic_directory_setup, download_and_check, filter_files, run_matlab_cmd
from .utils import _load, create_stimuli


Expand All @@ -37,7 +38,7 @@ def _get_mit1003(dataset_name, location=None, include_initial_fixation=False, on
return stimuli, fixations
os.makedirs(location)
with atomic_directory_setup(location):
with TemporaryDirectory(cleanup=True) as temp_dir:
with TemporaryDirectory() as temp_dir:
download_and_check('http://people.csail.mit.edu/tjudd/WherePeopleLook/ALLSTIMULI.zip',
os.path.join(temp_dir, 'ALLSTIMULI.zip'),
'0d7df8b954ecba69b6796e77b9afe4b6')
Expand Down Expand Up @@ -389,7 +390,7 @@ def get_mit300(location=None):
return stimuli
os.makedirs(location)
with atomic_directory_setup(location):
with TemporaryDirectory(cleanup=True) as temp_dir:
with TemporaryDirectory() as temp_dir:
download_and_check('http://saliency.mit.edu/BenchmarkIMAGES.zip',
os.path.join(temp_dir, 'BenchmarkIMAGES.zip'),
'03ed32bdf5e4289950cd28df89451260')
Expand Down
7 changes: 3 additions & 4 deletions pysaliency/external_datasets/nusef.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,18 @@
import glob
import os
import zipfile
from datetime import datetime, timedelta
from datetime import datetime
from tempfile import TemporaryDirectory

from tqdm import tqdm

from ..datasets import FixationTrains
from ..utils import (
TemporaryDirectory,
atomic_directory_setup,
download_and_check,
)
from .utils import _load, create_stimuli


IMAGE_TYPOS = {
'3005_0.jpg': '3005.1.jpg',
'3005_2.jpg': '3005.2.jpg',
Expand Down Expand Up @@ -92,7 +91,7 @@ def get_NUSEF_public(location=None):
return stimuli, fixations
os.makedirs(location)
with atomic_directory_setup(location):
with TemporaryDirectory(cleanup=True) as temp_dir:
with TemporaryDirectory() as temp_dir:

source_directory = os.path.join(location, 'src')
os.makedirs(source_directory)
Expand Down
13 changes: 6 additions & 7 deletions pysaliency/external_datasets/osie.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
from __future__ import absolute_import, print_function, division
from __future__ import absolute_import, division, print_function

import os
import urllib
from tempfile import TemporaryDirectory

import numpy as np
from scipy.io import loadmat
from boltons.fileutils import mkdir_p
from scipy.io import loadmat
from tqdm import tqdm

from ..datasets import FixationTrains
from ..utils import (
TemporaryDirectory,
download_and_check,
atomic_directory_setup,
download_and_check,
)

from .utils import create_stimuli, _load
from .utils import _load, create_stimuli


def get_OSIE(location=None):
Expand Down Expand Up @@ -45,7 +44,7 @@ def get_OSIE(location=None):
return stimuli, fixations
os.makedirs(location)
with atomic_directory_setup(location):
with TemporaryDirectory(cleanup=True) as temp_dir:
with TemporaryDirectory() as temp_dir:
stimuli_src_location = os.path.join(temp_dir, 'stimuli')
mkdir_p(stimuli_src_location)
images = []
Expand Down
4 changes: 2 additions & 2 deletions pysaliency/external_datasets/pascal_s.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

import os
import zipfile
from tempfile import TemporaryDirectory

import numpy as np
import requests

from ..datasets import ScanpathFixations, Scanpaths
from ..utils import (
TemporaryDirectory,
atomic_directory_setup,
download_and_check,
)
Expand Down Expand Up @@ -50,7 +50,7 @@ def get_PASCAL_S(location=None):
n_stimuli = 850

with atomic_directory_setup(location):
with TemporaryDirectory(cleanup=True) as temp_dir:
with TemporaryDirectory() as temp_dir:

try:
download_and_check('http://cbs.ic.gatech.edu/salobj/download/salObj.zip',
Expand Down
Loading

0 comments on commit 7313880

Please sign in to comment.