Пример #1
0
 def __init__(self,
              classifierPath=None,
              ilpFilename=None,
              ilpOptions=IlastikProjectOptions(),
              selectedFeatures=[]):
     """
     Construct a random forest by either loading it from file (`classifierPath` and `ilpFilename` must be given),
     or an empty untrained random forest with specified `selectedFeatures`
     """
     self._options = ilpOptions
     self._classifierPath = classifierPath
     self._ilpFilename = ilpFilename
     if ilpFilename is not None and classifierPath is not None:
         self._randomForests = self._readRandomForests()
         self.selectedFeatures = self._readSelectedFeatures()
     else:
         self._randomForests = []
         self.selectedFeatures = selectedFeatures
Пример #2
0
def run_pipeline(options, unknown):
    """
    Run the complete tracking pipeline by invoking the different steps.
    Using the `do-SOMETHING` switches one can configure which parts of the pipeline are run.

    **Params:**

    * `options`: the options of the tracking script as returned from argparse
    * `unknown`: unknown parameters read from the config file, needed in case merger resolving is supposed to be run.

    """

    params = convertToDict(unknown)
    
    if options.do_extract_weights:
        logging.info("Extracting weights from ilastik project...")
        weights = hytra.core.ilastik_project_options.extractWeightDictFromIlastikProject(options.ilastik_tracking_project)
    else:
        with open(options.weight_filename, 'r') as f:
            weights = json.load(f)

    if options.do_create_graph:
        logging.info("Create hypotheses graph...")

        import hytra.core.probabilitygenerator as probabilitygenerator
        from hytra.core.ilastik_project_options import IlastikProjectOptions
        ilpOptions = IlastikProjectOptions()
        ilpOptions.labelImagePath = params[str('label-image-path')]
        ilpOptions.labelImageFilename = params[str('label-image-file')]
        ilpOptions.rawImagePath = params[str('raw-data-path')]
        ilpOptions.rawImageFilename = params[str('raw-data-file')]
        try:
            ilpOptions.rawImageAxes = params[str('raw-data-axes')]
        except:
            ilpOptions.rawImageAxes = 'txyzc'

        ilpOptions.sizeFilter = [int(params[str('min-size')]), 100000]

        if 'object-count-classifier-file' in params:
            ilpOptions.objectCountClassifierFilename = params[str('object-count-classifier-file')]
        else:
            ilpOptions.objectCountClassifierFilename = options.ilastik_tracking_project

        withDivisions = 'without-divisions' not in params
        if withDivisions:
            if 'division-classifier-file' in params:
                ilpOptions.divisionClassifierFilename = params[str('division-classifier-file')]
            else:
                ilpOptions.divisionClassifierFilename = options.ilastik_tracking_project
        else:
            ilpOptions.divisionClassifierFilename = None

        probGenerator = probabilitygenerator.IlpProbabilityGenerator(ilpOptions, 
                                              pluginPaths=[str('../hytra/plugins')],
                                              useMultiprocessing=False)

        # if time_range is not None:
        #     traxelstore.timeRange = time_range

        probGenerator.fillTraxels(usePgmlink=False)
        fieldOfView = constructFov(probGenerator.shape,
                                   probGenerator.timeRange[0],
                                   probGenerator.timeRange[1],
                                   [probGenerator.x_scale,
                                   probGenerator.y_scale,
                                   probGenerator.z_scale])

        hypotheses_graph = IlastikHypothesesGraph(
            probabilityGenerator=probGenerator,
            timeRange=probGenerator.timeRange,
            maxNumObjects=int(params[str('max-number-objects')]),
            numNearestNeighbors=int(params[str('max-nearest-neighbors')]),
            fieldOfView=fieldOfView,
            withDivisions=withDivisions,
            divisionThreshold=0.1
        )

        withTracklets = True
        if withTracklets:
            hypotheses_graph = hypotheses_graph.generateTrackletGraph()

        hypotheses_graph.insertEnergies()
        trackingGraph = hypotheses_graph.toTrackingGraph()
    else:
        trackingGraph = JsonTrackingGraph(model_filename=options.model_filename)

    if options.do_convexify:
        logging.info("Convexifying graph energies...")
        trackingGraph.convexifyCosts()

    # get model out of trackingGraph
    model = trackingGraph.model

    if options.do_tracking:
        logging.info("Run tracking...")
        if options.solver == "flow-based":
            result = dpct.trackFlowBased(model, weights)
        elif options.solver == "ilp":
            try:
                import multiHypoTracking_with_cplex as mht
            except ImportError:
                try:
                    import multiHypoTracking_with_gurobi as mht
                except ImportError:
                    raise ImportError("Could not find multi hypotheses tracking ilp solver")
            result = mht.track(model, weights)
            
        hytra.core.jsongraph.writeToFormattedJSON(options.result_filename, result)
        
        if hypotheses_graph:
            # insert the solution into the hypotheses graph and from that deduce the lineages
            hypotheses_graph.insertSolution(result)
            hypotheses_graph.computeLineage()

    if options.do_merger_resolving:
        logging.info("Run merger resolving")
        trackingGraph = JsonTrackingGraph(model=model, result=result)
        merger_resolver = JsonMergerResolver(
            trackingGraph,
            ilpOptions.labelImageFilename,
            ilpOptions.labelImagePath,
            params[str('out-label-image-file')],
            ilpOptions.rawImageFilename,
            ilpOptions.rawImagePath,
            ilpOptions.rawImageAxes,
            [str('../hytra/plugins')],
            True)
        ilpOptions.labelImagePath = params[str('label-image-path')]
        ilpOptions.labelImageFilename = params[str('label-image-file')]
        ilpOptions.rawImagePath = params[str('raw-data-path')]
        ilpOptions.rawImageFilename = params[str('raw-data-file')]
        try:
            ilpOptions.rawImageAxes = params[str('raw-data-axes')]
        except:
            ilpOptions.rawImageAxes = 'txyzc'
        merger_resolver.run(None,  None)
