예제 #1
0
    def do_export(self, settings, selected_features, progress_slot):
        """
        Implements ExportOperator.do_export(settings, selected_features, progress_slot
        Most likely called from ExportOperator.export_object_data
        :param settings: the settings for the exporter, see
        :param selected_features:
        :param progress_slot:
        :return:
        """
        from ilastik.utility.exportFile import objects_per_frame, ExportFile, ilastik_ids, Mode, Default, \
            flatten_dict, division_flatten_dict

        selected_features = list(selected_features)
        with_divisions = self.Parameters.value["withDivisions"] if self.Parameters.ready() else False
        obj_count = list(objects_per_frame(self.LabelImage))
        track_ids, extra_track_ids, divisions = self.export_track_ids()
        self._setLabel2Color()
        lineage = flatten_dict(self.label2color, obj_count)
        multi_move_max = self.Parameters.value["maxObj"] if self.Parameters.ready() else 2
        t_range = self.Parameters.value["time_range"] if self.Parameters.ready() else (0, 0)
        ids = ilastik_ids(obj_count)

        export_file = ExportFile(settings["file path"])
        export_file.ExportProgress.subscribe(progress_slot)
        export_file.InsertionProgress.subscribe(progress_slot)

        export_file.add_columns("table", range(sum(obj_count)), Mode.List, Default.KnimeId)
        export_file.add_columns("table", list(ids), Mode.List, Default.IlastikId)
        export_file.add_columns("table", lineage, Mode.List, Default.Lineage)
        export_file.add_columns("table", track_ids, Mode.IlastikTrackingTable,
                                {"max": multi_move_max, "counts": obj_count, "extra ids": extra_track_ids,
                                 "range": t_range})
        if with_divisions:
            object_feature_slot = self.ObjectFeaturesWithDivFeatures
        else:
            object_feature_slot = self.ObjectFeatures
        export_file.add_columns("table", object_feature_slot, Mode.IlastikFeatureTable,
                                {"selection": selected_features})

        if with_divisions:
            if divisions:
                div_lineage = division_flatten_dict(divisions, self.label2color)
                zips = zip(*divisions)
                divisions = zip(zips[0], div_lineage, *zips[1:])
                export_file.add_columns("divisions", divisions, Mode.List, Default.DivisionNames)
            else:
                logger.debug("No divisions occurred. Division Table will not be exported!")

        if settings["file type"] == "h5":
            export_file.add_rois(Default.LabelRoiPath, self.LabelImage, "table", settings["margin"], "labeling")
            if settings["include raw"]:
                export_file.add_image(Default.RawPath, self.RawImage)
            else:
                export_file.add_rois(Default.RawRoiPath, self.RawImage, "table", settings["margin"])
        export_file.write_all(settings["file type"], settings["compression"])

        export_file.ExportProgress.unsubscribe(progress_slot)
        export_file.InsertionProgress.unsubscribe(progress_slot)
예제 #2
0
    def do_export(self, settings, selected_features, progress_slot):
        """
        Implements ExportOperator.do_export(settings, selected_features, progress_slot
        Most likely called from ExportOperator.export_object_data
        :param settings: the settings for the exporter, see
        :param selected_features:
        :param progress_slot:
        :return:
        """
        from ilastik.utility.exportFile import objects_per_frame, ExportFile, ilastik_ids, Mode, Default, \
            flatten_dict, division_flatten_dict

        obj_count = list(objects_per_frame(self.LabelImage))
        track_ids, extra_track_ids, divisions = self.export_track_ids()
        self._setLabel2Color()
        lineage = flatten_dict(self.label2color, obj_count)
        multi_move_max = self.Parameters.value["maxObj"] if self.Parameters.ready() else 2
        multi_move_max = max(2, multi_move_max)
        t_range = self.Parameters.value["time_range"] if self.Parameters.ready() else (0, 0)
        ids = ilastik_ids(obj_count)

        export_file = ExportFile(settings["file path"])
        export_file.ExportProgress.subscribe(progress_slot)
        export_file.InsertionProgress.subscribe(progress_slot)

        export_file.add_columns("table", range(sum(obj_count)), Mode.List, Default.KnimeId)
        export_file.add_columns("table", list(ids), Mode.List, Default.IlastikId)
        export_file.add_columns("table", lineage, Mode.List, Default.Lineage)
        export_file.add_columns("table", track_ids, Mode.IlastikTrackingTable,
                                {"max": multi_move_max, "counts": obj_count, "extra ids": extra_track_ids,
                                 "range": t_range})
        export_file.add_columns("table", self.ObjectFeatures, Mode.IlastikFeatureTable,
                                {"selection": selected_features})

        if self.Parameters.value["withDivisions"] if self.Parameters.ready() else False:
            try:
                div_lineage = division_flatten_dict(divisions, self.label2color)
                zips = zip(*divisions)
                divisions = zip(zips[0], div_lineage, *zips[1:])
                export_file.add_columns("divisions", divisions, Mode.List, Default.DivisionNames)
            except Exception as e:
                if hasattr(progress_slot, "safe_popup"):
                    progress_slot.safe_popup_noclose("warning", "Warning", "Cannot export divisions.\nContinuing ...",
                                                     e)
                else:
                    print "\nWarning, Cannot export divisions"

        if settings["file type"] == "h5":
            export_file.add_rois(Default.LabelRoiPath, self.LabelImage, "table", settings["margin"], "labeling")
            if settings["include raw"]:
                export_file.add_image(Default.RawPath, self.RawImage)
            else:
                export_file.add_rois(Default.RawRoiPath, self.RawImage, "table", settings["margin"])
        export_file.write_all(settings["file type"], settings["compression"])

        export_file.ExportProgress.unsubscribe(progress_slot)
        export_file.InsertionProgress.unsubscribe(progress_slot)
