<commit_before>from __future__ import print_function, absolute_import, nested_scopes, generators, division, with_statement, unicode_literals
import copy
import logging
import h5py
import os
import hytra.core.mergerresolver
from hytra.core.jsongraph import JsonTrackingGraph

def getLogger():
    ''' logger to be used in this module '''
    return logging.getLogger(__name__)

class JsonMergerResolver(hytra.core.mergerresolver.MergerResolver):
    '''
    Specialization of merger resolving that reads raw data and segmentation from HDF5 files,
    computes features on demand, and that uses JSON files as input/output for graph and tracking result.
    '''
    def __init__(self,
                 jsonTrackingGraph,
                 label_image_filename,
                 label_image_path,
                 out_label_image,
                 raw_filename,
                 raw_path,
                 raw_axes,
                 pluginPaths=[os.path.abspath('../hytra/plugins')],
                 verbose=False):
        super(JsonMergerResolver, self).__init__(pluginPaths, verbose)

        # copy model and result because we will modify it here
        assert(isinstance(jsonTrackingGraph, JsonTrackingGraph))
        assert(jsonTrackingGraph.model is not None and len(jsonTrackingGraph.model) > 0)
        assert(jsonTrackingGraph.result is not None and len(jsonTrackingGraph.result) > 0)
        self.model = copy.copy(jsonTrackingGraph.model)
        self.result = copy.copy(jsonTrackingGraph.result)

        assert(self.result['detectionResults'] is not None)
        assert(self.result['linkingResults'] is not None)
        self.withDivisions = self.result['divisionResults'] is not None

        self.label_image_filename = label_image_filename
        self.label_image_path = label_image_path
        self.out_label_image = out_label_image
        self.raw_filename = raw_filename
        self.raw_path = raw_path
        self.raw_axes = raw_axes
        self.pluginManager.setImageProvider('LocalImageLoader')
        self.imageProvider = self.pluginManager.getImageProvider()
    

    def _computeObjectFeatures(self, timesteps):
        """
        Computes object features for all nodes in the resolved graph because they
        are needed for the transition classifier or to compute new distances.

        **returns:** a dictionary of feature-dicts per node
        """
        rawImages = {}
        labelImages = {}
        for t in timesteps:
            rawImages[t] = self.imageProvider.getImageDataAtTimeFrame(self.raw_filename, self.raw_path, self.raw_axes, int(t))
            labelImages[t] = self.imageProvider.getLabelImageForFrame(self.label_image_filename, self.label_image_path, int(t))
            self.relabelMergers(labelImages[t], int(t))

        getLogger().info("Computing object features")
        objectFeatures = {}<commit_msg>JsonMergerResolver: do something useful even if no raw data is given<commit_after>from __future__ import print_function, absolute_import, nested_scopes, generators, division, with_statement, unicode_literals
import copy
import logging
import h5py
import numpy as np
import os
import hytra.core.mergerresolver
from hytra.core.jsongraph import JsonTrackingGraph

def getLogger():
    ''' logger to be used in this module '''
    return logging.getLogger(__name__)

class JsonMergerResolver(hytra.core.mergerresolver.MergerResolver):
    '''
    Specialization of merger resolving that reads raw data and segmentation from HDF5 files,
    computes features on demand, and that uses JSON files as input/output for graph and tracking result.
    '''
    def __init__(self,
                 jsonTrackingGraph,
                 label_image_filename,
                 label_image_path,
                 out_label_image,
                 raw_filename,
                 raw_path,
                 raw_axes,
                 pluginPaths=[os.path.abspath('../hytra/plugins')],
                 verbose=False):
        super(JsonMergerResolver, self).__init__(pluginPaths, verbose)

        # copy model and result because we will modify it here
        assert(isinstance(jsonTrackingGraph, JsonTrackingGraph))
        assert(jsonTrackingGraph.model is not None and len(jsonTrackingGraph.model) > 0)
        assert(jsonTrackingGraph.result is not None and len(jsonTrackingGraph.result) > 0)
        self.model = copy.copy(jsonTrackingGraph.model)
        self.result = copy.copy(jsonTrackingGraph.result)

        assert(self.result['detectionResults'] is not None)
        assert(self.result['linkingResults'] is not None)
        self.withDivisions = self.result['divisionResults'] is not None

        self.label_image_filename = label_image_filename
        self.label_image_path = label_image_path
        self.out_label_image = out_label_image
        self.raw_filename = raw_filename
        self.raw_path = raw_path
        self.raw_axes = raw_axes
        self.pluginManager.setImageProvider('LocalImageLoader')
        self.imageProvider = self.pluginManager.getImageProvider()
    

    def _computeObjectFeatures(self, timesteps):
        """
        Computes object features for all nodes in the resolved graph because they
        are needed for the transition classifier or to compute new distances.

        **returns:** a dictionary of feature-dicts per node
        """
        rawImages = {}
        labelImages = {}
        for t in timesteps:
            if self.raw_filename is not None:
                rawImages[str(t)] = self.imageProvider.getImageDataAtTimeFrame(self.raw_filename, self.raw_path, self.raw_axes, int(t))
            else:
                rawImages[str(t)] = self.imageProvider.getLabelImageForFrame(self.label_image_filename, self.label_image_path, int(t)).astype(np.float32)
            labelImages[str(t)] = self.imageProvider.getLabelImageForFrame(self.label_image_filename, self.label_image_path, int(t))
            self.relabelMergers(labelImages[str(t)], int(t))

        getLogger().info("Computing object features")
        objectFeatures = {}
{'fraction_non_alphanumeric': 0.07783837382250867, 'fraction_numerical': 0.0016526194017517765, 'mean_word_length': 2.9324236517218973, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5778379', 'n_tokens_mistral': 1647, 'n_tokens_neox': 1602, 'n_words': 408}