Пример #3
0
def loadProbabilityGenerator(options,
                             ilpFilename,
                             objectCountClassifierPath=None,
                             divisionClassifierPath=None,
                             time_range=None,
                             usePgmlink=True,
                             featuresOnly=False):
    """
    Set up a python side traxel store: compute all features, but do not evaluate classifiers.
    """
    import hytra.core.probabilitygenerator as traxelstore
    from hytra.core.ilastik_project_options import IlastikProjectOptions
    ilpOptions = IlastikProjectOptions()
    ilpOptions.labelImagePath = options.label_img_path
    ilpOptions.rawImagePath = options.raw_path
    ilpOptions.rawImageFilename = options.raw_filename
    ilpOptions.rawImageAxes = options.raw_axes
    ilpOptions.sizeFilter = [options.minsize, options.maxsize]
    if options.label_image_file is not None:
        ilpOptions.labelImageFilename = options.label_image_file
    else:
        ilpOptions.labelImageFilename = ilpFilename

    if featuresOnly:
        ilpOptions.objectCountClassifierFilename = None
        ilpOptions.divisionClassifierFilename = None
    else:
        ilpOptions.objectCountClassifierPath = objectCountClassifierPath
        ilpOptions.divisionClassifierPath = divisionClassifierPath
        if options.obj_count_file != None:
            ilpOptions.objectCountClassifierFilename = options.obj_count_file
        else:
            ilpOptions.objectCountClassifierFilename = ilpFilename

        if options.div_file != None:
            ilpOptions.divisionClassifierFilename = options.div_file
        else:
            ilpOptions.divisionClassifierFilename = ilpFilename

    probGenerator = traxelstore.IlpProbabilityGenerator(
        ilpOptions,
        turnOffFeatures=options.turnOffFeatures,
        pluginPaths=options.pluginPaths,
        useMultiprocessing=not options.disableMultiprocessing)
    if time_range is not None:
        probGenerator.timeRange = time_range

    a = probGenerator.fillTraxels(usePgmlink=usePgmlink,
                                  turnOffFeatures=options.turnOffFeatures)
    if usePgmlink:
        t, f = a
    else:
        t = None
        f = None
    return probGenerator, t, f
