Ejemplo n.º 1
0
    def __init__(self,
                 create_anomaly_model_func,
                 patches,
                 cell_size=0.2,
                 fake=False):
        """ Create a new spatial bin anomaly model

        Args:
            create_anomaly_model_func (func): Method that returns an anomaly model
            patches (PatchArray): The patches are needed to get the rasterization
            cell_size (float): Width and height of spatial bin in meter
        """
        AnomalyModelBase.__init__(self)
        self.CELL_SIZE = cell_size
        self.KEY = "%.2f" % self.CELL_SIZE
        if fake: self.KEY = "fake_" + self.KEY
        self.CREATE_ANOMALY_MODEL_FUNC = create_anomaly_model_func
        self.FAKE = fake

        # Get extent
        x_min, y_min, x_max, y_max = patches.get_extent(cell_size, fake=fake)

        # Create the bins
        bins_y = np.arange(y_min, y_max, cell_size)
        bins_x = np.arange(x_min, x_max, cell_size)

        shape = (len(bins_y), len(bins_x))

        # Create the grid
        raster = np.zeros(shape, dtype=object)

        for v, y in enumerate(bins_y):
            for u, x in enumerate(bins_x):
                b = box(
                    x, y, x + cell_size, y +
                    cell_size)  #Point(x + cell_size / 2, y + cell_size / 2)#
                b.u = u
                b.v = v
                b.patches = list()
                raster[v, u] = b

        # Create a search tree of spatial boxes
        self._grid = STRtree(raster.ravel().tolist())

        m = create_anomaly_model_func()
        self.NAME = "SpatialBin/%s/%s" % (m.__class__.__name__.replace(
            "AnomalyModel", ""), self.KEY)
    def __init__(self, create_anomaly_model_func, cell_size=0.2, fake=False):
        """ Create a new spatial bin anomaly model

        Args:
            create_anomaly_model_func (func): Method that returns an anomaly model
            cell_size (float): Width and height of spatial bin in meter
        """
        AnomalyModelBase.__init__(self)
        self.CELL_SIZE = cell_size
        self.KEY = "%.2f" % self.CELL_SIZE
        if fake: self.KEY = "fake_" + self.KEY
        self.CREATE_ANOMALY_MODEL_FUNC = create_anomaly_model_func
        self.FAKE = fake

        m = create_anomaly_model_func()
        self.NAME = "SpatialBin/%s/%s" % (m.__class__.__name__.replace(
            "AnomalyModel", ""), self.KEY)
Ejemplo n.º 3
0
    def __init__(self,
                 initial_normal_features=1000,
                 threshold_learning=300,
                 threshold_classification=5,
                 pruning_parameter=0.3):
        AnomalyModelBase.__init__(self)
        self.NAME += "/%i/%i/%.2f" % (initial_normal_features,
                                      threshold_learning, pruning_parameter)

        assert 0 < pruning_parameter < 1, "Pruning parameter out of range (0 < η < 1)"

        self.initial_normal_features = initial_normal_features  # See reference algorithm variable N
        self.threshold_learning = threshold_learning  # See reference algorithm variable α
        self.threshold_classification = threshold_classification  # See reference algorithm variable β
        self.pruning_parameter = pruning_parameter  # See reference algorithm variable η

        self.balanced_distribution = None  # Array containing all the "normal" samples

        self._mean = None  # Mean
        self._covI = None  # Inverse of covariance matrix
Ejemplo n.º 4
0
    def __generate_model__(self, patches, silent=False):
        # Ensure locations are calculated
        assert patches.contains_features, "Can only compute patch locations if there are patches"
        assert patches.contains_locations, "Can only compute patch locations if there are locations calculated"

        # Check if cell size rasterization is already calculated
        if not self.KEY in patches.contains_bins.keys(
        ) or not patches.contains_bins[self.KEY]:
            patches.calculate_rasterization(self.CELL_SIZE, self.FAKE)

        patches_flat = patches.ravel()

        raster = patches.rasterizations[self.KEY]

        # Empty grid that will contain the model for each bin
        self.models = np.empty(shape=raster.shape, dtype=object)
        models_created = 0

        with tqdm(desc="Generating models",
                  total=self.models.size,
                  file=sys.stderr) as pbar:
            for bin in np.ndindex(raster.shape):
                indices = raster[bin]

                if len(indices) > 0:
                    model_input = AnomalyModelBase.filter_training(
                        self, patches_flat[indices])
                    if model_input.size > 0:
                        # Create a new model
                        model = self.CREATE_ANOMALY_MODEL_FUNC(
                        )  # Instantiate a new model
                        model.__generate_model__(
                            model_input, silent=silent
                        )  # The model only gets flattened features
                        self.models[bin] = model  # Store the model
                        models_created += 1
                        pbar.set_postfix({"Models": models_created})
                pbar.update()
        return True
 def __init__(self):
     AnomalyModelBase.__init__(self)
     self._var       = None # Covariance matrix Ʃ
     self._varI      = None # Inverse of covariance matrix Ʃ⁻¹
     self._mean      = None # Mean μ
Ejemplo n.º 6
0
 def __init__(self):
     AnomalyModelBase.__init__(self)
     self._var = None  # Variance σ²
     self._mean = None  # Mean μ