예제 #3
0
    def do_export(self, settings, selected_features, progress_slot, lane_index, filename_suffix=""):
        """
        Implements ExportOperator.do_export(settings, selected_features, progress_slot
        Most likely called from ExportOperator.export_object_data
        :param settings: the settings for the exporter, see
        :param selected_features:
        :param progress_slot:
        :param lane_index: Ignored. (This is a single-lane operator. It is the caller's responsibility to make sure he's calling the right lane.)
        :param filename_suffix: If provided, appended to the filename (before the extension).
        :return:
        """
        
        obj_count = list(objects_per_frame(self.LabelImage))  # slow
        divisions = self.divisions
        t_range = (0, self.LabelImage.meta.shape[self.LabelImage.meta.axistags.index("t")])
        oid2tid, _ = self._getObjects(t_range, None)  # slow
        max_tracks = max(max(list(map(len, list(i.values())))) if list(map(len, list(i.values()))) else 0 for i in list(oid2tid.values()))
        ids = ilastik_ids(obj_count)

        file_path = settings["file path"]
        if filename_suffix:
            path, ext = os.path.splitext(file_path)
            file_path = path + "-" + filename_suffix + ext

        export_file = ExportFile(file_path)
        export_file.ExportProgress.subscribe(progress_slot)
        export_file.InsertionProgress.subscribe(progress_slot)

        export_file.add_columns("table", list(range(sum(obj_count))), Mode.List, Default.KnimeId)
        export_file.add_columns("table", list(ids), Mode.List, Default.IlastikId)
        export_file.add_columns("table", oid2tid, Mode.IlastikTrackingTable,
                                {"max": max_tracks, "counts": obj_count, "extra ids": {},
                                 "range": t_range})
        export_file.add_columns("table", self.ObjectFeatures, Mode.IlastikFeatureTable,
                                {"selection": selected_features})

        if divisions:
            ott = partial(self.lookup_oid_for_tid, oid2tid)
            divs = [(value[1], ott(key, value[1]), key, ott(value[0][0], value[1] + 1), value[0][0],
                     ott(value[0][1], value[1] + 1), value[0][1])
                    for key, value in sorted(iter(divisions.items()), key=itemgetter(0))]
            assert sum(Default.ManualDivMap) == len(divs[0])
            names = list(compress(Default.DivisionNames["names"], Default.ManualDivMap))
            export_file.add_columns("divisions", divs, Mode.List, extra={"names": names})

        if settings["file type"] == "h5":
            export_file.add_rois(Default.LabelRoiPath, self.LabelImage, "table", settings["margin"], "labeling")
            if settings["include raw"]:
                export_file.add_image(Default.RawPath, self.RawImage)
            else:
                export_file.add_rois(Default.RawRoiPath, self.RawImage, "table", settings["margin"])
        export_file.write_all(settings["file type"], settings["compression"])

        export_file.ExportProgress.unsubscribe(progress_slot)
        export_file.InsertionProgress.unsubscribe(progress_slot)
