|
| 1 | +import json |
| 2 | +import os |
| 3 | +from pathlib import Path |
| 4 | + |
| 5 | +import pooch |
| 6 | +from nipype.interfaces.base import ( |
| 7 | + DynamicTraitedSpec, |
| 8 | + File, |
| 9 | + SimpleInterface, |
| 10 | + TraitedSpec, |
| 11 | + traits, |
| 12 | +) |
| 13 | + |
| 14 | +import nibabies |
| 15 | + |
| 16 | + |
| 17 | +class _RetrievePoochFilesInputSpec(DynamicTraitedSpec): |
| 18 | + intermediate = traits.Str(required=True, desc='the intermediate space') |
| 19 | + target = traits.Str(required=True, desc='the target space') |
| 20 | + |
| 21 | + |
| 22 | +class _RetrievePoochFilesOutputSpec(TraitedSpec): |
| 23 | + int2tgt_xfm = File(desc='Intermediate to target transform') |
| 24 | + tgt2int_xfm = File(desc='Target to intermediate transform') |
| 25 | + |
| 26 | + |
| 27 | +class RetrievePoochFiles(SimpleInterface): |
| 28 | + input_spec = _RetrievePoochFilesInputSpec |
| 29 | + output_spec = _RetrievePoochFilesOutputSpec |
| 30 | + |
| 31 | + def _run_interface(self, runtime): |
| 32 | + int2tgt, tgt2int = _retrieve_xfms(self.inputs.intermediate, self.inputs.target) |
| 33 | + self._results['int2tgt_xfm'] = int2tgt |
| 34 | + self._results['tgt2int_xfm'] = tgt2int |
| 35 | + return runtime |
| 36 | + |
| 37 | + |
| 38 | +def _retrieve_xfms( |
| 39 | + intermediate: str, |
| 40 | + target: str, |
| 41 | +): |
| 42 | + """Fetch transforms from the OSF repository (https://osf.io/y763j/).""" |
| 43 | + |
| 44 | + manifest = json.loads(nibabies.data.load('xfm_manifest.json').read_text()) |
| 45 | + |
| 46 | + def sanitize(space): |
| 47 | + # MNIInfant:cohort-1 -> MNIInfant+1 |
| 48 | + return space.replace(':cohort-', '+') |
| 49 | + |
| 50 | + intmd = sanitize(intermediate) |
| 51 | + tgt = sanitize(target) |
| 52 | + |
| 53 | + cache_dir = Path(os.getenv('NIBABIES_POOCH_DIR', Path.cwd())) |
| 54 | + |
| 55 | + int2std_name = f'from-{intmd}_to-{tgt}_xfm.h5' |
| 56 | + int2std_meta = manifest[int2std_name] |
| 57 | + int2std = pooch.retrieve( |
| 58 | + url=int2std_meta['url'], |
| 59 | + path=cache_dir, |
| 60 | + known_hash=int2std_meta['hash'], |
| 61 | + fname=int2std_name, |
| 62 | + ) |
| 63 | + |
| 64 | + std2int_name = f'from-{tgt}_to-{intmd}_xfm.h5' |
| 65 | + std2int_meta = manifest[std2int_name] |
| 66 | + std2int = pooch.retrieve( |
| 67 | + url=std2int_meta['url'], |
| 68 | + path=cache_dir, |
| 69 | + known_hash=std2int_meta['hash'], |
| 70 | + fname=std2int_name, |
| 71 | + ) |
| 72 | + |
| 73 | + return int2std, std2int |
0 commit comments