Skip to content

Commit

Permalink
Merge pull request ome#1 from mpievolbio-scicomp/fixes
Browse files Browse the repository at this point in the history
Fixes
  • Loading branch information
CFGrote authored and joshmoore committed Jan 11, 2022
2 parents 115e92f + bb3f13b commit 92778a1
Show file tree
Hide file tree
Showing 13 changed files with 43 additions and 1,183 deletions.
Expand Up @@ -28,7 +28,6 @@
@since 5.3
"""
from __future__ import print_function

import sys, os
import re
Expand Down Expand Up @@ -72,7 +71,7 @@ def RemoveMapAnnotations(conn, dtype, Id ):
conn.c.waitOnCmd(handle, loops=10, ms=500, failonerror=True,
failontimeout=False, closehandle=False)

except Exception, ex:
except Exception as ex:
print("Failed to delete links: {}".format(ex.message))
return

Expand Down Expand Up @@ -102,7 +101,7 @@ def AddKeysToMatchingFiles( conn, Id, global_kv, template, file_keys, spec_kv=No

existing_kv = GetExistingMapAnnotions(image)
updated_kv = copy.deepcopy(existing_kv)
for key,vals in global_kv.iteritems():
for key,vals in global_kv.items():
if key not in updated_kv: updated_kv[key] = set()
for val in vals:
updated_kv[key].add(val)
Expand All @@ -129,19 +128,12 @@ def AddKeysToMatchingFiles( conn, Id, global_kv, template, file_keys, spec_kv=No
updated_kv[key].add(val)

if( spec_kv is not None ):
for key,vals in spec_kv.iteritems():
for key,vals in spec_kv.items():
if key not in updated_kv: updated_kv[key] = set()
for val in vals:
updated_kv[key].add(val)


#print("existing_kv")
#for k,v in existing_kv.iteritems():
# print(" {} : {}".format(k,v))
#print("updated_kv")
#for k,v in updated_kv.iteritems():
# print(" {} : {}".format(k,v))
#print("Are they the same?",existing_kv == updated_kv )
nold_i = sum(map( len, existing_kv.values()))
nnew_i = sum(map( len, updated_kv.values()))
nkv_added = nkv_added+(nnew_i+nold_i)
Expand All @@ -151,11 +143,11 @@ def AddKeysToMatchingFiles( conn, Id, global_kv, template, file_keys, spec_kv=No
RemoveMapAnnotations( conn, 'image', image.getId() )
map_ann = omero.gateway.MapAnnotationWrapper(conn)
namespace = omero.constants.metadata.NSCLIENTMAPANNOTATION
namespace = "openmicroscopy.org/mapr/gene"
# namespace = "openmicroscopy.org/mapr/gene"
map_ann.setNs(namespace)
# convert the ordered dict to a list of lists
kv_list=[]
for k,vset in updated_kv.iteritems():
for k,vset in updated_kv.items():
for v in vset:
kv_list.append( [k,v] )
map_ann.setValue( kv_list )
Expand Down Expand Up @@ -268,21 +260,18 @@ def AddMapAnnotations(conn, dtype, Id ):
val = match.group(2)
if( key not in spec_kv ): spec_kv[key]=set()
spec_kv[key].add(val)
#print("Global k-v's")
#for k,v in global_kv.iteritems():
# print( k,v)

# now add the key value pairs to the dataset
existing_kv = GetExistingMapAnnotions(dataset)
if( existing_kv != global_kv ):
RemoveMapAnnotations( conn, 'dataset', dataset.getId() )
map_ann = omero.gateway.MapAnnotationWrapper(conn)
namespace = omero.constants.metadata.NSCLIENTMAPANNOTATION
namespace = "openmicroscopy.org/mapr/gene"
# namespace = "openmicroscopy.org/mapr/gene"
map_ann.setNs(namespace)
# convert the ordered dict to a list of lists
kv_list=[]
for k,vset in global_kv.iteritems():
for k,vset in global_kv.items():
for v in vset:
kv_list.append( [k,v] )
map_ann.setValue( kv_list )
Expand Down Expand Up @@ -325,10 +314,10 @@ def AddMapAnnotations(conn, dtype, Id ):
updated_kv[key].add(val)

print("existing_kv")
for k,v in existing_kv.iteritems():
for k,v in existing_kv.items():
print(" {} : {}".format(k,v))
print("updated_kv")
for k,v in updated_kv.iteritems():
for k,v in updated_kv.items():
print(" {} : {}".format(k,v))
print("Are they the same?",existing_kv == updated_kv )

Expand All @@ -337,13 +326,13 @@ def AddMapAnnotations(conn, dtype, Id ):
print("The key-values pairs are different")
RemoveMapAnnotations( conn, 'image', image.getId() )
map_ann = omero.gateway.MapAnnotationWrapper(conn)
namespace ="openmicroscopy.org/mapr/gene"
# namespace ="openmicroscopy.org/mapr/gene"
map_ann.setNs(namespace)
print("Namespace")
print(map_ann)
# convert the ordered dict to a list of lists
kv_list=[]
for k,vset in updated_kv.iteritems():
for k,vset in updated_kv.items():
for v in vset:
kv_list.append( [k,v] )
map_ann.setValue( kv_list )
Expand Down Expand Up @@ -454,5 +443,8 @@ def getObjects(conn, scriptParams):
# " details")
# # Insight will display the 'Message' parameter
#client.setOutput("Message", rstring(message))
except:
pass

finally:
client.closeSession()
Expand Up @@ -73,8 +73,8 @@ def attach_csv_file( conn, obj, data ):
# get the list of keys and maximum number of occurences
# A key can appear multiple times, for example multiple dyes can be used
key_union=OrderedDict()
for img_n,img_kv in data.iteritems():
for key, vset in img_kv.iteritems():
for img_n,img_kv in data.items():
for key, vset in img_kv.items():
key_union[key] = max(key_union.get(key,0),len(vset))
all_keys = key_union.keys()

Expand All @@ -86,15 +86,15 @@ def to_csv( ll ):

# construct the header of the CSV file
header = ['filename']
for key,count in key_union.iteritems():
for key,count in key_union.items():
header.extend( [key]*count ) # keys can repeat multiple times
tfile.write( to_csv( header ) )

# write the keys values for each file
for filename,kv_dict in data.iteritems():
for filename,kv_dict in data.items():
row = [""]*len(header) # empty row
row[0] = filename
for key,vset, in kv_dict.iteritems():
for key,vset, in kv_dict.items():
n0 = header.index(key) # first occurence of key in header
for i,val in enumerate(vset):
row[n0+i] = val
Expand Down Expand Up @@ -173,7 +173,7 @@ def run_script():
conn.c.waitOnCmd(handle, loops=10, ms=500, failonerror=True,
failontimeout=False, closehandle=False)
print("Deleted existing csv")
except Exception, ex:
except Exception as ex:
print("Failed to delete existing csv: {}".format(ex.message))
else:
print("No exisiting file")
Expand All @@ -191,6 +191,9 @@ def run_script():
print(mess)
mess="done"
client.setOutput("Message", rstring(mess))

except:
pass

finally:
client.closeSession()
Expand Down
Expand Up @@ -82,7 +82,7 @@ def remove_MapAnnotations(conn, dtype, Id ):
conn.c.waitOnCmd(handle, loops=10, ms=500, failonerror=True,
failontimeout=False, closehandle=False)

except Exception, ex:
except Exception as ex:
print("Failed to delete links: {}".format(ex.message))
return

Expand Down Expand Up @@ -162,7 +162,7 @@ def populate_metadata(client, conn, script_params):
existing_kv = get_existing_MapAnnotions( img )
updated_kv = copy.deepcopy(existing_kv)
print("Existing kv ")
for k,vset in existing_kv.iteritems():
for k,vset in existing_kv.items():
print(type(vset),len(vset))
for v in vset:
print(k,v)
Expand All @@ -187,7 +187,7 @@ def populate_metadata(client, conn, script_params):
map_ann.setNs(namespace)
# convert the ordered dict to a list of lists
kv_list=[]
for k,vset in updated_kv.iteritems():
for k,vset in updated_kv.items():
for v in vset:
kv_list.append( [k,v] )
map_ann.setValue(kv_list)
Expand Down Expand Up @@ -234,12 +234,14 @@ def run_script():

# wrap client to use the Blitz Gateway
conn = BlitzGateway(client_obj=client)
message="here I am"
print "scaript params"
for k,v in script_params.iteritems():
print k,v
print("script params")
for k,v in script_params.items():
print(k,v)
message = populate_metadata(client, conn, script_params)
client.setOutput("Message", rstring(message))

except:
pass

finally:
client.closeSession()
Expand Down
Expand Up @@ -27,8 +27,6 @@
@since 5.3.3
"""
from __future__ import print_function