Пример #4
0
def setupGraph(options):
    """
    Configure where to load raw data and classifiers from, then first find all objects and their features 
    and probabilities with the probabilitygenerator, and finally build a hypotheses graph and prepare it for tracking
    """
    ilpOptions = IlastikProjectOptions()
    ilpOptions.labelImagePath = options.label_image_paths[0]
    ilpOptions.labelImageFilename = options.label_image_files[0]

    ilpOptions.rawImagePath = options.raw_data_path
    ilpOptions.rawImageFilename = options.raw_data_file
    ilpOptions.rawImageAxes = options.raw_data_axes
    
    ilpOptions.sizeFilter = [10, 100000]
    ilpOptions.objectCountClassifierFilename = options.obj_count_classifier_file
    ilpOptions.objectCountClassifierPath = options.obj_count_classifier_path
    
    withDivisions = options.with_divisions
    if withDivisions:
        ilpOptions.divisionClassifierFilename = options.div_classifier_file
        ilpOptions.divisionClassifierPath = options.div_classifier_path
    else:
        ilpOptions.divisionClassifierFilename = None

    getLogger().info("Extracting traxels from images")
    probGenerator = probabilitygenerator.ConflictingSegmentsProbabilityGenerator(
        ilpOptions, 
        options.label_image_files[1:],
        options.label_image_paths[1:],
        pluginPaths=['../hytra/plugins'],
        useMultiprocessing=not options.disableMultiprocessing)

    # restrict range of timeframes used for learning and tracking
    if options.end_frame < 0:
        options.end_frame += probGenerator.timeRange[1] + 1
    assert(options.init_frame < probGenerator.timeRange[1])
    assert(options.end_frame <= probGenerator.timeRange[1])
    probGenerator.timeRange = (options.init_frame, options.end_frame)

    probGenerator.fillTraxels(usePgmlink=False)
    fieldOfView = constructFov(probGenerator.shape,
                            probGenerator.timeRange[0],
                            probGenerator.timeRange[1],
                            [probGenerator.x_scale,
                            probGenerator.y_scale,
                            probGenerator.z_scale])

    getLogger().info("Building hypotheses graph")
    hypotheses_graph = IlastikHypothesesGraph(
        probabilityGenerator=probGenerator,
        timeRange=probGenerator.timeRange,
        maxNumObjects=1,
        numNearestNeighbors=options.max_nearest_neighbors,
        fieldOfView=fieldOfView,
        withDivisions=withDivisions,
        divisionThreshold=0.1,
        maxNeighborDistance=options.max_neighbor_distance
    )

    # if options.with_tracklets:
    #     hypotheses_graph = hypotheses_graph.generateTrackletGraph()

    getLogger().info("Preparing for tracking")
    hypotheses_graph.insertEnergies()
    trackingGraph = hypotheses_graph.toTrackingGraph()
    
    if options.do_convexify or options.use_flow_solver:
        getLogger().info("Convexifying graph energies...")
        trackingGraph.convexifyCosts()

    if options.graph_json_filename is not None:
        writeToFormattedJSON(options.graph_json_filename, trackingGraph.model)

    return fieldOfView, hypotheses_graph, ilpOptions, probGenerator, trackingGraph
