Ejemplo n.º 1
0
    def _setLabel2Color(self, successive_ids=True, export_mode=False):
        if not self.EventsVector.ready() or not self.Parameters.ready() \
                or not self.FilteredLabels.ready():
            return

        if export_mode:
            assert successive_ids, "Export mode only works for successive ids"

        events = self.EventsVector.value
        parameters = self.Parameters.value
        time_min, time_max = parameters['time_range']
        time_range = range(time_min, time_max)

        filtered_labels = self.FilteredLabels.value

        label2color = []
        label2color.append({})
        mergers = []

        maxId = 2  # misdetections have id 1

        # handle start time offsets
        for i in range(time_range[0]):
            label2color.append({})
            mergers.append({})

        if export_mode:
            extra_track_ids = {}
            multi_move = {}
            multi_move_next = {}
            divisions = []

        for i in time_range:
            dis = get_dict_value(events[str(i - time_range[0] + 1)], "dis", [])
            app = get_dict_value(events[str(i - time_range[0] + 1)], "app", [])
            div = get_dict_value(events[str(i - time_range[0] + 1)], "div", [])
            mov = get_dict_value(events[str(i - time_range[0] + 1)], "mov", [])
            merger = get_dict_value(events[str(i - time_range[0])], "merger", [])
            multi = get_dict_value(events[str(i - time_range[0] + 1)], "multiMove", [])

            logger.info(" {} dis at {}".format(len(dis), i))
            logger.info(" {} app at {}".format(len(app), i))
            logger.info(" {} div at {}".format(len(div), i))
            logger.info(" {} mov at {}".format(len(mov), i))
            logger.info(" {} merger at {}".format(len(merger), i))
            logger.info(" {} multiMoves at {}\n".format(len(multi), i))

            label2color.append({})
            mergers.append({})
            moves_at = []

            if export_mode:
                moves_to = {}

            for e in app:
                if successive_ids:
                    label2color[-1][int(e[0])] = maxId
                    maxId += 1
                else:
                    label2color[-1][int(e[0])] = np.random.randint(1, 255)

            for e in mov:
                if export_mode:
                    if e[1] in moves_to:
                        multi_move.setdefault(i, {})
                        multi_move[i][e[0]] = e[1]
                        multi_move[i][moves_to[e[1]][0]] = e[1]
                        multi_move_next[(i, e[1])] = 0
                    moves_to.setdefault(e[1], [])
                    moves_to[e[1]].append(e[0])

                if not label2color[-2].has_key(int(e[0])):
                    if successive_ids:
                        label2color[-2][int(e[0])] = maxId
                        maxId += 1
                    else:
                        label2color[-2][int(e[0])] = np.random.randint(1, 255)
                label2color[-1][int(e[1])] = label2color[-2][int(e[0])]
                moves_at.append(int(e[0]))

                if export_mode:
                    key = i - 1, e[0]
                    if key in multi_move_next:
                        multi_move_next[key] = e[1]
                        multi_move_next[(i, e[1])] = 0

            for e in div:  # event(parent, child, child)
                # if not label2color[-2].has_key(int(e[0])):
                if not int(e[0]) in label2color[-2]:
                    if successive_ids:
                        label2color[-2][int(e[0])] = maxId
                        maxId += 1
                    else:
                        label2color[-2][int(e[0])] = np.random.randint(1, 255)
                ancestor_color = label2color[-2][int(e[0])]
                if export_mode:
                    label2color[-1][int(e[1])] = maxId
                    label2color[-1][int(e[2])] = maxId + 1
                    divisions.append((i, int(e[0]), ancestor_color,
                                      int(e[1]), maxId,
                                      int(e[2]), maxId + 1
                    ))
                    maxId += 2
                else:
                    label2color[-1][int(e[1])] = ancestor_color
                    label2color[-1][int(e[2])] = ancestor_color

            for e in merger:
                mergers[-1][int(e[0])] = int(e[1])

            for e in multi:
                if int(e[2]) >= 0 and not label2color[time_range[0] + int(e[2])].has_key(int(e[0])):
                    if successive_ids:
                        label2color[time_range[0] + int(e[2])][int(e[0])] = maxId
                        maxId += 1
                    else:
                        label2color[time_range[0] + int(e[2])][int(e[0])] = np.random.randint(1, 255)
                label2color[-1][int(e[1])] = label2color[time_range[0] + int(e[2])][int(e[0])]
                if export_mode:
                    e_start = int(time_range[0] + e[2])
                    e_end = int(i)
                    track_id = label2color[time_range[0] + int(e[2])][int(e[0])]
                    double_object = multi_move[e_start][e[0]]
                    for t in xrange(e_start, e_end):
                        extra_track_ids.setdefault(t + 1, {})
                        extra_track_ids[t + 1].setdefault(double_object, [])
                        extra_track_ids[t + 1][double_object].append(track_id)
                        next_key = (t, double_object)
                        double_object = multi_move_next[next_key]


        # last timestep
        merger = get_dict_value(events[str(time_range[-1] - time_range[0] + 1)], "merger", [])
        mergers.append({})
        for e in merger:
            mergers[-1][int(e[0])] = int(e[1])


        # mark the filtered objects
        for i in filtered_labels.keys():
            if int(i) + time_range[0] >= len(label2color):
                continue
            fl_at = filtered_labels[i]
            for l in fl_at:
                assert l not in label2color[int(i) + time_range[0]]
                label2color[int(i) + time_range[0]][l] = 0

        if export_mode:  # don't set fields when in export_mode
            self.track_id = label2color
            self.extra_track_ids = extra_track_ids
            self.divisions = divisions
            return label2color, extra_track_ids, divisions

        self.label2color = label2color
        self.mergers = mergers

        self.Output._value = None
        self.Output.setDirty(slice(None))

        if 'MergerOutput' in self.outputs:
            self.MergerOutput._value = None
            self.MergerOutput.setDirty(slice(None))