예제 #4
0
    def _do_export_impl(self, settings, selected_features, progress_slot, object_feature_slot, label_image_slot, lane_index, filename_suffix=""):
        from ilastik.utility.exportFile import objects_per_frame, ExportFile, ilastik_ids, Mode, Default, \
            flatten_dict, division_flatten_dict

        selected_features = list(selected_features)
        with_divisions = self.Parameters.value["withDivisions"] if self.Parameters.ready() else False
        obj_count = list(objects_per_frame(label_image_slot))
        track_ids, extra_track_ids, divisions = self.export_track_ids()
        self._setLabel2Color()
        lineage = flatten_dict(self.label2color, obj_count)
        multi_move_max = self.Parameters.value["maxObj"] if self.Parameters.ready() else 2
        t_range = self.Parameters.value["time_range"] if self.Parameters.ready() else (0, 0)
        ids = ilastik_ids(obj_count)

        file_path = settings["file path"]
        if filename_suffix:
            path, ext = os.path.splitext(file_path)
            file_path = path + "-" + filename_suffix + ext

        export_file = ExportFile(file_path)
        export_file.ExportProgress.subscribe(progress_slot)
        export_file.InsertionProgress.subscribe(progress_slot)

        export_file.add_columns("table", range(sum(obj_count)), Mode.List, Default.KnimeId)
        export_file.add_columns("table", list(ids), Mode.List, Default.IlastikId)
        export_file.add_columns("table", lineage, Mode.List, Default.Lineage)
        export_file.add_columns("table", track_ids, Mode.IlastikTrackingTable,
                                {"max": multi_move_max, "counts": obj_count, "extra ids": extra_track_ids,
                                 "range": t_range})

        export_file.add_columns("table", object_feature_slot, Mode.IlastikFeatureTable,
                                {"selection": selected_features})

        if with_divisions:
            if divisions:
                div_lineage = division_flatten_dict(divisions, self.label2color)
                zips = zip(*divisions)
                divisions = zip(zips[0], div_lineage, *zips[1:])
                export_file.add_columns("divisions", divisions, Mode.List, Default.DivisionNames)
            else:
                logger.debug("No divisions occurred. Division Table will not be exported!")

        if settings["file type"] == "h5":
            export_file.add_rois(Default.LabelRoiPath, label_image_slot, "table", settings["margin"], "labeling")
            if settings["include raw"]:
                export_file.add_image(Default.RawPath, self.RawImage)
            else:
                export_file.add_rois(Default.RawRoiPath, self.RawImage, "table", settings["margin"])
        export_file.write_all(settings["file type"], settings["compression"])

        export_file.ExportProgress.unsubscribe(progress_slot)
        export_file.InsertionProgress.unsubscribe(progress_slot)
예제 #5
0
    def _do_export_impl(self, settings, selected_features, progress_slot, object_feature_slot, label_image_slot, lane_index, filename_suffix=""):
        from ilastik.utility.exportFile import objects_per_frame, ExportFile, ilastik_ids, Mode, Default, \
            flatten_dict, division_flatten_dict

        selected_features = list(selected_features)
        with_divisions = self.Parameters.value["withDivisions"] if self.Parameters.ready() else False
        obj_count = list(objects_per_frame(label_image_slot))
        track_ids, extra_track_ids, divisions = self.export_track_ids()
        self._setLabel2Color()
        lineage = flatten_dict(self.label2color, obj_count)
        multi_move_max = self.Parameters.value["maxObj"] if self.Parameters.ready() else 2
        t_range = self.Parameters.value["time_range"] if self.Parameters.ready() else (0, 0)
        ids = ilastik_ids(obj_count)

        file_path = settings["file path"]
        if filename_suffix:
            path, ext = os.path.splitext(file_path)
            file_path = path + "-" + filename_suffix + ext

        export_file = ExportFile(file_path)
        export_file.ExportProgress.subscribe(progress_slot)
        export_file.InsertionProgress.subscribe(progress_slot)

        export_file.add_columns("table", range(sum(obj_count)), Mode.List, Default.KnimeId)
        export_file.add_columns("table", list(ids), Mode.List, Default.IlastikId)
        export_file.add_columns("table", lineage, Mode.List, Default.Lineage)
        export_file.add_columns("table", track_ids, Mode.IlastikTrackingTable,
                                {"max": multi_move_max, "counts": obj_count, "extra ids": extra_track_ids,
                                 "range": t_range})

        export_file.add_columns("table", object_feature_slot, Mode.IlastikFeatureTable,
                                {"selection": selected_features})

        if with_divisions:
            if divisions:
                div_lineage = division_flatten_dict(divisions, self.label2color)
                zips = zip(*divisions)
                divisions = zip(zips[0], div_lineage, *zips[1:])
                export_file.add_columns("divisions", divisions, Mode.List, Default.DivisionNames)
            else:
                logger.debug("No divisions occurred. Division Table will not be exported!")

        if settings["file type"] == "h5":
            export_file.add_rois(Default.LabelRoiPath, label_image_slot, "table", settings["margin"], "labeling")
            if settings["include raw"]:
                export_file.add_image(Default.RawPath, self.RawImage)
            else:
                export_file.add_rois(Default.RawRoiPath, self.RawImage, "table", settings["margin"])
        export_file.write_all(settings["file type"], settings["compression"])

        export_file.ExportProgress.unsubscribe(progress_slot)
        export_file.InsertionProgress.unsubscribe(progress_slot)