Пример #5
0
def run_pipeline(options, unknown):
    """
    Run the complete tracking pipeline by invoking the different steps.
    Using the `do-SOMETHING` switches one can configure which parts of the pipeline are run.

    **Params:**

    * `options`: the options of the tracking script as returned from argparse
    * `unknown`: unknown parameters read from the config file, needed in case merger resolving is supposed to be run.

    """

    params = convertToDict(unknown)

    if options.do_extract_weights:
        logging.info("Extracting weights from ilastik project...")
        weights = hytra.core.ilastik_project_options.extractWeightDictFromIlastikProject(
            options.ilastik_tracking_project)
    else:
        with open(options.weight_filename, 'r') as f:
            weights = json.load(f)

    if options.do_create_graph:
        logging.info("Create hypotheses graph...")

        import hytra.core.probabilitygenerator as probabilitygenerator
        from hytra.core.ilastik_project_options import IlastikProjectOptions
        ilpOptions = IlastikProjectOptions()
        ilpOptions.labelImagePath = params[str('label-image-path')]
        ilpOptions.labelImageFilename = params[str('label-image-file')]
        ilpOptions.rawImagePath = params[str('raw-data-path')]
        ilpOptions.rawImageFilename = params[str('raw-data-file')]
        try:
            ilpOptions.rawImageAxes = params[str('raw-data-axes')]
        except:
            ilpOptions.rawImageAxes = 'txyzc'

        ilpOptions.sizeFilter = [int(params[str('min-size')]), 100000]

        if 'object-count-classifier-file' in params:
            ilpOptions.objectCountClassifierFilename = params[str(
                'object-count-classifier-file')]
        else:
            ilpOptions.objectCountClassifierFilename = options.ilastik_tracking_project

        withDivisions = 'without-divisions' not in params
        if withDivisions:
            if 'division-classifier-file' in params:
                ilpOptions.divisionClassifierFilename = params[str(
                    'division-classifier-file')]
            else:
                ilpOptions.divisionClassifierFilename = options.ilastik_tracking_project
        else:
            ilpOptions.divisionClassifierFilename = None

        probGenerator = probabilitygenerator.IlpProbabilityGenerator(
            ilpOptions,
            pluginPaths=[str('../hytra/plugins')],
            useMultiprocessing=False)

        # if time_range is not None:
        #     traxelstore.timeRange = time_range

        probGenerator.fillTraxels(usePgmlink=False)
        fieldOfView = constructFov(
            probGenerator.shape, probGenerator.timeRange[0],
            probGenerator.timeRange[1], [
                probGenerator.x_scale, probGenerator.y_scale,
                probGenerator.z_scale
            ])

        hypotheses_graph = IlastikHypothesesGraph(
            probabilityGenerator=probGenerator,
            timeRange=probGenerator.timeRange,
            maxNumObjects=int(params[str('max-number-objects')]),
            numNearestNeighbors=int(params[str('max-nearest-neighbors')]),
            fieldOfView=fieldOfView,
            withDivisions=withDivisions,
            divisionThreshold=0.1)

        withTracklets = True
        if withTracklets:
            hypotheses_graph = hypotheses_graph.generateTrackletGraph()

        hypotheses_graph.insertEnergies()
        trackingGraph = hypotheses_graph.toTrackingGraph()
    else:
        trackingGraph = JsonTrackingGraph(
            model_filename=options.model_filename)

    if options.do_convexify:
        logging.info("Convexifying graph energies...")
        trackingGraph.convexifyCosts()

    # get model out of trackingGraph
    model = trackingGraph.model

    if options.do_tracking:
        logging.info("Run tracking...")
        if options.solver == "flow-based":
            result = dpct.trackFlowBased(model, weights)
        elif options.solver == "ilp":
            try:
                import multiHypoTracking_with_cplex as mht
            except ImportError:
                try:
                    import multiHypoTracking_with_gurobi as mht
                except ImportError:
                    raise ImportError(
                        "Could not find multi hypotheses tracking ilp solver")
            result = mht.track(model, weights)

        hytra.core.jsongraph.writeToFormattedJSON(options.result_filename,
                                                  result)

        if hypotheses_graph:
            # insert the solution into the hypotheses graph and from that deduce the lineages
            hypotheses_graph.insertSolution(result)
            hypotheses_graph.computeLineage()

    if options.do_merger_resolving:
        logging.info("Run merger resolving")
        trackingGraph = JsonTrackingGraph(model=model, result=result)
        merger_resolver = JsonMergerResolver(
            trackingGraph, ilpOptions.labelImageFilename,
            ilpOptions.labelImagePath, params[str('out-label-image-file')],
            ilpOptions.rawImageFilename, ilpOptions.rawImagePath,
            ilpOptions.rawImageAxes, [str('../hytra/plugins')], True)
        ilpOptions.labelImagePath = params[str('label-image-path')]
        ilpOptions.labelImageFilename = params[str('label-image-file')]
        ilpOptions.rawImagePath = params[str('raw-data-path')]
        ilpOptions.rawImageFilename = params[str('raw-data-file')]
        try:
            ilpOptions.rawImageAxes = params[str('raw-data-axes')]
        except:
            ilpOptions.rawImageAxes = 'txyzc'
        merger_resolver.run(None, None)