Ejemplo n.º 2
0
    def _setLabel2Color(self, successive_ids=True):
        if not self.EventsVector.ready() or not self.Parameters.ready() \
            or not self.FilteredLabels.ready():            
            return
        
        events = self.EventsVector.value
        parameters = self.Parameters.value
        time_min, time_max = parameters['time_range']
        time_range = range(time_min, time_max)
        
#         x_range = parameters['x_range']
#         y_range = parameters['y_range']
#         z_range = parameters['z_range']
#         
        filtered_labels = self.FilteredLabels.value
                                                    
        label2color = []
        label2color.append({})
        mergers = []
        mergers.append({})
        
        maxId = 2 #  misdetections have id 1
        
        # handle start time offsets
        for i in range(time_range[0]):            
            label2color.append({})
            mergers.append({})
        
        for i in time_range:
            dis = get_dict_value(events[str(i-time_range[0]+1)], "dis", [])            
            app = get_dict_value(events[str(i-time_range[0]+1)], "app", [])
            div = get_dict_value(events[str(i-time_range[0]+1)], "div", [])
            mov = get_dict_value(events[str(i-time_range[0]+1)], "mov", [])
            merger = get_dict_value(events[str(i-time_range[0]+1)], "merger", [])
            
            logger.info( " {} dis at {}".format( len(dis), i ) )
            logger.info( " {} app at {}".format( len(app), i ) )
            logger.info( " {} div at {}".format( len(div), i ) )
            logger.info( " {} mov at {}".format( len(mov), i ) )
            logger.info( " {} merger at {}\n".format( len(merger), i ) )
            
            label2color.append({})
            mergers.append({})
                        
            for e in app:
                if successive_ids:
                    label2color[-1][int(e[0])] = maxId
                    maxId += 1
                else:
                    label2color[-1][int(e[0])] = np.random.randint(1, 255)

            for e in mov:                
                if not label2color[-2].has_key(int(e[0])):
                    if successive_ids:
                        label2color[-2][int(e[0])] = maxId
                        maxId += 1
                    else:
                        label2color[-2][int(e[0])] = np.random.randint(1, 255)
                label2color[-1][int(e[1])] = label2color[-2][int(e[0])]

            for e in div:
                if not label2color[-2].has_key(int(e[0])):
                    if successive_ids:
                        label2color[-2][int(e[0])] = maxId
                        maxId += 1
                    else:
                        label2color[-2][int(e[0])] = np.random.randint(1, 255)
                ancestor_color = label2color[-2][int(e[0])]
                label2color[-1][int(e[1])] = ancestor_color
                label2color[-1][int(e[2])] = ancestor_color
            
            for e in merger:
                mergers[-1][int(e[0])] = int(e[1])
                
        # mark the filtered objects
        for i in filtered_labels.keys():
            if int(i)+time_range[0] >= len(label2color):
                continue
            fl_at = filtered_labels[i]
            for l in fl_at:
                assert l not in label2color[int(i)+time_range[0]]
                label2color[int(i)+time_range[0]][l] = 0                

        self.label2color = label2color
        self.mergers = mergers        
        
        self.Output._value = None
        self.Output.setDirty(slice(None))
