@@ -342,10 +342,9 @@ def from_file(cls, folder, filename="", ref_only=False, verbose=True):
342342 try :labels_comments = c ['labels_comments' ]
343343 except :labels_comments = None
344344
345- try :labels_sources = c ['labels_sources' ]
345+ try :labels_sources = c ['labels_sources' ];
346346 except :labels_sources = None
347347
348-
349348 # cache in the pickled version
350349 #f = open(folder+pickle_filename,"wb")
351350 #pickle.dump({"coordZYX":coordZYX,"nInVolume":nInVolume,"zOfFrame":zOfFrame,"properties":properties},f)
@@ -536,6 +535,31 @@ def add_points(self, points, vol=0):
536535 self .labels [vol ].append ("" )
537536 self .labels_confidences [vol ].append (- 1.0 )
538537 self .labels_comments [vol ].append ("" )
538+
539+ def add_points2 (self , points , vol = 0 ):
540+ '''Append points to brain.'''
541+
542+ if len (points .shape )== 1 :
543+ points = np .array ([points ])
544+
545+ n_points = points .shape [0 ]
546+ last_p_in_old_vol = np .sum (self .nInVolume [:vol + 1 ])
547+ self .nInVolume [vol ] += n_points
548+
549+ self .coord = np .insert (self .coord ,last_p_in_old_vol ,points ,axis = 0 )
550+ self .coord = irrarray (self .coord , [self .nInVolume ], strideNames = ["vol" ])
551+
552+ if self .curvature .shape [0 ]> 1 and len (self .curvature .shape )> 1 :
553+ c_elements = self .curvature .shape [1 ]
554+ self .curvature = np .insert (self .curvature ,last_p_in_old_vol ,
555+ np .ones ((n_points ,c_elements )),axis = 0 )
556+ self .curvature = irrarray (self .curvature ,self .nInVolume ,
557+ strideNames = ["vol" ])
558+
559+ for i in np .arange (points .shape [0 ]):
560+ self .labels [vol ].append ("" )
561+ self .labels_confidences [vol ].append (- 1.0 )
562+ self .labels_comments [vol ].append ("" )
539563
540564
541565 def delete_points (self , indices , vol = 0 ):
@@ -547,6 +571,9 @@ def delete_points(self, indices, vol=0):
547571
548572 self .coord = np .delete (self .coord ,indices ,axis = 0 )
549573 self .nInVolume [vol ] -= indices .shape [0 ]
574+ self .labels [vol ] = [self .labels [vol ][i ] for i in np .arange (len (self .labels [vol ])) if i not in indices ]
575+ self .labels_confidences = [self .labels_confidences [vol ][i ] for i in np .arange (len (self .labels_confidences [vol ])) if i not in indices ]
576+ self .labels_comments = [self .labels_comments [vol ][i ] for i in np .arange (len (self .labels_comments [vol ])) if i not in indices ]
550577
551578 def __getitem__ (self , i ):
552579 '''
@@ -745,30 +772,38 @@ def set_labels(self, vol, labels, confidences=None):
745772 if confidences is not None :
746773 self .labels_confidences [vol ] = confidences
747774
748- def get_labels (self , vol , attr = True , return_confidences = False , lookup_source = True ):
775+ def get_labels (self , vol , attr = True , return_confidences = False ,
776+ lookup_source = True ):
777+
749778 if len (self .labels [vol ])< self .nInVolume [vol ]:
750779 for i in np .arange (self .nInVolume [vol ]- len (self .labels [vol ])):
751780 self .labels [vol ].append ("" )
752781 self .labels_confidences [vol ].append (- 1.0 )
753782
754783 labs = self .labels [vol ]
784+ conf = self .labels_confidences [vol ]
755785
756786 # If a source for the labels of this volume is indicated, then use
757787 # these labels as indices to get the labels from the source brain
758788 # object.
759789 if self .labels_sources [vol ] is not None and lookup_source :
760790 source_brain = Brains .from_file (self .labels_sources [vol ])
761791 source_brain_labels = source_brain .get_labels (0 )
792+ source_brain_conf = source_brain .labels_confidences [0 ]
762793
763794 labs = labs .copy ()
795+ conf = np .copy (conf )
764796 for pq in np .arange (len (labs )):
765797 lab_s = labs [pq ]
766798 try :
767799 lab_i_tmp = int (lab_s )
768800 lab_tmp = source_brain_labels [lab_i_tmp ]
801+ conf_tmp = source_brain_conf [lab_i_tmp ]
769802 except :
770803 lab_tmp = lab_s
804+ conf_tmp = conf [pq ]
771805 labs [pq ] = lab_tmp
806+ conf [pq ] = conf_tmp
772807
773808 '''
774809 labs_i = [int(lab_s) if lab_s!= "" else -1 for lab_s in labs]
@@ -787,9 +822,14 @@ def get_labels(self, vol, attr=True, return_confidences=False, lookup_source=Tru
787822
788823 if not attr :
789824 labs = [a .split ("_" )[0 ].split ("*" )[0 ].upper () for a in labs ]
825+
826+ #if index_for_unlabeled:
827+ # prefix = "mc" if lookup_source else ""
828+ # for q in np.arange(len(labs)):
829+ # if labs[q]=="": labs[q]=prefix+str(q)
790830
791831 if return_confidences :
792- return labs , self . labels_confidences [ vol ]
832+ return labs , conf
793833 else :
794834 return labs
795835
@@ -976,7 +1016,9 @@ def getOverlay(self, vol, folder="", returnLabels=False):
9761016 else :
9771017 return Overlay
9781018
979- def get_overlay2 (self ,vol ,return_labels = False ,label_size = None ,copy = True ,scale = 1 ,indices_as_labels = False ,index_for_unlabeled = True ,** kwargs ):
1019+ def get_overlay2 (self ,vol ,return_labels = False ,label_size = None ,copy = True ,
1020+ scale = 1 ,indices_as_labels = False ,index_for_unlabeled = True ,
1021+ ** kwargs ):
9801022 if copy :
9811023 overlay_ = self .coord (vol = vol ).copy ()
9821024 else :
@@ -997,6 +1039,13 @@ def get_overlay2(self,vol,return_labels=False,label_size=None,copy=True,scale=1,
9971039 for ll in np .arange (len (labs__ )):
9981040 if labs__ [ll ]!= '' or not index_for_unlabeled :
9991041 labels_ [ll ] = labs__ [ll ]
1042+
1043+ if indices_as_labels and not index_for_unlabeled :
1044+ # Replace indices with blanks, where present
1045+ labs__ = self .get_labels (vol ,** kwargs )
1046+ for ll in np .arange (len (labs__ )):
1047+ if labs__ [ll ] in [" " ]:
1048+ labels_ [ll ] = ""
10001049
10011050 if label_size is not None :
10021051 labels_ = [label_ .ljust (label_size ) for label_ in labels_ ]
0 commit comments