Пример #6
0
                        dest='image_provider_name',
                        default="LocalImageLoader")
    parser.add_argument('--feature-serializer',
                        type=str,
                        dest='feature_serializer_name',
                        default='LocalFeatureSerializer')
    parser.add_argument(
        '--disable-multiprocessing',
        dest='disableMultiprocessing',
        action='store_true',
        help='Do not use multiprocessing to speed up computation',
        default=False)

    args = parser.parse_args()

    ilpOptions = IlastikProjectOptions()

    logging.basicConfig(level=logging.INFO)

    ilpOptions.objectCountClassifierPath = args.objectCountClassifierPath
    if args.withoutDivisions:
        ilpOptions.divisionClassifierPath = None
    else:
        ilpOptions.divisionClassifierPath = args.divisionClassifierPath
    ilpOptions.randomForestZeroPaddingWidth = args.rfZeroPadding
    ilpOptions.labelImagePath = args.labelImagePath
    ilpOptions.rawImagePath = args.rawPath
    ilpOptions.rawImageAxes = args.rawAxes

    ilpOptions.imageProviderName = args.image_provider_name
    ilpOptions.featureSerializerName = args.feature_serializer_name
def setupGraph(options):
    """
    Configure where to load raw data and classifiers from, then first find all objects and their features 
    and probabilities with the probabilitygenerator, and finally build a hypotheses graph and prepare it for tracking
    """
    ilpOptions = IlastikProjectOptions()
    ilpOptions.labelImagePath = options.label_image_paths[0]
    ilpOptions.labelImageFilename = options.label_image_files[0]

    ilpOptions.rawImagePath = options.raw_data_path
    ilpOptions.rawImageFilename = options.raw_data_file
    ilpOptions.rawImageAxes = options.raw_data_axes
    
    ilpOptions.sizeFilter = [10, 100000]
    ilpOptions.objectCountClassifierFilename = options.obj_count_classifier_file
    ilpOptions.objectCountClassifierPath = options.obj_count_classifier_path
    
    withDivisions = options.with_divisions
    if withDivisions:
        ilpOptions.divisionClassifierFilename = options.div_classifier_file
        ilpOptions.divisionClassifierPath = options.div_classifier_path
    else:
        ilpOptions.divisionClassifierFilename = None

    getLogger().info("Extracting traxels from images")
    probGenerator = probabilitygenerator.ConflictingSegmentsProbabilityGenerator(
        ilpOptions, 
        options.label_image_files[1:],
        options.label_image_paths[1:],
        pluginPaths=['../hytra/plugins'],
        useMultiprocessing=not options.disableMultiprocessing)

    # restrict range of timeframes used for learning and tracking
    if options.end_frame < 0:
        options.end_frame += probGenerator.timeRange[1] + 1
    assert(options.init_frame < probGenerator.timeRange[1])
    assert(options.end_frame <= probGenerator.timeRange[1])
    probGenerator.timeRange = (options.init_frame, options.end_frame)

    probGenerator.fillTraxels(usePgmlink=False)
    fieldOfView = constructFov(probGenerator.shape,
                            probGenerator.timeRange[0],
                            probGenerator.timeRange[1],
                            [probGenerator.x_scale,
                            probGenerator.y_scale,
                            probGenerator.z_scale])

    getLogger().info("Building hypotheses graph")
    hypotheses_graph = IlastikHypothesesGraph(
        probabilityGenerator=probGenerator,
        timeRange=probGenerator.timeRange,
        maxNumObjects=1,
        numNearestNeighbors=options.max_nearest_neighbors,
        fieldOfView=fieldOfView,
        withDivisions=withDivisions,
        divisionThreshold=0.1,
        maxNeighborDistance=options.max_neighbor_distance
    )

    # if options.with_tracklets:
    #     hypotheses_graph = hypotheses_graph.generateTrackletGraph()

    getLogger().info("Preparing for tracking")
    hypotheses_graph.insertEnergies()
    trackingGraph = hypotheses_graph.toTrackingGraph()
    
    if options.do_convexify or options.use_flow_solver:
        getLogger().info("Convexifying graph energies...")
        trackingGraph.convexifyCosts()

    if options.graph_json_filename is not None:
        writeToFormattedJSON(options.graph_json_filename, trackingGraph.model)

    return fieldOfView, hypotheses_graph, ilpOptions, probGenerator, trackingGraph