Ejemplo n.º 3
0
    def _setLabel2Color(self, successive_ids=True, export_mode=False):
        if not self.EventsVector.ready() or not self.Parameters.ready() \
                or not self.FilteredLabels.ready():
            return

        if export_mode:
            assert successive_ids, "Export mode only works for successive ids"

        events = self.EventsVector.value
        parameters = self.Parameters.value
        time_min, time_max = parameters['time_range']
        time_range = range(time_min, time_max)

        filtered_labels = self.FilteredLabels.value

        label2color = []
        label2color.append({})
        mergers = []
        resolvedto = []

        maxId = 2  # misdetections have id 1

        # handle start time offsets
        for i in range(time_range[0]):
            label2color.append({})
            mergers.append({})
            resolvedto.append({})

        if export_mode:
            extra_track_ids = {}
            multi_move = {}
            multi_move_next = {}
            divisions = []

        for i in time_range:
            dis = get_dict_value(events[str(i - time_range[0] + 1)], "dis", [])
            app = get_dict_value(events[str(i - time_range[0] + 1)], "app", [])
            div = get_dict_value(events[str(i - time_range[0] + 1)], "div", [])
            mov = get_dict_value(events[str(i - time_range[0] + 1)], "mov", [])
            merger = get_dict_value(events[str(i - time_range[0])], "merger", [])
            res = get_dict_value(events[str(i - time_range[0])], "res", {})

            logger.debug(" {} dis at {}".format(len(dis), i))
            logger.debug(" {} app at {}".format(len(app), i))
            logger.debug(" {} div at {}".format(len(div), i))
            logger.debug(" {} mov at {}".format(len(mov), i))
            logger.debug(" {} merger at {}".format(len(merger), i))
            logger.debug(" {} res at {}".format(len(res), i))

            label2color.append({})
            mergers.append({})
            moves_at = []
            resolvedto.append({})

            if export_mode:
                moves_to = {}

            for e in app:
                if successive_ids:
                    label2color[-1][int(e[0])] = maxId  # in export mode, the label color is used as track ID
                    maxId += 1
                else:
                    label2color[-1][int(e[0])] = np.random.randint(1, 255)

            for e in mov:
                if export_mode:
                    if e[1] in moves_to:
                        multi_move.setdefault(i, {})
                        multi_move[i][e[0]] = e[1]
                        if len(moves_to[e[1]]) == 1:  # if we are just setting up this multi move
                            multi_move[i][moves_to[e[1]][0]] = e[1]
                        multi_move_next[(i, e[1])] = 0
                    moves_to.setdefault(e[1], [])
                    moves_to[e[1]].append(e[0])  # moves_to[target] contains list of incoming object ids

                # alternative way of appearance
                if not label2color[-2].has_key(int(e[0])):
                    if successive_ids:
                        label2color[-2][int(e[0])] = maxId
                        maxId += 1
                    else:
                        label2color[-2][int(e[0])] = np.random.randint(1, 255)

                # assign color of parent
                label2color[-1][int(e[1])] = label2color[-2][int(e[0])]
                moves_at.append(int(e[0]))

                if export_mode:
                    key = i - 1, e[0]
                    if key in multi_move_next:  # captures mergers staying connected over longer time spans
                        multi_move_next[key] = e[1]  # redirects output of last merger to target in this frame
                        multi_move_next[(i, e[1])] = 0  # sets current end to zero (might be changed by above line in the future)

            for e in div:  # event(parent, child, child)
                # if not label2color[-2].has_key(int(e[0])):
                if not int(e[0]) in label2color[-2]:
                    if successive_ids:
                        label2color[-2][int(e[0])] = maxId
                        maxId += 1
                    else:
                        label2color[-2][int(e[0])] = np.random.randint(1, 255)
                ancestor_color = label2color[-2][int(e[0])]
                if export_mode:
                    label2color[-1][int(e[1])] = maxId
                    label2color[-1][int(e[2])] = maxId + 1
                    divisions.append((i, int(e[0]), ancestor_color,
                                      int(e[1]), maxId,
                                      int(e[2]), maxId + 1
                    ))
                    maxId += 2
                else:
                    label2color[-1][int(e[1])] = ancestor_color
                    label2color[-1][int(e[2])] = ancestor_color

            for e in merger:
                mergers[-1][int(e[0])] = int(e[1])

            for o, r in res.iteritems():
                resolvedto[-1][int(o)] = [int(c) for c in r[:-1]]
                # label the original object with the false detection label
                mergers[-1][int(o)] = len(r[:-1])

                if export_mode:
                    extra_track_ids.setdefault(i, {})
                    extra_track_ids[i][int(o)] = [int(c) for c in r[:-1]]

        # last timestep
        merger = get_dict_value(events[str(time_range[-1] - time_range[0] + 1)], "merger", [])
        mergers.append({})
        for e in merger:
            mergers[-1][int(e[0])] = int(e[1])

        res = get_dict_value(events[str(time_range[-1] - time_range[0] + 1)], "res", {})
        resolvedto.append({})
        if export_mode:
            extra_track_ids[time_range[-1] + 1] = {}
        for o, r in res.iteritems():
            resolvedto[-1][int(o)] = [int(c) for c in r[:-1]]
            mergers[-1][int(o)] = len(r[:-1])

            if export_mode:
                    extra_track_ids[time_range[-1] + 1][int(o)] = [int(c) for c in r[:-1]]

        # mark the filtered objects
        for i in filtered_labels.keys():
            if int(i) + time_range[0] >= len(label2color):
                continue
            fl_at = filtered_labels[i]
            for l in fl_at:
                assert l not in label2color[int(i) + time_range[0]]
                label2color[int(i) + time_range[0]][l] = 0

        if export_mode:  # don't set fields when in export_mode
            self.track_id = label2color
            self.divisions = divisions
            self.extra_track_ids = extra_track_ids
            return label2color, extra_track_ids, divisions

        self.label2color = label2color
        self.resolvedto = resolvedto
        self.mergers = mergers

        self.Output._value = None
        self.Output.setDirty(slice(None))

        if 'MergerOutput' in self.outputs:
            self.MergerOutput._value = None
            self.MergerOutput.setDirty(slice(None))