예제 #6
0
    def do_export(self, settings, selected_features, progress_slot):
        """
        Implements ExportOperator.do_export(settings, selected_features, progress_slot
        Most likely called from ExportOperator.export_object_data
        :param settings: the settings for the exporter, see
        :param selected_features:
        :param progress_slot:
        :return:
        """
        obj_count = list(objects_per_frame(self.LabelImage))  # slow
        divisions = self.divisions
        t_range = (0, self.LabelImage.meta.shape[self.LabelImage.meta.axistags.index("t")])
        oid2tid, _ = self._getObjects(t_range, None)  # slow
        max_tracks = max(max(map(len, i.values())) for i in oid2tid.values())
        ids = ilastik_ids(obj_count)

        export_file = ExportFile(settings["file path"])
        export_file.ExportProgress.subscribe(progress_slot)
        export_file.InsertionProgress.subscribe(progress_slot)

        export_file.add_columns("table", range(sum(obj_count)), Mode.List, Default.KnimeId)
        export_file.add_columns("table", list(ids), Mode.List, Default.IlastikId)
        export_file.add_columns("table", oid2tid, Mode.IlastikTrackingTable,
                                {"max": max_tracks, "counts": obj_count, "extra ids": {},
                                 "range": t_range})
        export_file.add_columns("table", self.ObjectFeatures, Mode.IlastikFeatureTable,
                                {"selection": selected_features})

        if divisions:
            ott = partial(self.lookup_oid_for_tid, oid2tid)
            divs = [(value[1], ott(key, value[1]), key, ott(value[0][0], value[1] + 1), value[0][0],
                     ott(value[0][1], value[1] + 1), value[0][1])
                    for key, value in sorted(divisions.iteritems(), key=itemgetter(0))]
            assert sum(Default.ManualDivMap) == len(divs[0])
            names = list(compress(Default.DivisionNames["names"], Default.ManualDivMap))
            export_file.add_columns("divisions", divs, Mode.List, extra={"names": names})

        if settings["file type"] == "h5":
            export_file.add_rois(Default.LabelRoiPath, self.LabelImage, "table", settings["margin"], "labeling")
            if settings["include raw"]:
                export_file.add_image(Default.RawPath, self.RawImage)
            else:
                export_file.add_rois(Default.RawRoiPath, self.RawImage, "table", settings["margin"])
        export_file.write_all(settings["file type"], settings["compression"])

        export_file.ExportProgress.unsubscribe(progress_slot)
        export_file.InsertionProgress.unsubscribe(progress_slot)
예제 #7
0
    def do_export(self, settings, selected_features, progress_slot):
        """
        Implements ExportOperator.do_export(settings, selected_features, progress_slot
        Most likely called from ExportOperator.export_object_data
        :param settings: the settings for the exporter, see
        :param selected_features:
        :param progress_slot:
        :return:
        """
        from ilastik.utility.exportFile import objects_per_frame, ExportFile, ilastik_ids, Mode, Default, \
            flatten_dict, division_flatten_dict

        selected_features = list(selected_features)
        with_divisions = self.Parameters.value[
            "withDivisions"] if self.Parameters.ready() else False
        obj_count = list(objects_per_frame(self.LabelImage))
        track_ids, extra_track_ids, divisions = self.export_track_ids()
        self._setLabel2Color()
        lineage = flatten_dict(self.label2color, obj_count)
        multi_move_max = self.Parameters.value[
            "maxObj"] if self.Parameters.ready() else 2
        t_range = self.Parameters.value["time_range"] if self.Parameters.ready(
        ) else (0, 0)
        ids = ilastik_ids(obj_count)

        export_file = ExportFile(settings["file path"])
        export_file.ExportProgress.subscribe(progress_slot)
        export_file.InsertionProgress.subscribe(progress_slot)

        export_file.add_columns("table", range(sum(obj_count)), Mode.List,
                                Default.KnimeId)
        export_file.add_columns("table", list(ids), Mode.List,
                                Default.IlastikId)
        export_file.add_columns("table", lineage, Mode.List, Default.Lineage)
        export_file.add_columns(
            "table", track_ids, Mode.IlastikTrackingTable, {
                "max": multi_move_max,
                "counts": obj_count,
                "extra ids": extra_track_ids,
                "range": t_range
            })
        if with_divisions:
            object_feature_slot = self.ObjectFeaturesWithDivFeatures
        else:
            object_feature_slot = self.ObjectFeatures
        export_file.add_columns("table", object_feature_slot,
                                Mode.IlastikFeatureTable,
                                {"selection": selected_features})

        if with_divisions:
            if divisions:
                div_lineage = division_flatten_dict(divisions,
                                                    self.label2color)
                zips = zip(*divisions)
                divisions = zip(zips[0], div_lineage, *zips[1:])
                export_file.add_columns("divisions", divisions, Mode.List,
                                        Default.DivisionNames)
            else:
                logger.debug(
                    "No divisions occurred. Division Table will not be exported!"
                )

        if settings["file type"] == "h5":
            export_file.add_rois(Default.LabelRoiPath, self.LabelImage,
                                 "table", settings["margin"], "labeling")
            if settings["include raw"]:
                export_file.add_image(Default.RawPath, self.RawImage)
            else:
                export_file.add_rois(Default.RawRoiPath, self.RawImage,
                                     "table", settings["margin"])
        export_file.write_all(settings["file type"], settings["compression"])

        export_file.ExportProgress.unsubscribe(progress_slot)
        export_file.InsertionProgress.unsubscribe(progress_slot)