def loadProbabilityGenerator(options,
                      ilpFilename,
                      objectCountClassifierPath=None,
                      divisionClassifierPath=None,
                      time_range=None,
                      usePgmlink=True,
                      featuresOnly=False):
    """
    Set up a python side traxel store: compute all features, but do not evaluate classifiers.
    """
    import hytra.core.probabilitygenerator as traxelstore
    from hytra.core.ilastik_project_options import IlastikProjectOptions
    ilpOptions = IlastikProjectOptions()
    ilpOptions.labelImagePath = options.label_img_path
    ilpOptions.rawImagePath = options.raw_path
    ilpOptions.rawImageFilename = options.raw_filename
    ilpOptions.rawImageAxes = options.raw_axes
    ilpOptions.sizeFilter = [options.minsize, options.maxsize]
    if options.label_image_file is not None:
        ilpOptions.labelImageFilename = options.label_image_file
    else:
        ilpOptions.labelImageFilename = ilpFilename

    if featuresOnly:
        ilpOptions.objectCountClassifierFilename = None
        ilpOptions.divisionClassifierFilename = None
    else:
        ilpOptions.objectCountClassifierPath = objectCountClassifierPath
        ilpOptions.divisionClassifierPath = divisionClassifierPath
        if options.obj_count_file != None:
            ilpOptions.objectCountClassifierFilename = options.obj_count_file
        else:
            ilpOptions.objectCountClassifierFilename = ilpFilename

        if options.div_file != None:
            ilpOptions.divisionClassifierFilename = options.div_file
        else:
            ilpOptions.divisionClassifierFilename = ilpFilename

    probGenerator = traxelstore.IlpProbabilityGenerator(ilpOptions, 
                                            turnOffFeatures=options.turnOffFeatures, 
                                            pluginPaths=options.pluginPaths,
                                            useMultiprocessing=not options.disableMultiprocessing)
    if time_range is not None:
        probGenerator.timeRange = time_range

    a = probGenerator.fillTraxels(usePgmlink=usePgmlink, turnOffFeatures=options.turnOffFeatures)
    if usePgmlink:
        t, f = a
    else:
        t = None
        f = None
    return probGenerator, t, f