Ejemplo n.º 4
0
    def _setLabel2Color(self, successive_ids=True):
        if not self.EventsVector.ready() or not self.Parameters.ready() \
            or not self.FilteredLabels.ready():
            return

        events = self.EventsVector.value
        parameters = self.Parameters.value
        time_min, time_max = parameters['time_range']
        time_range = range(time_min, time_max)

        filtered_labels = self.FilteredLabels.value

        label2color = []
        label2color.append({})
        mergers = []

        maxId = 2  #  misdetections have id 1

        # handle start time offsets
        for i in range(time_range[0]):
            label2color.append({})
            mergers.append({})

        for i in time_range:
            dis = get_dict_value(events[str(i - time_range[0] + 1)], "dis", [])
            app = get_dict_value(events[str(i - time_range[0] + 1)], "app", [])
            div = get_dict_value(events[str(i - time_range[0] + 1)], "div", [])
            mov = get_dict_value(events[str(i - time_range[0] + 1)], "mov", [])
            merger = get_dict_value(events[str(i - time_range[0])], "merger",
                                    [])
            multi = get_dict_value(events[str(i - time_range[0] + 1)],
                                   "multiMove", [])

            logger.info(" {} dis at {}".format(len(dis), i))
            logger.info(" {} app at {}".format(len(app), i))
            logger.info(" {} div at {}".format(len(div), i))
            logger.info(" {} mov at {}".format(len(mov), i))
            logger.info(" {} merger at {}".format(len(merger), i))
            logger.info(" {} multiMoves at {}\n".format(len(multi), i))

            label2color.append({})
            mergers.append({})
            moves_at = []

            for e in app:
                if successive_ids:
                    label2color[-1][int(e[0])] = maxId
                    maxId += 1
                else:
                    label2color[-1][int(e[0])] = np.random.randint(1, 255)

            for e in mov:
                if not label2color[-2].has_key(int(e[0])):
                    if successive_ids:
                        label2color[-2][int(e[0])] = maxId
                        maxId += 1
                    else:
                        label2color[-2][int(e[0])] = np.random.randint(1, 255)
                label2color[-1][int(e[1])] = label2color[-2][int(e[0])]
                moves_at.append(int(e[0]))

            for e in div:
                if not label2color[-2].has_key(int(e[0])):
                    if successive_ids:
                        label2color[-2][int(e[0])] = maxId
                        maxId += 1
                    else:
                        label2color[-2][int(e[0])] = np.random.randint(1, 255)
                ancestor_color = label2color[-2][int(e[0])]
                label2color[-1][int(e[1])] = ancestor_color
                label2color[-1][int(e[2])] = ancestor_color

            for e in merger:
                mergers[-1][int(e[0])] = int(e[1])

            for e in multi:
                if int(e[2]) >= 0 and not label2color[time_range[0] +
                                                      int(e[2])].has_key(
                                                          int(e[0])):
                    if successive_ids:
                        label2color[time_range[0] + int(e[2])][int(
                            e[0])] = maxId
                        maxId += 1
                    else:
                        label2color[time_range[0] + int(e[2])][int(
                            e[0])] = np.random.randint(1, 255)
                label2color[-1][int(e[1])] = label2color[time_range[0] +
                                                         int(e[2])][int(e[0])]

        # last timestep
        merger = get_dict_value(
            events[str(time_range[-1] - time_range[0] + 1)], "merger", [])
        mergers.append({})
        for e in merger:
            mergers[-1][int(e[0])] = int(e[1])

        # mark the filtered objects
        for i in filtered_labels.keys():
            if int(i) + time_range[0] >= len(label2color):
                continue
            fl_at = filtered_labels[i]
            for l in fl_at:
                assert l not in label2color[int(i) + time_range[0]]
                label2color[int(i) + time_range[0]][l] = 0

        self.label2color = label2color
        self.mergers = mergers

        self.Output._value = None
        self.Output.setDirty(slice(None))

        if 'MergerOutput' in self.outputs:
            self.MergerOutput._value = None
            self.MergerOutput.setDirty(slice(None))