예제 #8
0
    def _do_export_impl(self, settings, selected_features, progress_slot, object_feature_slot, label_image_slot, lane_index, filename_suffix=""):
        from ilastik.utility.exportFile import objects_per_frame, ExportFile, ilastik_ids, Mode, Default, \
            flatten_dict, division_flatten_dict

        hypothesesGraph = self.HypothesesGraph.value

        if not hypothesesGraph:
            raise DatasetConstraintError('Tracking', 'Tracking solution not ready: Did you forgot to press the Track button?')

        with_divisions = self.Parameters.value["withDivisions"] if self.Parameters.ready() else False

        resolvedMergersDict = self.ResolvedMergers.value

        obj_count = list(objects_per_frame(label_image_slot))        
        object_ids_generator = ilastik_ids(obj_count)
        
        # Tracking results data 
        object_ids = [] 
        lineage_ids = []
        track_ids = []
        
        # Division table data
        div_timesteps = []
        div_object_ids = []
        div_lineage_ids = []
        div_track_ids = []
        div_child1_oids = []
        div_child1_track_ids = []
        div_child2_oids = []
        div_child2_track_ids = []        
        
        # these will be set for false detections
        false_detection_lineage_id = 1
        false_detection_track_id = 1

        # these will be set for resolved mergers:
        resolved_merger_lineage_id = 0
        resolved_merger_track_id = 0

        for (time, object_id) in object_ids_generator: 
            node = (time, object_id)
            
            # Populate results table
            object_ids.append(node)
            
            if hypothesesGraph.hasNode(node):     
                lineage_id = hypothesesGraph.getLineageId(time, object_id)

                if (resolvedMergersDict is not None) and (time in resolvedMergersDict) and (object_id in resolvedMergersDict[time]):
                    isMerger = True
                else:
                    isMerger = False

                if not lineage_id:
                    if isMerger: 
                        lineage_id = resolved_merger_lineage_id
                    else:
                        lineage_id = false_detection_lineage_id
                lineage_ids.append(lineage_id)
                    
                track_id = hypothesesGraph.getTrackId(time, object_id)    
                if not track_id:
                    if isMerger: 
                        track_id = resolved_merger_track_id
                    else:
                        track_id = false_detection_track_id
                track_ids.append(track_id) 
                
                # Populate division table  
                if with_divisions and 'divisionValue' in hypothesesGraph._graph.node[node] and hypothesesGraph._graph.node[node]['divisionValue']:
                    div_timesteps.append(time)
                    div_object_ids.append(object_id)
                    div_lineage_ids.append(object_id)
                    div_track_ids.append(track_id)
                    
                    assert len(hypothesesGraph.countOutgoingObjects(node)) != (2,2), "Division node {}, does not have 2 separate children".format(node) 
                    
                    childIdx = 1
                    for outEdge in hypothesesGraph._graph.out_edges(node):
                        if hypothesesGraph._graph.edge[outEdge[0]][outEdge[1]]['value'] == 1:
                            child = outEdge[1]
                            child_time = child[0]
                            child_object_id = child[1]
                            child_track_id = hypothesesGraph.getTrackId(child_time, child_object_id)
                            if not child_track_id:
                                logger.warning("Node {} in frame {} has no track ID set but is child of a division...".format(child_time, child_object_id))
                                child_track_id = 0

                            if childIdx == 1:
                                div_child1_oids.append(child_object_id)
                                div_child1_track_ids.append(child_track_id)
                            else:
                                div_child2_oids.append(child_object_id)
                                div_child2_track_ids.append(child_track_id)
                            
                            childIdx += 1 # next child will be 2nd
                    assert(childIdx == 3)
            else:
                lineage_ids.append(0)
                track_ids.append(0)
                
        # Write results table
        selected_features = list(selected_features)

        file_path = settings["file path"]
        if filename_suffix:
            path, ext = os.path.splitext(file_path)
            file_path = path + "-" + filename_suffix + ext

        export_file = ExportFile(file_path)
        export_file.ExportProgress.subscribe(progress_slot)
        export_file.InsertionProgress.subscribe(progress_slot)

        export_file.add_columns("table", range(sum(obj_count)), Mode.List, Default.KnimeId)
        export_file.add_columns("table", object_ids, Mode.List, Default.IlastikId)
        export_file.add_columns("table", lineage_ids, Mode.List, Default.Lineage)
        export_file.add_columns("table", track_ids, Mode.List, Default.TrackId)

        export_file.add_columns("table", object_feature_slot, Mode.IlastikFeatureTable,
                                {"selection": selected_features})

        # Write divisions tab;e
        if with_divisions:
            divisions_data = zip(div_timesteps, div_object_ids, div_lineage_ids, div_track_ids, div_child1_oids, div_child1_track_ids, div_child2_oids, div_child2_track_ids)
            if divisions_data:
                export_file.add_columns("divisions", divisions_data, Mode.List, Default.DivisionNames)

        if settings["file type"] == "h5":
            export_file.add_rois(Default.LabelRoiPath, label_image_slot, "table", settings["margin"], "labeling")
            if settings["include raw"]:
                export_file.add_image(Default.RawPath, self.RawImage)
            else:
                export_file.add_rois(Default.RawRoiPath, self.RawImage, "table", settings["margin"])
                
        # Write files
        export_file.write_all(settings["file type"], settings["compression"])

        export_file.ExportProgress.unsubscribe(progress_slot)
        export_file.InsertionProgress.unsubscribe(progress_slot)