def test_twoSegmentations():
    # set up ConflictingSegmentsProbabilityGenerator
    ilpOptions = IlastikProjectOptions()
    ilpOptions.divisionClassifierPath = None
    ilpOptions.divisionClassifierFilename = None
    
    ilpOptions.rawImageFilename = 'tests/multiSegmentationHypothesesTestDataset/Raw.h5'
    ilpOptions.rawImagePath = 'exported_data'
    ilpOptions.rawImageAxes = 'txyzc'

    ilpOptions.labelImageFilename = 'tests/multiSegmentationHypothesesTestDataset/segmentation.h5'

    ilpOptions.objectCountClassifierFilename = 'tests/multiSegmentationHypothesesTestDataset/tracking.ilp'

    additionalLabelImageFilenames = ['tests/multiSegmentationHypothesesTestDataset/segmentationAlt.h5']
    additionalLabelImagePaths = [ilpOptions.labelImagePath]

    probabilityGenerator = ConflictingSegmentsProbabilityGenerator(
        ilpOptions, 
        additionalLabelImageFilenames,
        additionalLabelImagePaths,
        useMultiprocessing=False,
        verbose=False)
    probabilityGenerator.fillTraxels(usePgmlink=False)

    assert(len(probabilityGenerator.TraxelsPerFrame[0]) == 4)
    assert(len(probabilityGenerator.TraxelsPerFrame[1]) == 3)
    assert(len(probabilityGenerator.TraxelsPerFrame[2]) == 3)
    assert(len(probabilityGenerator.TraxelsPerFrame[3]) == 4)
    filenamesPerTraxel = [t.segmentationFilename for t in probabilityGenerator.TraxelsPerFrame[3].values()]
    idsPerTraxel = [t.idInSegmentation for t in probabilityGenerator.TraxelsPerFrame[3].values()]
    assert(idsPerTraxel.count(1) == 2)
    assert(idsPerTraxel.count(2) == 2)
    assert(filenamesPerTraxel.count('tests/multiSegmentationHypothesesTestDataset/segmentation.h5') == 2)
    assert(filenamesPerTraxel.count('tests/multiSegmentationHypothesesTestDataset/segmentationAlt.h5') == 2)

    # build hypotheses graph, check that conflicting traxels are properly detected
    fieldOfView = constructFov(probabilityGenerator.shape,
                               probabilityGenerator.timeRange[0],
                               probabilityGenerator.timeRange[1],
                               [probabilityGenerator.x_scale,
                                probabilityGenerator.y_scale,
                                probabilityGenerator.z_scale])
 
    hypotheses_graph = IlastikHypothesesGraph(
        probabilityGenerator=probabilityGenerator,
        timeRange=probabilityGenerator.timeRange,
        maxNumObjects=1,
        numNearestNeighbors=2,
        fieldOfView=fieldOfView,
        withDivisions=False,
        divisionThreshold=0.1
    )

    assert(hypotheses_graph.countNodes() == 14)
    assert(hypotheses_graph.countArcs() == 23)
    assert(hypotheses_graph._graph.node[(0, 1)]['traxel'].conflictingTraxelIds == [3])
    assert(hypotheses_graph._graph.node[(0, 3)]['traxel'].conflictingTraxelIds == [1])
    assert(hypotheses_graph._graph.node[(0, 2)]['traxel'].conflictingTraxelIds == [4])
    assert(hypotheses_graph._graph.node[(0, 4)]['traxel'].conflictingTraxelIds == [2])
    assert(hypotheses_graph._graph.node[(1, 1)]['traxel'].conflictingTraxelIds == [2, 3])
    assert(hypotheses_graph._graph.node[(1, 2)]['traxel'].conflictingTraxelIds == [1])
    assert(hypotheses_graph._graph.node[(1, 3)]['traxel'].conflictingTraxelIds == [1])

    # track, but check that the right exclusion constraints are present
    hypotheses_graph.insertEnergies()
    trackingGraph = hypotheses_graph.toTrackingGraph()

    assert(len(trackingGraph.model['exclusions']) == 8)
    for exclusionSet in trackingGraph.model['exclusions']:
        assert(len(exclusionSet) == 2)

    # use multiHypoTracking, insert exclusion constraints!
    if mht is not None:
        result = mht.track(trackingGraph.model, {"weights": [10, 10, 500, 500]})
    else:
        return
        # standard dpct cannot handle exclusion constraints yet
        result = dpct.trackFlowBased(trackingGraph.model, {"weights": [10, 10, 500, 500]})

    hypotheses_graph.insertSolution(result)
    # hypotheses_graph.computeLineage()

    numActivePerFrame = {}

    for node in hypotheses_graph.nodeIterator():
        timeframe = node[0]
        if 'value' in hypotheses_graph._graph.node[node]: 
            value = hypotheses_graph._graph.node[node]['value']
        else:
            value = 0 
        numActivePerFrame.setdefault(timeframe, []).append(value) 

    for _, v in numActivePerFrame.iteritems():
        assert(sum(v) == 2)        

    edgeFlow = 0
    for edge in hypotheses_graph.arcIterator():
        if 'value' in hypotheses_graph._graph.edge[edge[0]][edge[1]]:
            edgeFlow += hypotheses_graph._graph.edge[edge[0]][edge[1]]['value']
    assert(edgeFlow == 6)