from omero.gateway import BlitzGateway
import omero
Expand All @@ -55,7 +53,7 @@ def RemoveMapAnnotations(conn, dtype, Id ):
conn.c.waitOnCmd(handle, loops=10, ms=500, failonerror=True,
failontimeout=False, closehandle=False)
return 0
except Exception, ex:
except Exception as ex:
print("Failed to delete links: {} ".format(ex.message) )
return 1
return
Expand Down Expand Up @@ -163,6 +161,9 @@ def getObjects(conn, scriptParams):
nobjs = len(objs)
message = "Key value data deleted from {} of {} files".format( nobjs-nfailed, nobjs)
client.setOutput("Message", rstring(message))

except:
pass

finally:
client.closeSession()
Expand Up @@ -28,7 +28,6 @@
@since 5.3
"""
from __future__ import print_function

import sys, os
import re
Expand Down Expand Up @@ -70,7 +69,7 @@ def RemoveMapAnnotations(conn, dtype, Id ):
conn.c.waitOnCmd(handle, loops=10, ms=500, failonerror=True,
failontimeout=False, closehandle=False)

except Exception, ex:
except Exception as ex:
print("Failed to delete links: {}".format(ex.message))
return

Expand Down Expand Up @@ -103,7 +102,7 @@ def AddMapAnnotations(conn, dtype, Id ):

for line in description:
# 1. See if this is a mode string
for key,value in modes.iteritems():
for key,value in modes.items():
match = re.search( "^#\s+{}".format(key),line.lower())
if( match is not None ):
mode = value
Expand Down Expand Up @@ -140,7 +139,7 @@ def AddMapAnnotations(conn, dtype, Id ):
file_keys[i] = match.group(2)

print("Global k-v's")
for k,v in global_kv.iteritems():
for k,v in global_kv.items():
print( k,v)

# convert the template to a regexp
Expand All @@ -163,7 +162,7 @@ def AddMapAnnotations(conn, dtype, Id ):
map_ann = omero.gateway.MapAnnotationWrapper(conn)
namespace = omero.constants.metadata.NSCLIENTMAPANNOTATION
map_ann.setNs(namespace)
map_ann.setValue( [ [k,v] for k,v in global_kv.iteritems() ] )
map_ann.setValue( [ [k,v] for k,v in global_kv.items() ] )
map_ann.save()
dataset.linkAnnotation(map_ann)

Expand All @@ -187,10 +186,6 @@ def AddMapAnnotations(conn, dtype, Id ):
filename = path+"/"+name
match = regexp.search(filename)

# extract the keys
#for i,key in file_keys.iteritems():
# val = match.group(int(i))
# updated_kv[key] = val
if( match is not None ):
for i,val in enumerate(match.groups()):
i1 = i+1
Expand All @@ -199,10 +194,10 @@ def AddMapAnnotations(conn, dtype, Id ):
updated_kv[key] = val

print("existing_kv")
for k,v in existing_kv.iteritems():
for k,v in existing_kv.items():
print(" {} : {}".format(k,v))
print("updated_kv")
for k,v in updated_kv.iteritems():
for k,v in updated_kv.items():
print(" {} : {}".format(k,v))
print("Are they the same?",existing_kv == updated_kv )

Expand All @@ -214,7 +209,7 @@ def AddMapAnnotations(conn, dtype, Id ):
namespace = omero.constants.metadata.NSCLIENTMAPANNOTATION
map_ann.setNs(namespace)
# convert the ordered dict to a list of lists
map_ann.setValue([ [k,v] for k,v in updated_kv.iteritems() ] )
map_ann.setValue([ [k,v] for k,v in updated_kv.items() ] )
map_ann.save()
image.linkAnnotation(map_ann)

Expand Down

0 comments on commit 92778a1

Please sign in to comment.