From 4a87e2e16882e31b1c64981e257b2347375642a5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=20Dupr=C3=A9=20la=20Tour?= Date: Thu, 16 Dec 2021 12:42:40 -0800 Subject: [PATCH 1/2] FIX add downloaded subjects to the cortex database --- cortex/tests/test_utils.py | 12 ++++++++++++ cortex/utils.py | 9 ++++++--- 2 files changed, 18 insertions(+), 3 deletions(-) create mode 100644 cortex/tests/test_utils.py diff --git a/cortex/tests/test_utils.py b/cortex/tests/test_utils.py new file mode 100644 index 000000000..13cccb0e3 --- /dev/null +++ b/cortex/tests/test_utils.py @@ -0,0 +1,12 @@ +import cortex + +def test_download_subject(): + # Test that newly downloaded subjects are added to the current database. + + # remove fsaverage from the list of available subjects if present. + if "fsaverage" in cortex.db.subjects: + cortex.db._subjects.pop("fsaverage") + + assert "fsaverage" not in cortex.db.subjects + cortex.utils.download_subject(subject_id='fsaverage') + assert "fsaverage" in cortex.db.subjects diff --git a/cortex/utils.py b/cortex/utils.py index d172d0dd1..82694a7ce 100644 --- a/cortex/utils.py +++ b/cortex/utils.py @@ -1005,7 +1005,7 @@ def download_subject(subject_id='fsaverage', url=None, pycortex_store=None, to add more in the future. If provided, URL overrides `subject_id` pycortex_store : string or None Directory to which to put the new subject folder. If None, defaults to - the `filestore` variable specified in the pycortex config file. + current filestore in use (`cortex.db.filestore`). download_again : bool Download the data again even if the subject id is already present in the pycortex's database. @@ -1030,13 +1030,16 @@ def download_subject(subject_id='fsaverage', url=None, pycortex_store=None, print('Downloaded subject {} to {}'.format(subject_id, tmp_dir)) # Un-tar to pycortex store if pycortex_store is None: - # Default location is config file pycortex store. - pycortex_store = config.get('basic', 'filestore') + # Default location is current filestore in cortex.db + pycortex_store = db.filestore pycortex_store = os.path.expanduser(pycortex_store) with tarfile.open(os.path.join(tmp_dir, subject_id + '.tar.gz'), "r:gz") as tar: print("Extracting subject {} to {}".format(subject_id, pycortex_store)) tar.extractall(path=pycortex_store) + # force filestore reload + db._subjects = None + def rotate_flatmap(surf_id, theta, plot=False): """Rotate flatmap to be less V-shaped From 9baa6ff607314a296f62877d6958a3f9aa37da45 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=20Dupr=C3=A9=20la=20Tour?= Date: Thu, 16 Dec 2021 14:13:39 -0800 Subject: [PATCH 2/2] ENH add reload_subjects method --- cortex/database.py | 5 +++++ cortex/utils.py | 4 ++-- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/cortex/database.py b/cortex/database.py index ae998e9d6..93befa4d3 100644 --- a/cortex/database.py +++ b/cortex/database.py @@ -180,6 +180,11 @@ def subjects(self): self._subjects = dict([(sname, SubjectDB(sname, filestore=self.filestore)) for sname in subjs]) return self._subjects + def reload_subjects(self): + """Force the reload of the subject dictionary.""" + self._subjects = None + self.subjects + def get_anat(self, subject, type='raw', xfmname=None, recache=False, order=1, **kwargs): """Return anatomical information from the filestore. Anatomical information is defined as any volume-space anatomical information pertaining to the subject, such as T1 image, diff --git a/cortex/utils.py b/cortex/utils.py index 82694a7ce..49f77f19c 100644 --- a/cortex/utils.py +++ b/cortex/utils.py @@ -1037,8 +1037,8 @@ def download_subject(subject_id='fsaverage', url=None, pycortex_store=None, print("Extracting subject {} to {}".format(subject_id, pycortex_store)) tar.extractall(path=pycortex_store) - # force filestore reload - db._subjects = None + # reload all subjects from the filestore + db.reload_subjects() def rotate_flatmap(surf_id, theta, plot=False):