def getAnnotation ( self, ch, annid ): """Return a RAMON object by identifier""" kvdict = self.annodb.getAnnotationKV ( ch, annid ) annotype = int(kvdict['ann_type']) # switch on the type of annotation if annotype is None: return None elif annotype == annotation.ANNO_SYNAPSE: anno = annotation.AnnSynapse(self.annodb,ch) elif annotype == annotation.ANNO_SEED: anno = annotation.AnnSeed(self.annodb,ch) elif annotype == annotation.ANNO_SEGMENT: anno = annotation.AnnSegment(self.annodb,ch) elif annotype == annotation.ANNO_NEURON: anno = annotation.AnnNeuron(self.annodb,ch) elif annotype == annotation.ANNO_ORGANELLE: anno = annotation.AnnOrganelle(self.annodb,ch) elif annotype == annotation.ANNO_NODE: anno = annotation.AnnNode(self.annodb,ch) elif annotype == annotation.ANNO_SKELETON: anno = annotation.AnnSkeleton(self.annodb,ch) elif annotype == annotation.ANNO_ROI: anno = annotation.AnnROI(self.annodb,ch) elif annotype == annotation.ANNO_ANNOTATION: anno = annotation.Annotation(self.annodb,ch) else: raise NDWSError ( "Unrecognized annotation type {}".format(type) ) # load the annotation anno.fromDict ( kvdict ) return anno
def _readSynapse(self, id): # create a new synapse anno = annotation.AnnSynapse(self.annodb, self.ch) # set the synapse ID anno.setField('annid', id) # fill in the fields # basic metadata first [confidence, status] = self._readAnnoMetadata(id) anno.setField('status', status) anno.setField('confidence', confidence) # synapse related metadata with closing( MySQLdb.connect( host=self.proj.getDBHost(), user=settings.DATABASES['default']['USER'], passwd=settings.DATABASES['default']['PASSWORD'], db=self.proj.getDBName(), connect_timeout=1)) as conn: with closing(conn.cursor()) as cursor: synapseTableName = "{}_synapses".format(self.ch.channel_name) sql = "SELECT synapse_type, weight FROM {} WHERE annoid={}".format( synapseTableName, id) try: cursor.execute(sql) res = cursor.fetchall()[0] except MySQLdb.Error, e: print "Error: Failed to get annotation type for RAMON object with ID {}: {}".format( id, e) sys.exit(1)
def addSynapse(self, newid, voxels): """Send the specific synapse to the database""" # Define synapse metadata syn = annotation.AnnSynapse() syn.annid = newid syn.author = self.seg.author syn.kvpairs = self.seg.kvpairs print "Writing synapse ", syn.annid # Put the RAMON object self.annodb.putAnnotation(syn) # Rewrite the voxels self.annodb.annotate(newid, self._resolution, voxels)
def H5toAnnotation(key, idgrp, annodb, ch): """Return an annotation constructed from the contents of this HDF5 file""" # get the annotation type # if idgrp.get('ANNOTATION_TYPE'): if 'ANNOTATION_TYPE' in idgrp: annotype = idgrp['ANNOTATION_TYPE'][0] else: annotype = annotation.ANNO_ANNOTATION # And get the metadata group mdgrp = idgrp.get('METADATA') if annotype == annotation.ANNO_SEED: # Create the appropriate annotation type anno = annotation.AnnSeed(annodb, ch) # Load metadata if it exists if mdgrp: # load the seed specific metadata if 'PARENT' in mdgrp: anno.parent = mdgrp['PARENT'][0] if 'POSITION' in mdgrp: anno.position = mdgrp['POSITION'][:] if 'CUBE_LOCATION' in mdgrp: anno.cubelocation = mdgrp['CUBE_LOCATION'][0] if 'SOURCE' in mdgrp: anno.source = mdgrp['SOURCE'][0] elif annotype == annotation.ANNO_SYNAPSE: # Create the appropriate annotation type anno = annotation.AnnSynapse(annodb, ch) # Load metadata if it exists if mdgrp: # load the synapse specific metadata if 'SYNAPSE_TYPE' in mdgrp: anno.synapse_type = mdgrp['SYNAPSE_TYPE'][0] if 'WEIGHT' in mdgrp: anno.weight = mdgrp['WEIGHT'][0] if 'SEEDS' in mdgrp: anno.seeds = mdgrp['SEEDS'][:] if 'SEGMENTS' in mdgrp: anno.segments = mdgrp['SEGMENTS'][:] if 'PRESEGMENTS' in mdgrp: anno.presegments = mdgrp['PRESEGMENTS'][:] if 'POSTSEGMENTS' in mdgrp: anno.postsegments = mdgrp['POSTSEGMENTS'][:] elif annotype == annotation.ANNO_SEGMENT: # Create the appropriate annotation type anno = annotation.AnnSegment(annodb, ch) # Load metadata if it exists if mdgrp: # load the synapse specific metadata if mdgrp.get('PARENTSEED'): anno.parentseed = mdgrp['PARENTSEED'][0] if mdgrp.get('SEGMENTCLASS'): anno.segmentclass = mdgrp['SEGMENTCLASS'][0] if mdgrp.get('NEURON'): anno.neuron = mdgrp['NEURON'][0] if mdgrp.get('SYNAPSES') and len(mdgrp['SYNAPSES']) != 0: anno.synapses = mdgrp['SYNAPSES'][:] if mdgrp.get('ORGANELLES') and len(mdgrp['ORGANELLES']) != 0: anno.organelles = mdgrp['ORGANELLES'][:] elif annotype == annotation.ANNO_NEURON: # Create the appropriate annotation type anno = annotation.AnnNeuron(annodb, ch) # Load metadata if it exists if mdgrp: # load the synapse specific metadata if mdgrp.get('SEGMENTS') and len(mdgrp['SEGMENTS']) != 0: anno.segments = mdgrp['SEGMENTS'][:] elif annotype == annotation.ANNO_ORGANELLE: # Create the appropriate annotation type anno = annotation.AnnOrganelle(annodb, ch) # Load metadata if it exists if mdgrp: # load the synapse specific metadata if mdgrp.get('PARENTSEED'): anno.parentseed = mdgrp['PARENTSEED'][0] if mdgrp.get('ORGANELLECLASS'): anno.organelleclass = mdgrp['ORGANELLECLASS'][0] if mdgrp.get('SEEDS') and len(mdgrp['SEEDS']) != 0: anno.seeds = mdgrp['SEEDS'][:] if mdgrp.get('CENTROID'): anno.centroid = mdgrp['CENTROID'][:] elif annotype == annotation.ANNO_NODE: # Create the appropriate annotation type anno = annotation.AnnNode(annodb, ch) # Load metadata if it exists if mdgrp: # load the synapse specific metadata if 'NODETYPE' in mdgrp: anno.nodetype = mdgrp['NODETYPE'][0] if 'PARENTID' in mdgrp: anno.parentid = mdgrp['PARENTID'][0] if 'SKELETONID' in mdgrp: anno.skeletonid = mdgrp['SKELETONID'][0] if 'RADIUS' in mdgrp: anno.radius = mdgrp['RADIUS'][0] if mdgrp.get('CHILDREN') and len(mdgrp['CHILDREN']) != 0: anno.children = mdgrp['CHILDREN'][:] if mdgrp.get('LOCATION'): anno.location = mdgrp['LOCATION'][:] elif annotype == annotation.ANNO_SKELETON: # Create the appropriate annotation type anno = annotation.AnnSkeleton(annodb, ch) # Load metadata if it exists if mdgrp: # load the synapse specific metadata if 'SKELETONTYPE' in mdgrp: anno.skeletontype = mdgrp['SKELETONTYPE'][0] if 'ROOTNODE' in mdgrp: anno.rootnode = mdgrp['ROOTNODE'][0] elif annotype == annotation.ANNO_ROI: # Create the appropriate annotation type anno = annotation.AnnROI(annodb, ch) # Load metadata if it exists if mdgrp: # load the synapse specific metadata if 'PARENT' in mdgrp: anno.parent = mdgrp['PARENT'][0] # No special action if it's a no type elif annotype == annotation.ANNO_ANNOTATION: # Just create a generic annotation object anno = annotation.Annotation(annodb, ch) else: logger.warning("Do not support this annotation type yet. Type = %s" % annotype) raise NDWSError("Do n0t support this annotation type yet. Type = %s" % annotype) # now load the annotation common fields if re.match("^\d+$", key): anno.annid = int(key) else: anno.annid = 0 if mdgrp: # now load the metadata common fields if mdgrp.get('STATUS'): anno.status = mdgrp['STATUS'][0] if mdgrp.get('CONFIDENCE'): anno.confidence = mdgrp['CONFIDENCE'][0] if mdgrp.get('AUTHOR'): anno.author = mdgrp['AUTHOR'][0] # and the key/value pairs if mdgrp.get('KVPAIRS'): fstring = cStringIO.StringIO(mdgrp['KVPAIRS'][0]) csvr = csv.reader(fstring, delimiter=',') for r in csvr: anno.kvpairs[r[0]] = r[1] return anno