def test_twoSegmentations():
    # set up ConflictingSegmentsProbabilityGenerator
    ilpOptions = IlastikProjectOptions()
    ilpOptions.divisionClassifierPath = None
    ilpOptions.divisionClassifierFilename = None

    ilpOptions.rawImageFilename = 'tests/multiSegmentationHypothesesTestDataset/Raw.h5'
    ilpOptions.rawImagePath = 'exported_data'
    ilpOptions.rawImageAxes = 'txyzc'

    ilpOptions.labelImageFilename = 'tests/multiSegmentationHypothesesTestDataset/segmentation.h5'

    ilpOptions.objectCountClassifierFilename = 'tests/multiSegmentationHypothesesTestDataset/tracking.ilp'

    additionalLabelImageFilenames = [
        'tests/multiSegmentationHypothesesTestDataset/segmentationAlt.h5'
    ]
    additionalLabelImagePaths = [ilpOptions.labelImagePath]

    probabilityGenerator = ConflictingSegmentsProbabilityGenerator(
        ilpOptions,
        additionalLabelImageFilenames,
        additionalLabelImagePaths,
        useMultiprocessing=False,
        verbose=False)
    probabilityGenerator.fillTraxels(usePgmlink=False)

    assert (len(probabilityGenerator.TraxelsPerFrame[0]) == 4)
    assert (len(probabilityGenerator.TraxelsPerFrame[1]) == 3)
    assert (len(probabilityGenerator.TraxelsPerFrame[2]) == 3)
    assert (len(probabilityGenerator.TraxelsPerFrame[3]) == 4)
    filenamesPerTraxel = [
        t.segmentationFilename
        for t in probabilityGenerator.TraxelsPerFrame[3].values()
    ]
    idsPerTraxel = [
        t.idInSegmentation
        for t in probabilityGenerator.TraxelsPerFrame[3].values()
    ]
    assert (idsPerTraxel.count(1) == 2)
    assert (idsPerTraxel.count(2) == 2)
    assert (filenamesPerTraxel.count(
        'tests/multiSegmentationHypothesesTestDataset/segmentation.h5') == 2)
    assert (filenamesPerTraxel.count(
        'tests/multiSegmentationHypothesesTestDataset/segmentationAlt.h5') == 2
            )

    # build hypotheses graph, check that conflicting traxels are properly detected
    fieldOfView = constructFov(
        probabilityGenerator.shape, probabilityGenerator.timeRange[0],
        probabilityGenerator.timeRange[1], [
            probabilityGenerator.x_scale, probabilityGenerator.y_scale,
            probabilityGenerator.z_scale
        ])

    hypotheses_graph = IlastikHypothesesGraph(
        probabilityGenerator=probabilityGenerator,
        timeRange=probabilityGenerator.timeRange,
        maxNumObjects=1,
        numNearestNeighbors=2,
        fieldOfView=fieldOfView,
        withDivisions=False,
        divisionThreshold=0.1)

    assert (hypotheses_graph.countNodes() == 14)
    assert (hypotheses_graph.countArcs() == 23)
    assert (hypotheses_graph._graph.node[(
        0, 1)]['traxel'].conflictingTraxelIds == [3])
    assert (hypotheses_graph._graph.node[(
        0, 3)]['traxel'].conflictingTraxelIds == [1])
    assert (hypotheses_graph._graph.node[(
        0, 2)]['traxel'].conflictingTraxelIds == [4])
    assert (hypotheses_graph._graph.node[(
        0, 4)]['traxel'].conflictingTraxelIds == [2])
    assert (hypotheses_graph._graph.node[(
        1, 1)]['traxel'].conflictingTraxelIds == [2, 3])
    assert (hypotheses_graph._graph.node[(
        1, 2)]['traxel'].conflictingTraxelIds == [1])
    assert (hypotheses_graph._graph.node[(
        1, 3)]['traxel'].conflictingTraxelIds == [1])

    # track, but check that the right exclusion constraints are present
    hypotheses_graph.insertEnergies()
    trackingGraph = hypotheses_graph.toTrackingGraph()

    assert (len(trackingGraph.model['exclusions']) == 8)
    for exclusionSet in trackingGraph.model['exclusions']:
        assert (len(exclusionSet) == 2)

    # use multiHypoTracking, insert exclusion constraints!
    if mht is not None:
        result = mht.track(trackingGraph.model,
                           {"weights": [10, 10, 500, 500]})
    else:
        result = dpct.trackFlowBased(trackingGraph.model,
                                     {"weights": [10, 10, 500, 500]})

    hypotheses_graph.insertSolution(result)
    # hypotheses_graph.computeLineage()

    numActivePerFrame = {}

    for node in hypotheses_graph.nodeIterator():
        timeframe = node[0]
        if 'value' in hypotheses_graph._graph.node[node]:
            value = hypotheses_graph._graph.node[node]['value']
        else:
            value = 0
        numActivePerFrame.setdefault(timeframe, []).append(value)

    for _, v in numActivePerFrame.iteritems():
        assert (sum(v) == 2)

    edgeFlow = 0
    for edge in hypotheses_graph.arcIterator():
        if 'value' in hypotheses_graph._graph.edge[edge[0]][edge[1]]:
            edgeFlow += hypotheses_graph._graph.edge[edge[0]][edge[1]]['value']
    assert (edgeFlow == 6)