Ejemplo n.º 5
0
    def _setLabel2Color(self, successive_ids=True, export_mode=False):
        if not self.EventsVector.ready() or not self.Parameters.ready() \
                or not self.FilteredLabels.ready():
            return

        if export_mode:
            assert successive_ids, "Export mode only works for successive ids"

        events = self.EventsVector.value
        parameters = self.Parameters.value

        time_min = 0
        time_max = self.RawImage.meta.shape[0] - 1  # Assumes t,x,y,z,c
        if 'time_range' in parameters:
            time_min, time_max = parameters['time_range']
        time_range = range(time_min, time_max)

        filtered_labels = self.FilteredLabels.value

        label2color = []
        label2color.append({})
        mergers = []
        resolvedto = []

        maxId = 2  # misdetections have id 1

        # handle start time offsets
        for i in range(time_range[0]):
            label2color.append({})
            mergers.append({})
            resolvedto.append({})

        extra_track_ids = {}
        if export_mode:
            multi_move = {}
            multi_move_next = {}
            divisions = []

        for i in time_range:
            app = get_dict_value(events[str(i - time_range[0] + 1)], "app", [])
            div = get_dict_value(events[str(i - time_range[0] + 1)], "div", [])
            mov = get_dict_value(events[str(i - time_range[0] + 1)], "mov", [])
            merger = get_dict_value(events[str(i - time_range[0])], "merger",
                                    [])
            res = get_dict_value(events[str(i - time_range[0])], "res", {})

            logger.debug(" {} app at {}".format(len(app), i))
            logger.debug(" {} div at {}".format(len(div), i))
            logger.debug(" {} mov at {}".format(len(mov), i))
            logger.debug(" {} merger at {}".format(len(merger), i))

            label2color.append({})
            mergers.append({})
            moves_at = []
            resolvedto.append({})

            if export_mode:
                moves_to = {}

            for e in app:
                if successive_ids:
                    label2color[-1][int(
                        e[0]
                    )] = maxId  # in export mode, the label color is used as track ID
                    maxId += 1
                else:
                    label2color[-1][int(e[0])] = np.random.randint(1, 255)

            for e in mov:
                if export_mode:
                    if e[1] in moves_to:
                        multi_move.setdefault(i, {})
                        multi_move[i][e[0]] = e[1]
                        if len(
                                moves_to[e[1]]
                        ) == 1:  # if we are just setting up this multi move
                            multi_move[i][moves_to[e[1]][0]] = e[1]
                        multi_move_next[(i, e[1])] = 0
                    moves_to.setdefault(e[1], [])
                    moves_to[e[1]].append(
                        e[0]
                    )  # moves_to[target] contains list of incoming object ids

                # alternative way of appearance
                if not label2color[-2].has_key(int(e[0])):
                    if successive_ids:
                        label2color[-2][int(e[0])] = maxId
                        maxId += 1
                    else:
                        label2color[-2][int(e[0])] = np.random.randint(1, 255)

                # assign color of parent
                label2color[-1][int(e[1])] = label2color[-2][int(e[0])]
                moves_at.append(int(e[0]))

                if export_mode:
                    key = i - 1, e[0]
                    if key in multi_move_next:  # captures mergers staying connected over longer time spans
                        multi_move_next[key] = e[
                            1]  # redirects output of last merger to target in this frame
                        multi_move_next[(
                            i, e[1]
                        )] = 0  # sets current end to zero (might be changed by above line in the future)

            for e in div:  # event(parent, child, child)
                # if not label2color[-2].has_key(int(e[0])):
                if not int(e[0]) in label2color[-2]:
                    if successive_ids:
                        label2color[-2][int(e[0])] = maxId
                        maxId += 1
                    else:
                        label2color[-2][int(e[0])] = np.random.randint(1, 255)
                ancestor_color = label2color[-2][int(e[0])]
                if export_mode:
                    label2color[-1][int(e[1])] = maxId
                    label2color[-1][int(e[2])] = maxId + 1
                    divisions.append(
                        (i, int(e[0]), ancestor_color, int(e[1]), maxId,
                         int(e[2]), maxId + 1))
                    maxId += 2
                else:
                    label2color[-1][int(e[1])] = ancestor_color
                    label2color[-1][int(e[2])] = ancestor_color

            for e in merger:
                mergers[-1][int(e[0])] = int(e[1])

            for o, r in res.iteritems():
                resolvedto[-1][int(o)] = [int(c) for c in r[:-1]]
                # label the original object with the false detection label
                mergers[-1][int(o)] = len(r[:-1])

                if export_mode:
                    extra_track_ids.setdefault(i, {})
                    extra_track_ids[i][int(o)] = [int(c) for c in r[:-1]]

        # last timestep
        merger = get_dict_value(
            events[str(time_range[-1] - time_range[0] + 1)], "merger", [])
        mergers.append({})
        for e in merger:
            mergers[-1][int(e[0])] = int(e[1])

        res = get_dict_value(events[str(time_range[-1] - time_range[0] + 1)],
                             "res", {})
        resolvedto.append({})
        if export_mode:
            extra_track_ids[time_range[-1] + 1] = {}
        for o, r in res.iteritems():
            resolvedto[-1][int(o)] = [int(c) for c in r[:-1]]
            mergers[-1][int(o)] = len(r[:-1])

            if export_mode:
                extra_track_ids[time_range[-1] +
                                1][int(o)] = [int(c) for c in r[:-1]]

        # mark the filtered objects
        for i in filtered_labels.keys():
            if int(i) + time_range[0] >= len(label2color):
                continue
            fl_at = filtered_labels[i]
            for l in fl_at:
                assert l not in label2color[int(i) + time_range[0]]
                label2color[int(i) + time_range[0]][l] = 0

        if export_mode:  # don't set fields when in export_mode
            self.track_id = label2color
            self.divisions = divisions
            self.extra_track_ids = extra_track_ids
            return label2color, extra_track_ids, divisions

        self.track_id = label2color
        self.extra_track_ids = extra_track_ids
        self.label2color = label2color
        self.resolvedto = resolvedto
        self.mergers = mergers

        self.Output._value = None
        self.Output.setDirty(slice(None))

        if 'MergerOutput' in self.outputs:
            self.MergerOutput._value = None
            self.MergerOutput.setDirty(slice(None))