예제 #9
0
    def do_export(self, settings, selected_features, progress_slot, lane_index, filename_suffix=""):
        """
        Implements ExportOperator.do_export(settings, selected_features, progress_slot
        Most likely called from ExportOperator.export_object_data
        :param settings: the settings for the exporter, see
        :param selected_features:
        :param progress_slot:
        :param lane_index: Ignored. (This is a single-lane operator. It is the caller's responsibility to make sure he's calling the right lane.)
        :param filename_suffix: If provided, appended to the filename (before the extension).
        :return:
        """
        assert lane_index == 0, "This has only been tested in tracking workflows with a single image."

        from ilastik.utility.exportFile import objects_per_frame, ExportFile, ilastik_ids, Mode, Default, \
            flatten_dict, division_flatten_dict

        selected_features = list(selected_features)
        with_divisions = self.Parameters.value["withDivisions"] if self.Parameters.ready() else False
        obj_count = list(objects_per_frame(self.LabelImage))
        track_ids, extra_track_ids, divisions = self.export_track_ids()
        self._setLabel2Color()
        lineage = flatten_dict(self.label2color, obj_count)
        multi_move_max = self.Parameters.value["maxObj"] if self.Parameters.ready() else 2
        t_range = self.Parameters.value["time_range"] if self.Parameters.ready() else (0, 0)
        ids = ilastik_ids(obj_count)

        file_path = settings["file path"]
        if filename_suffix:
            path, ext = os.path.splitext(file_path)
            file_path = path + "-" + filename_suffix + ext

        export_file = ExportFile(file_path)
        export_file.ExportProgress.subscribe(progress_slot)
        export_file.InsertionProgress.subscribe(progress_slot)

        export_file.add_columns("table", range(sum(obj_count)), Mode.List, Default.KnimeId)
        export_file.add_columns("table", list(ids), Mode.List, Default.IlastikId)
        export_file.add_columns("table", lineage, Mode.List, Default.Lineage)
        export_file.add_columns("table", track_ids, Mode.IlastikTrackingTable,
                                {"max": multi_move_max, "counts": obj_count, "extra ids": extra_track_ids,
                                 "range": t_range})
        if with_divisions:
            object_feature_slot = self.ObjectFeaturesWithDivFeatures
        else:
            object_feature_slot = self.ObjectFeatures
        export_file.add_columns("table", object_feature_slot, Mode.IlastikFeatureTable,
                                {"selection": selected_features})

        if with_divisions:
            if divisions:
                div_lineage = division_flatten_dict(divisions, self.label2color)
                zips = zip(*divisions)
                divisions = zip(zips[0], div_lineage, *zips[1:])
                export_file.add_columns("divisions", divisions, Mode.List, Default.DivisionNames)
            else:
                logger.debug("No divisions occurred. Division Table will not be exported!")

        if settings["file type"] == "h5":
            export_file.add_rois(Default.LabelRoiPath, self.LabelImage, "table", settings["margin"], "labeling")
            if settings["include raw"]:
                export_file.add_image(Default.RawPath, self.RawImage)
            else:
                export_file.add_rois(Default.RawRoiPath, self.RawImage, "table", settings["margin"])
        export_file.write_all(settings["file type"], settings["compression"])

        export_file.ExportProgress.unsubscribe(progress_slot)
        export_file.InsertionProgress.unsubscribe(progress_slot)
