From d22dc4c81689dd01d9004a5486647c6990bddde9 Mon Sep 17 00:00:00 2001 From: Vincenzooo Date: Wed, 6 Mar 2024 22:11:21 +0100 Subject: [PATCH] additional (final) changes to vectorization, doesn't solve ambiguity. --- source/dataIO/arrays.py | 8 ++-- source/dataIO/dicts.py | 69 ++++++++++++++++++---------------- source/pySurf/scripts/dlist.py | 26 ++++++++----- 3 files changed, 59 insertions(+), 44 deletions(-) diff --git a/source/dataIO/arrays.py b/source/dataIO/arrays.py index 1c7a90c..a426a73 100644 --- a/source/dataIO/arrays.py +++ b/source/dataIO/arrays.py @@ -242,13 +242,13 @@ def vectorize(arg, n, force_if_n=False): """ - if (np.size(arg) != n) or force_if_n: + if (real_len(arg) != n) or force_if_n: try: arg=[arg.deepcopy() for _ in range(n)] - except AttributeError: # fails e.g. if tuple which don't have copy method + except AttributeError: #fails e.g. if tuple which don't have copy method arg=[arg for _ in range(n)] else: - if (n == 1) and len(np.shape(arg)) == 0: # distinguish between [1] and 1 + if (n == 1) and len(np.shape(arg)) == 0: arg = [arg] return arg @@ -276,6 +276,8 @@ def test_vectorize(): assert vectorize([[1,2],[3,4]],2) == [[1,2],[3,4]] assert vectorize([[1,2],[3,4]],2,True) == [[[1,2],[3,4]],[[1,2],[3,4]]] + + print('All tests passed.') def make_raster(*vectors, as_axis = False, extend = True): """Create a raster grid on the base of a list of vectors. diff --git a/source/dataIO/dicts.py b/source/dataIO/dicts.py index 9208011..6982580 100644 --- a/source/dataIO/dicts.py +++ b/source/dataIO/dicts.py @@ -8,43 +8,48 @@ filterByKey2 = lambda data,keys : {key: data[key] for key in keys if key in data} +from dataIO import arrays + def vectorize(kwargs,n): if kwargs : #passed explicit parameters for each reader # Note, there is ambiguity when rfiles and a kwargs value have same - # number of elements() - #pdb.set_trace() - #vectorize all values - for k,v in kwargs.items(): - if (np.size(v) == 1): - kwargs[k]=[v for i in range(n)] - #elif (len(v) != len(rfiles)): - else: - try: - kwargs[k]=[v.copy() for i in range(n)] - except AttributeError: #fails e.g. if tuple which don't have copy method - kwargs[k]=[v for i in range(n)] - # else: #non funziona perche' ovviamente anche chiamando esplicitamente, sara' - # # sempre di lunghezza identica a rfiles. - # print ('WARNING: ambiguity detected, it is not possible to determine'+ - # 'if `%s` values are intended as n-element value or n values for each data.\n'%k+ - # 'To solve, call the function explicitly repeating the value, es. `units=[['um','um','nm']]*3.`') - # in realtà la forma "esplicità" fallisce al plot, mi fa temere che non funzioni anche se le immagini l'altra funziona. - - - # 2020/07/10 args overwrite kwargs (try to avoid duplicates anyway). - # args were ignored before. - - #if not args: #assume is correct number of elements - # args = [[]]*len(rfiles) ## Non va fatto cosi'!! senno' duplica "by ref" + # number of elements. This cannot be easily solved: + # it's impossible to say if n values are intended for n files or as n-value parameter, + # to be replicated for n files. - #pdb.set_trace() + res = [{} for _ in range(n)] - #transform vectorized kwargs in list of kwargs - kwargs=[{k:deepcopy(v[i]) for k,v in kwargs.items()} for i in range(n)] - else: - kwargs = [{} for i in range(n)] - - return kwargs + for k,v in kwargs.items(): + vvec = arrays.vectorize(v,n) + for r,val in zip(res,vvec): + r[k]=val + + return res + +def test_vectorize(): + + kwargs = {'scale':[(-1,-1,1),(1,1,-1),(-1,-1,1)], + 'units':[['mm','mm','um'],['mm','mm','um'],['mm','mm','$\mu$m']]} + + # # deve essere: + # kwargs == [{'scale': (-1, -1, 1),'units': ['mm', 'mm', 'um']}, + # {'scale': (1, 1, -1), 'units': ['mm', 'mm', 'um']}, + # {'scale': (-1, -1, 1), 'units': ['mm', 'mm', '$\\mu$m']}] + + assert vectorize(kwargs,3) == [{'scale': (-1, -1, 1),'units': ['mm', 'mm', 'um']}, + {'scale': (1, 1, -1), 'units': ['mm', 'mm', 'um']}, + {'scale': (-1, -1, 1), 'units': ['mm', 'mm', '$\\mu$m']}] + + kwargs = {'scale':(-1,-1,1), + 'units':['mm','mm','um']} + + assert vectorize(kwargs,3) == [{'scale': (-1, -1, 1),'units': ['mm', 'mm', 'um']}, + {'scale': (-1,-1,1), 'units': ['mm', 'mm', 'um']}, + {'scale': (-1,-1,1), 'units': ['mm', 'mm', 'um']}] + + + + print('All tests passed.') def pop_kw(kws,defaults=None,keylist=None,exclude=None): """A multiple pop function for a dictionary, hopefully useful to filter kws and manage function **kwargs parameters. diff --git a/source/pySurf/scripts/dlist.py b/source/pySurf/scripts/dlist.py index 42d4781..0c6420a 100644 --- a/source/pySurf/scripts/dlist.py +++ b/source/pySurf/scripts/dlist.py @@ -65,13 +65,17 @@ def load_dlist(rfiles,reader=None,*args,**kwargs): # import pdb # pdb.set_trace() + # if reader is None: + # #reader=auto_reader(rfiles[0]) + # reader = [auto_reader(r) for r in rfiles] + + # if np.size(reader) ==1: + # reader=[reader]*len(rfiles) + if reader is None: #reader=auto_reader(rfiles[0]) reader = [auto_reader(r) for r in rfiles] - if np.size(reader) ==1: - reader=[reader]*len(rfiles) - ''' if kwargs : #passed explicit parameters for all readers pdb.set_trace() @@ -135,22 +139,26 @@ def load_dlist(rfiles,reader=None,*args,**kwargs): from dataIO import dicts kwargs = dicts.vectorize(kwargs,len(rfiles)) - kwargs = [{'scale': (-1, -1, 1),'units': ['mm', 'mm', 'um']}, + kwargs = [{'scale': (-1, -1, 1),'units': ['mm', 'mm', 'um']}, #correct result of vectorization {'scale': (1, 1, -1), 'units': ['mm', 'mm', 'um']}, {'scale': (-1, -1, 1), 'units': ['mm', 'mm', '$\\mu$m']}] from dataIO import arrays + reader = arrays.vectorize(reader,len(rfiles)) args = arrays.vectorize(args,len(rfiles)) #kwargs here is a list of dictionaries {option:value}, matching the readers #dlist=[Data2D(file=wf1,reader=r,**{**k, **a}) for wf1,r,k,a in zip(rfiles,reader,args,kwargs)] # import pdb # pdb.set_trace() - # dlist=Dlist([Data2D(file=wf1,reader=r,*a,**k) for wf1,r,a,k in zip(rfiles,reader,args,kwargs)]) - for wf1,r,a,k in zip(rfiles,reader,args,kwargs): - print("load %s with reader %s and args: %s, keywords:"%(os.path.basename(wf1),r,a),k) - print('-----------------------\n') - dlist = None + #dlist=Dlist([Data2D(file=wf1,reader=r,*a,**k) for wf1,r,a,k in zip(rfiles,reader,args,kwargs)]) + + dlist=Dlist([Data2D(file=wf1,reader=r,*a,**k) for wf1,r,a,k in zip(rfiles,reader,args,kwargs)]) # OK + + # for wf1,r,a,k in zip(rfiles,reader,args,kwargs): + # print("load %s with reader %s and args: %s, keywords:"%(os.path.basename(wf1),r,a),k) + # print('-----------------------\n') + # dlist = None return dlist