Ejemplo n.º 6
0
    def _setLabel2Color(self, successive_ids=True, export_mode=False):
        if not self.EventsVector.ready() or not self.Parameters.ready() \
                or not self.FilteredLabels.ready():
            return

        if export_mode:
            assert successive_ids, "Export mode only works for successive ids"

        events = self.EventsVector.value
        parameters = self.Parameters.value
        time_min, time_max = parameters['time_range']
        time_range = range(time_min, time_max)

        filtered_labels = self.FilteredLabels.value

        label2color = []
        label2color.append({})
        mergers = []

        maxId = 2  # misdetections have id 1

        # handle start time offsets
        for i in range(time_range[0]):
            label2color.append({})
            mergers.append({})

        if export_mode:
            extra_track_ids = {}
            multi_move = {}
            multi_move_next = {}
            divisions = []

        for i in time_range:
            dis = get_dict_value(events[str(i - time_range[0] + 1)], "dis", [])
            app = get_dict_value(events[str(i - time_range[0] + 1)], "app", [])
            div = get_dict_value(events[str(i - time_range[0] + 1)], "div", [])
            mov = get_dict_value(events[str(i - time_range[0] + 1)], "mov", [])
            merger = get_dict_value(events[str(i - time_range[0])], "merger",
                                    [])
            multi = get_dict_value(events[str(i - time_range[0] + 1)],
                                   "multiMove", [])

            logger.info(" {} dis at {}".format(len(dis), i))
            logger.info(" {} app at {}".format(len(app), i))
            logger.info(" {} div at {}".format(len(div), i))
            logger.info(" {} mov at {}".format(len(mov), i))
            logger.info(" {} merger at {}".format(len(merger), i))
            logger.info(" {} multiMoves at {}\n".format(len(multi), i))

            label2color.append({})
            mergers.append({})
            moves_at = []

            if export_mode:
                moves_to = {}

            for e in app:
                if successive_ids:
                    label2color[-1][int(e[0])] = maxId
                    maxId += 1
                else:
                    label2color[-1][int(e[0])] = np.random.randint(1, 255)

            for e in mov:
                if export_mode:
                    if e[1] in moves_to:
                        multi_move.setdefault(i, {})
                        multi_move[i][e[0]] = e[1]
                        multi_move[i][moves_to[e[1]][0]] = e[1]
                        multi_move_next[(i, e[1])] = 0
                    moves_to.setdefault(e[1], [])
                    moves_to[e[1]].append(e[0])

                if not label2color[-2].has_key(int(e[0])):
                    if successive_ids:
                        label2color[-2][int(e[0])] = maxId
                        maxId += 1
                    else:
                        label2color[-2][int(e[0])] = np.random.randint(1, 255)
                label2color[-1][int(e[1])] = label2color[-2][int(e[0])]
                moves_at.append(int(e[0]))

                if export_mode:
                    key = i - 1, e[0]
                    if key in multi_move_next:
                        multi_move_next[key] = e[1]
                        multi_move_next[(i, e[1])] = 0

            for e in div:  # event(parent, child, child)
                # if not label2color[-2].has_key(int(e[0])):
                if not int(e[0]) in label2color[-2]:
                    if successive_ids:
                        label2color[-2][int(e[0])] = maxId
                        maxId += 1
                    else:
                        label2color[-2][int(e[0])] = np.random.randint(1, 255)
                ancestor_color = label2color[-2][int(e[0])]
                if export_mode:
                    label2color[-1][int(e[1])] = maxId
                    label2color[-1][int(e[2])] = maxId + 1
                    divisions.append(
                        (i, int(e[0]), ancestor_color, int(e[1]), maxId,
                         int(e[2]), maxId + 1))
                    maxId += 2
                else:
                    label2color[-1][int(e[1])] = ancestor_color
                    label2color[-1][int(e[2])] = ancestor_color

            for e in merger:
                mergers[-1][int(e[0])] = int(e[1])

            for e in multi:
                if int(e[2]) >= 0 and not label2color[time_range[0] +
                                                      int(e[2])].has_key(
                                                          int(e[0])):
                    if successive_ids:
                        label2color[time_range[0] + int(e[2])][int(
                            e[0])] = maxId
                        maxId += 1
                    else:
                        label2color[time_range[0] + int(e[2])][int(
                            e[0])] = np.random.randint(1, 255)
                label2color[-1][int(e[1])] = label2color[time_range[0] +
                                                         int(e[2])][int(e[0])]
                if export_mode:
                    e_start = int(time_range[0] + e[2])
                    e_end = int(i)
                    track_id = label2color[time_range[0] + int(e[2])][int(
                        e[0])]
                    double_object = multi_move[e_start][e[0]]
                    for t in xrange(e_start, e_end):
                        extra_track_ids.setdefault(t + 1, {})
                        extra_track_ids[t + 1].setdefault(double_object, [])
                        extra_track_ids[t + 1][double_object].append(track_id)
                        next_key = (t, double_object)
                        double_object = multi_move_next[next_key]

        # last timestep
        merger = get_dict_value(
            events[str(time_range[-1] - time_range[0] + 1)], "merger", [])
        mergers.append({})
        for e in merger:
            mergers[-1][int(e[0])] = int(e[1])

        # mark the filtered objects
        for i in filtered_labels.keys():
            if int(i) + time_range[0] >= len(label2color):
                continue
            fl_at = filtered_labels[i]
            for l in fl_at:
                assert l not in label2color[int(i) + time_range[0]]
                label2color[int(i) + time_range[0]][l] = 0

        if export_mode:  # don't set fields when in export_mode
            self.track_id = label2color
            self.extra_track_ids = extra_track_ids
            self.divisions = divisions
            return label2color, extra_track_ids, divisions

        self.label2color = label2color
        self.mergers = mergers

        self.Output._value = None
        self.Output.setDirty(slice(None))

        if 'MergerOutput' in self.outputs:
            self.MergerOutput._value = None
            self.MergerOutput.setDirty(slice(None))