예제 #10
0
    def do_export(self,
                  settings,
                  selected_features,
                  progress_slot,
                  lane_index,
                  filename_suffix=""):
        """
        Implements ExportOperator.do_export(settings, selected_features, progress_slot
        Most likely called from ExportOperator.export_object_data
        :param settings: the settings for the exporter, see
        :param selected_features:
        :param progress_slot:
        :param lane_index: Ignored. (This is a single-lane operator. It is the caller's responsibility to make sure he's calling the right lane.)
        :param filename_suffix: If provided, appended to the filename (before the extension).
        :return:
        """

        obj_count = list(objects_per_frame(self.LabelImage))  # slow
        divisions = self.divisions
        t_range = (0, self.LabelImage.meta.shape[
            self.LabelImage.meta.axistags.index("t")])
        oid2tid, _ = self._getObjects(t_range, None)  # slow
        max_tracks = max(
            max(list(map(len, list(i.values())))
                ) if list(map(len, list(i.values()))) else 0
            for i in list(oid2tid.values()))
        ids = ilastik_ids(obj_count)

        file_path = settings["file path"]
        if filename_suffix:
            path, ext = os.path.splitext(file_path)
            file_path = path + "-" + filename_suffix + ext

        export_file = ExportFile(file_path)
        export_file.ExportProgress.subscribe(progress_slot)
        export_file.InsertionProgress.subscribe(progress_slot)

        export_file.add_columns("table", list(range(sum(obj_count))),
                                Mode.List, Default.KnimeId)
        export_file.add_columns("table", list(ids), Mode.List,
                                Default.IlastikId)
        export_file.add_columns(
            "table",
            oid2tid,
            Mode.IlastikTrackingTable,
            {
                "max": max_tracks,
                "counts": obj_count,
                "extra ids": {},
                "range": t_range
            },
        )
        export_file.add_columns("table", self.ObjectFeatures,
                                Mode.IlastikFeatureTable,
                                {"selection": selected_features})

        if divisions:
            ott = partial(self.lookup_oid_for_tid, oid2tid)
            divs = [(
                value[1],
                ott(key, value[1]),
                key,
                ott(value[0][0], value[1] + 1),
                value[0][0],
                ott(value[0][1], value[1] + 1),
                value[0][1],
            ) for key, value in sorted(iter(divisions.items()),
                                       key=itemgetter(0))]
            assert sum(Default.ManualDivMap) == len(divs[0])
            names = list(
                compress(Default.DivisionNames["names"], Default.ManualDivMap))
            export_file.add_columns("divisions",
                                    divs,
                                    Mode.List,
                                    extra={"names": names})

        if settings["file type"] == "h5":
            export_file.add_rois(Default.LabelRoiPath, self.LabelImage,
                                 "table", settings["margin"], "labeling")
            if settings["include raw"]:
                export_file.add_image(Default.RawPath, self.RawImage)
            else:
                export_file.add_rois(Default.RawRoiPath, self.RawImage,
                                     "table", settings["margin"])
        export_file.write_all(settings["file type"], settings["compression"])

        export_file.ExportProgress.unsubscribe(progress_slot)
        export_file.InsertionProgress.unsubscribe(progress_slot)
예제 #11
0
    def do_export(self, settings, selected_features, progress_slot):
        """
        Implements ExportOperator.do_export(settings, selected_features, progress_slot
        Most likely called from ExportOperator.export_object_data
        :param settings: the settings for the exporter, see
        :param selected_features:
        :param progress_slot:
        :return:
        """
        obj_count = list(objects_per_frame(self.LabelImage))  # slow
        divisions = self.divisions
        t_range = (0, self.LabelImage.meta.shape[
            self.LabelImage.meta.axistags.index("t")])
        oid2tid, _ = self._getObjects(t_range, None)  # slow
        max_tracks = max(max(map(len, i.values())) for i in oid2tid.values())
        ids = ilastik_ids(obj_count)

        export_file = ExportFile(settings["file path"])
        export_file.ExportProgress.subscribe(progress_slot)
        export_file.InsertionProgress.subscribe(progress_slot)

        export_file.add_columns("table", range(sum(obj_count)), Mode.List,
                                Default.KnimeId)
        export_file.add_columns("table", list(ids), Mode.List,
                                Default.IlastikId)
        export_file.add_columns(
            "table", oid2tid, Mode.IlastikTrackingTable, {
                "max": max_tracks,
                "counts": obj_count,
                "extra ids": {},
                "range": t_range
            })
        export_file.add_columns("table", self.ObjectFeatures,
                                Mode.IlastikFeatureTable,
                                {"selection": selected_features})

        if divisions:
            ott = partial(self.lookup_oid_for_tid, oid2tid)
            divs = [(value[1], ott(key, value[1]), key,
                     ott(value[0][0], value[1] + 1), value[0][0],
                     ott(value[0][1], value[1] + 1), value[0][1])
                    for key, value in sorted(divisions.iteritems(),
                                             key=itemgetter(0))]
            assert sum(Default.ManualDivMap) == len(divs[0])
            names = list(
                compress(Default.DivisionNames["names"], Default.ManualDivMap))
            export_file.add_columns("divisions",
                                    divs,
                                    Mode.List,
                                    extra={"names": names})

        if settings["file type"] == "h5":
            export_file.add_rois(Default.LabelRoiPath, self.LabelImage,
                                 "table", settings["margin"], "labeling")
            if settings["include raw"]:
                export_file.add_image(Default.RawPath, self.RawImage)
            else:
                export_file.add_rois(Default.RawRoiPath, self.RawImage,
                                     "table", settings["margin"])
        export_file.write_all(settings["file type"], settings["compression"])

        export_file.ExportProgress.unsubscribe(progress_slot)
        export_file.InsertionProgress.unsubscribe(progress_slot)
