Skip to content

Commit

Permalink
additional (final) changes to vectorization, doesn't solve ambiguity.
Browse files Browse the repository at this point in the history
  • Loading branch information
vincenzooo committed Mar 6, 2024
1 parent a931fd3 commit d22dc4c
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 44 deletions.
8 changes: 5 additions & 3 deletions source/dataIO/arrays.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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.
Expand Down
69 changes: 37 additions & 32 deletions source/dataIO/dicts.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
26 changes: 17 additions & 9 deletions source/pySurf/scripts/dlist.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit d22dc4c

Please sign in to comment.