@@ -385,15 +385,40 @@ def _ts_first(paths):
385385
386386 return req
387387
388- def get_uploads (self , id ):
388+ def get_uploads (self , id , select = 'latest' , ** kwargs ):
389389 """ Get NeuroVault uploads associated with this analysis
390390 :param str id: Analysis hash_id.
391+ :param str select: How to select from multiple collections.
392+ Options: "latest", "oldest" or None. If None, returns all results.
393+ :param dict kwargs: Attributes to filter collections on.
394+ If any attributes are not found, they are ignored.
391395 :return: client response object
392396 """
393- return self .get (id = id , sub_route = 'upload' )
397+ uploads = self .get (id = id , sub_route = 'upload' )
398+
399+ # Sort by date
400+ uploads = sorted (uploads , key = lambda x : datetime .datetime .strptime (
401+ x ['uploaded_at' ], '%Y-%m-%dT%H:%M' ),
402+ reverse = (select == 'latest' ))
403+
404+ # Select collections based on filters
405+ uploads = [
406+ u for u in uploads
407+ if all ([u .get (k ) == v for k , v in kwargs .items () if k in u ])
408+ ]
409+
410+ if not uploads :
411+ return None
412+
413+ # Select first item unless all are requested
414+ if select is not None :
415+ uploads = [uploads [0 ]]
416+
417+ return uploads
394418
395419 def load_uploads (self , id , select = 'latest' ,
396- download_dir = None , ** kwargs ):
420+ download_dir = None , collection_filters = {},
421+ image_filters = {}):
397422 """ Load collection upload as NiBabel images and associated meta-data
398423 You can filter which images are loaded based on either collection
399424 level attributes or statmap image level attributes. These correspond
@@ -405,7 +430,8 @@ def load_uploads(self, id, select='latest',
405430 :param str select: How to select from multiple collections.
406431 Options: "latest", "oldest" or None. If None, returns all results.
407432 :param str download_dir: Path to download images. If None, tempdir.
408- :param dict kwargs: Attributes to filter images on.
433+ :param dict collection_filters: Attributes to filter collections on.
434+ :param dict image_filters: Attributes to filter images on.
409435 If any attributes are not found, they are ignored.
410436 :return list list of tuples of format (Nifti1Image, kwargs).
411437 """
@@ -415,30 +441,15 @@ def load_uploads(self, id, select='latest',
415441 download_dir = Path (download_dir )
416442
417443 # Sort uploads for upload date
418- uploads = self .get_uploads (id )
419- for u in uploads :
420- u ['uploaded_at' ] = datetime .datetime .strptime (
421- u ['uploaded_at' ], '%Y-%m-%dT%H:%M' )
422- uploads = sorted (uploads , key = lambda x : x ['uploaded_at' ],
423- reverse = (select == 'latest' ))
424-
425- # Select collections based on filters
426- uploads = [
427- u for u in uploads
428- if all ([u .get (k ) == v for k , v in kwargs .items () if k in u ])
429- ]
430-
444+ uploads = self .get_uploads (id , ** collection_filters )
445+
431446 if not uploads :
432447 return None
433448
434- # Select first item unless all are requested
435- if select is not None :
436- uploads = [uploads [0 ]]
437-
438449 # Extract entities from file path
439450 def _get_entities (path ):
440451 di = {}
441- for t in ['task' , 'contrast' , 'stat' ]:
452+ for t in ['task' , 'contrast' , 'stat' , 'space' ]:
442453 matches = re .findall (f"{ t } -(.*?)_" , path )
443454 if matches :
444455 di [t ] = matches [0 ]
@@ -452,7 +463,7 @@ def _get_entities(path):
452463
453464 # If file matches kwargs and is in NV
454465 if f .pop ('status' ) == 'OK' and all (
455- [f .get (k , None ) == v for k , v in kwargs .items () if k in f ]):
466+ [f .get (k , None ) == v if k in f else False for k , v in image_filters .items ()]):
456467 # Download and open
457468 img_url = "https://neurovault.org/media/images/" \
458469 f"{ u ['collection_id' ]} /{ f ['basename' ]} "
@@ -482,7 +493,7 @@ def plot_uploads(self, id, plot_args={}, **kwargs):
482493 plots = []
483494 for niimg , _ in images :
484495 plots .append (
485- nilearn . plotting .plot_stat_map (niimg , ** plot_args ))
496+ niplt .plot_stat_map (niimg , ** plot_args ))
486497
487498 return plots
488499 else :
0 commit comments