예제 #12
0
    def _do_export_impl(self, settings, selected_features, progress_slot, object_feature_slot, label_image_slot, lane_index, filename_suffix=""):
        from ilastik.utility.exportFile import objects_per_frame, ExportFile, ilastik_ids, Mode, Default, \
            flatten_dict, division_flatten_dict

        hypothesesGraph = self.HypothesesGraph.value

        if not hypothesesGraph:
            raise DatasetConstraintError('Tracking', 'Tracking solution not ready: Did you forgot to press the Track button?')

        with_divisions = self.Parameters.value["withDivisions"] if self.Parameters.ready() else False

        resolvedMergersDict = self.ResolvedMergers.value

        obj_count = list(objects_per_frame(label_image_slot))        
        object_ids_generator = ilastik_ids(obj_count)
        
        # Tracking results data 
        object_ids = [] 
        lineage_ids = []
        track_ids = []
        
        # Division table data
        div_timesteps = []
        div_object_ids = []
        div_lineage_ids = []
        div_track_ids = []
        div_child1_oids = []
        div_child1_track_ids = []
        div_child2_oids = []
        div_child2_track_ids = []        
        
        # these will be set for false detections
        false_detection_lineage_id = 1
        false_detection_track_id = 1

        # these will be set for resolved mergers:
        resolved_merger_lineage_id = 0
        resolved_merger_track_id = 0

        for (time, object_id) in object_ids_generator: 
            node = (time, object_id)
            
            # Populate results table
            object_ids.append(node)
            
            if hypothesesGraph.hasNode(node):     
                lineage_id = hypothesesGraph.getLineageId(time, object_id)

                if (resolvedMergersDict is not None) and (time in resolvedMergersDict) and (object_id in resolvedMergersDict[time]):
                    isMerger = True
                else:
                    isMerger = False

                if not lineage_id:
                    if isMerger: 
                        lineage_id = resolved_merger_lineage_id
                    else:
                        lineage_id = false_detection_lineage_id
                lineage_ids.append(lineage_id)
                    
                track_id = hypothesesGraph.getTrackId(time, object_id)    
                if not track_id:
                    if isMerger: 
                        track_id = resolved_merger_track_id
                    else:
                        track_id = false_detection_track_id
                track_ids.append(track_id) 
                
                # Populate division table  
                if with_divisions and 'divisionValue' in hypothesesGraph._graph.node[node] and hypothesesGraph._graph.node[node]['divisionValue']:
                    div_timesteps.append(time)
                    div_object_ids.append(object_id)
                    div_lineage_ids.append(object_id)
                    div_track_ids.append(track_id)
                    
                    assert len(hypothesesGraph.countOutgoingObjects(node)) != (2,2), "Division node {}, does not have 2 separate children".format(node) 
                    
                    childIdx = 1
                    for outEdge in hypothesesGraph._graph.out_edges(node):
                        if hypothesesGraph._graph.edge[outEdge[0]][outEdge[1]]['value'] == 1:
                            child = outEdge[1]
                            child_time = child[0]
                            child_object_id = child[1]
                            child_track_id = hypothesesGraph.getTrackId(child_time, child_object_id)
                            if not child_track_id:
                                logger.warning("Node {} in frame {} has no track ID set but is child of a division...".format(child_time, child_object_id))
                                child_track_id = 0

                            if childIdx == 1:
                                div_child1_oids.append(child_object_id)
                                div_child1_track_ids.append(child_track_id)
                            else:
                                div_child2_oids.append(child_object_id)
                                div_child2_track_ids.append(child_track_id)
                            
                            childIdx += 1 # next child will be 2nd
                    assert(childIdx == 3)
            else:
                lineage_ids.append(0)
                track_ids.append(0)
                
        # Write results table
        selected_features = list(selected_features)

        file_path = settings["file path"]
        if filename_suffix:
            path, ext = os.path.splitext(file_path)
            file_path = path + "-" + filename_suffix + ext

        export_file = ExportFile(file_path)
        export_file.ExportProgress.subscribe(progress_slot)
        export_file.InsertionProgress.subscribe(progress_slot)

        export_file.add_columns("table", range(sum(obj_count)), Mode.List, Default.KnimeId)
        export_file.add_columns("table", object_ids, Mode.List, Default.IlastikId)
        export_file.add_columns("table", lineage_ids, Mode.List, Default.Lineage)
        export_file.add_columns("table", track_ids, Mode.List, Default.TrackId)

        export_file.add_columns("table", object_feature_slot, Mode.IlastikFeatureTable,
                                {"selection": selected_features})

        # Write divisions tab;e
        if with_divisions:
            divisions_data = zip(div_timesteps, div_object_ids, div_lineage_ids, div_track_ids, div_child1_oids, div_child1_track_ids, div_child2_oids, div_child2_track_ids)
            if divisions_data:
                export_file.add_columns("divisions", divisions_data, Mode.List, Default.DivisionNames)

        if settings["file type"] == "h5":
            export_file.add_rois(Default.LabelRoiPath, label_image_slot, "table", settings["margin"], "labeling")
            if settings["include raw"]:
                export_file.add_image(Default.RawPath, self.RawImage)
            else:
                export_file.add_rois(Default.RawRoiPath, self.RawImage, "table", settings["margin"])
                
        # Write files
        export_file.write_all(settings["file type"], settings["compression"])

        export_file.ExportProgress.unsubscribe(progress_slot)
        export_file.InsertionProgress.unsubscribe(progress_slot)