@@ -87,7 +87,12 @@ def __init__(self, cache_dir=None, mode='local', wildcards=True, tables_dir=None
8787 self .uuid_filenames = False
8888 # init the cache file
8989 self ._reset_cache ()
90- self .load_cache ()
90+ if self .mode == 'local' :
91+ # Ensure that we don't call any subclass method here as we only load local cache
92+ # tables on init. Direct calls to load_cache can be made by the user or subclass.
93+ One .load_cache (self )
94+ elif self .mode != 'remote' :
95+ raise ValueError (f'Mode "{ self .mode } " not recognized' )
9196
9297 def __repr__ (self ):
9398 return f'One ({ "off" if self .offline else "on" } line, { self .cache_dir } )'
@@ -237,39 +242,6 @@ def _save_cache(self, save_dir=None, force=False):
237242 _logger .debug (f'Saved { filename } ' )
238243 meta ['saved_time' ] = datetime .now ()
239244
240- def refresh_cache (self , mode = None ):
241- """Check and reload cache tables.
242-
243- Parameters
244- ----------
245- mode : {'local', 'refresh', 'remote'}
246- Options are 'local' (reload if expired); 'refresh' (reload); 'remote' (don't reload).
247-
248- Returns
249- -------
250- datetime.datetime
251- Loaded timestamp.
252-
253- """
254- # NB: Currently modified table will be lost if called with 'refresh';
255- # May be instances where modified cache is saved then immediately replaced with a new
256- # remote cache. Also it's too slow :(
257- # self.save_cache() # Save cache if modified
258- mode = mode or self .mode
259- if mode == 'remote' :
260- pass
261- elif mode == 'local' :
262- loaded_time = self ._cache ['_meta' ]['loaded_time' ]
263- if not loaded_time or (datetime .now () - loaded_time >= self .cache_expiry ):
264- _logger .info ('Cache expired, refreshing' )
265- self .load_cache ()
266- elif mode == 'refresh' :
267- _logger .debug ('Forcing reload of cache' )
268- self .load_cache (clobber = True )
269- else :
270- raise ValueError (f'Unknown refresh type "{ mode } "' )
271- return self ._cache ['_meta' ]['loaded_time' ]
272-
273245 def _update_cache_from_records (self , strict = False , ** kwargs ):
274246 """Update the cache tables with new records.
275247
@@ -309,6 +281,8 @@ def _update_cache_from_records(self, strict=False, **kwargs):
309281 if isinstance (records , pd .Series ):
310282 records = pd .DataFrame ([records ])
311283 records .index .set_names (self ._cache [table ].index .names , inplace = True )
284+ # Drop duplicate indices
285+ records = records [~ records .index .duplicated (keep = 'first' )]
312286 if not strict :
313287 # Deal with case where there are extra columns in the cache
314288 extra_columns = list (set (self ._cache [table ].columns ) - set (records .columns ))
@@ -323,7 +297,7 @@ def _update_cache_from_records(self, strict=False, **kwargs):
323297 records .insert (n , col , val )
324298 # Drop any extra columns in the records that aren't in cache table
325299 to_drop = set (records .columns ) - set (self ._cache [table ].columns )
326- records .drop (to_drop , axis = 1 , inplace = True )
300+ records = records .drop (to_drop , axis = 1 )
327301 records = records .reindex (columns = self ._cache [table ].columns )
328302 assert set (self ._cache [table ].columns ) == set (records .columns )
329303 records = records .astype (self ._cache [table ].dtypes )
@@ -1767,8 +1741,6 @@ def load_cache(self, tables_dir=None, clobber=False, tag=None):
17671741 location and creation date of the remote cache. If newer, it will be download and
17681742 loaded.
17691743
1770- Note: Unlike refresh_cache, this will always reload the local files at least once.
1771-
17721744 Parameters
17731745 ----------
17741746 tables_dir : str, pathlib.Path
@@ -1802,7 +1774,7 @@ def load_cache(self, tables_dir=None, clobber=False, tag=None):
18021774 if not (clobber or different_tag ):
18031775 super (OneAlyx , self ).load_cache (tables_dir ) # Load any present cache
18041776 expired = self ._cache and (cache_meta := self ._cache .get ('_meta' , {}))['expired' ]
1805- if not expired or self . mode in { 'local' , 'remote' } :
1777+ if not expired :
18061778 return
18071779
18081780 # Warn user if expired
@@ -2598,9 +2570,10 @@ def _dset2url(self, dset, update_cache=True):
25982570 if isinstance (dset , str ) and dset .startswith ('http' ):
25992571 url = dset
26002572 elif isinstance (dset , (str , Path )):
2601- url = self .path2url (dset )
2602- if not url :
2603- _logger .warning (f'Dataset { dset } not found in cache' )
2573+ try :
2574+ url = self .path2url (dset )
2575+ except alferr .ALFObjectNotFound :
2576+ _logger .warning (f'Dataset { dset } not found' )
26042577 return
26052578 elif isinstance (dset , (list , tuple )):
26062579 dset2url = partial (self ._dset2url , update_cache = update_cache )
@@ -2914,7 +2887,12 @@ def path2url(self, filepath, query_type=None) -> str:
29142887 return super (OneAlyx , self ).path2url (filepath )
29152888 eid = self .path2eid (filepath )
29162889 try :
2917- dataset , = self .alyx .rest ('datasets' , 'list' , session = eid , name = Path (filepath ).name )
2890+ params = {'name' : Path (filepath ).name }
2891+ if eid is None :
2892+ params ['django' ] = 'session__isnull,True'
2893+ else :
2894+ params ['session' ] = str (eid )
2895+ dataset , = self .alyx .rest ('datasets' , 'list' , ** params )
29182896 return next (
29192897 r ['data_url' ] for r in dataset ['file_records' ] if r ['data_url' ] and r ['exists' ])
29202898 except (ValueError , StopIteration ):
0 commit comments