Exemplo n.º 1
0
    def resampleModelSet(self):
        """
    FIXME
    """
        unchangedSamplePercentage = "%g * nrCellsChanged / nrCellsUnchanged" % (float(self.modelSet()) / 100.0)
        modelSetSampleSize = "nrCells"

        if self.modelSetSampleSizeIsSet():
            modelSetSampleSize = self.modelSetSampleSize()

        script = u"""\
binding
  split = "%s";
  modelSetClass = %d;
  modelSetSampleClass = %d;
  landUse1 = "%s";
  landUse2 = "%s";

initial
  changed = split == modelSetClass and landUse1 != landUse2;
  unchanged = split == modelSetClass and landUse1 == landUse2;
  nrCellsChanged = maptotal(scalar(changed));
  nrCellsUnchanged = maptotal(scalar(unchanged));
  nrCells = nrCellsChanged + nrCellsUnchanged;

  # Take a subset of the unchanged cells in the model set.
  unchangedSamplePercentage = %s;
  order = areaorder(uniform(unchanged), landUse1);
  fractions = order / areamaximum(order, landUse1);
  unchanged = cover(fractions <= unchangedSamplePercentage, 0);

  # Model set sample contains all changed cells from the model set and the
  # subset of the unchanged cells from the model set.
  sample = changed or unchanged;
  nrCells = maptotal(scalar(sample));

  # Create a sample of the model set sample just created.
  sampleSize = %s;
  samplePercentage = sampleSize / nrCells;
  sample = cover(uniform(sample) <= samplePercentage, 0);

  report split = if(sample, modelSetSampleClass, split);
""" % (
            self.splitMapFilename(),
            constants.modelSetClass,
            constants.modelSetSampleClass,
            self.landUseMap1Filename(),
            self.landUseMap2Filename(),
            unchangedSamplePercentage,
            modelSetSampleSize,
        )

        try:
            utils.executePCRCalcScript(script, logFilename=self.logFilename())
        except AssertionError, exception:
            raise
Exemplo n.º 2
0
  def createSelectionMaps(self):
    """
    Each categorical variable can contain a selection specification. If so,
    create a copy of the variable which contains the selected classes. Not
    selected classes get a value -9999.
    """

    for variable in self.categoricalVariables():
      assert isinstance(variable, types.ListType)
      assert len(variable) == 3

      if variable[1]:
        assert not os.path.exists(variable[2])

        # Reclassify the original map.
        selection = []
        for classId in variable[1]:
          selection.append("\"%s\" == %s" % (variable[0], classId))

        script = """\
binding
  input = "%s";
  result = "%s";

initial
  report result = if(%s, input, nominal(-9999));
""" % (variable[0], variable[2], " or ".join(selection))

        try:
          utils.executePCRCalcScript(script, logFilename=self.logFilename())
        except AssertionError, exception:
          raise
        except Exception, exception:
          utils.raiseException(u"error reclassifying classification variable",
              exception)

        assert os.path.exists(variable[2])
Exemplo n.º 3
0
    def createSplitMap(self):
        """
    FIXME
    """

        # Default selection mask is the whole raster of course.
        mask = "1"

        if self.maskIsSet():
            mask = '"%s"' % (self.mask())

        script = u"""\
binding
  mask = %s;
  landUse1 = "%s";
  landUse2 = "%s";
  valset = "%s";
  split = "%s";

initial
  selection = mask and defined(landUse1) and defined(landUse2);
  report split = if(selection, if(valset, nominal(%d), nominal(%d)));
""" % (
            mask,
            self.landUseMap1Filename(),
            self.landUseMap2Filename(),
            self.__validationMapFilename(),
            self.splitMapFilename(),
            constants.validationSetClass,
            constants.modelSetClass,
        )

        try:
            utils.executePCRCalcScript(script, logFilename=self.logFilename())
        except AssertionError, exception:
            raise
Exemplo n.º 4
0
    def createValidationSetMapUsingPercentage(self, percentage):
        """
    FIXME
    """

        # Default selection mask is the whole raster of course.
        mask = "1"

        if self.maskIsSet():
            mask = '"%s"' % (self.mask())

        script = u"""\
binding
  mask = %s;
  landUse1 = "%s";
  landUse2 = "%s";
  percentage = %g;
  valset = "%s";

initial
  selection = mask and defined(landUse1) and defined(landUse2);
  order = areaorder(uniform(selection), landUse1);
  fractions = order / areamaximum(order, landUse1);
  report valset = fractions <= percentage / 100;
""" % (
            mask,
            self.landUseMap1Filename(),
            self.landUseMap2Filename(),
            percentage,
            self.__validationMapFilename(),
        )

        try:
            utils.executePCRCalcScript(script, logFilename=self.logFilename())
        except AssertionError, exception:
            raise
Exemplo n.º 5
0
  def determineFractionsInModelSetSample(self):
    """
    Determines the fractions of cells in the model set which have changed and
    cells which have not changed compared to the model set.

    The model set sample is a subset of the model set. For the regression
    analyses it is important to now the fraction between the amount of cells
    which have changed in the model set sample compared to the ones in the
    total model set. The same goes for the unchanged cells.

    The way the sample of the model set is taken changes the fraction of
    changed to unchanged cells. The regression analyses needs to compensate
    for this.
    """
    fractionUnchangedCellsRasterFilename = os.tempnam(self.tempDirectoryName())
    fractionChangedCellsRasterFilename = os.tempnam(self.tempDirectoryName())
    script = u"""\
binding
  split = "%s";
  modelSetClass = %d;
  modelSetSampleClass = %d;
  landUse1 = "%s";
  landUse2 = "%s";
  fractionUnchangedCells = "%s";
  fractionChangedCells = "%s";

initial
  modelSetSample = split == modelSetSampleClass;
  modelSet = split == modelSetClass or split == modelSetSampleClass;

  unchangedCells = landUse1 == landUse2;
  unchangedCellsInModelSetSample = modelSetSample and unchangedCells;
  unchangedCellsInModelSet = modelSet and unchangedCells;

  changedCells = landUse1 != landUse2;
  changedCellsInModelSetSample = modelSetSample and changedCells;
  changedCellsInModelSet = modelSet and changedCells;

  # Use if to make false cells missing values.
  report fractionUnchangedCells = maparea(if(unchangedCellsInModelSetSample, boolean(1))) / maparea(if(unchangedCellsInModelSet, boolean(1)));
  report fractionChangedCells = maparea(if(changedCellsInModelSetSample, boolean(1))) / maparea(if(changedCellsInModelSet, boolean(1)));
""" % (self.splitMapFilename(),
         constants.modelSetClass,
         constants.modelSetSampleClass,
         self.landUseMap1Filename(), self.landUseMap2Filename(),
         fractionUnchangedCellsRasterFilename,
         fractionChangedCellsRasterFilename)

    try:
      utils.executePCRCalcScript(script, logFilename=self.logFilename())

      def determineFraction(resultFilename):
        assert os.path.exists(resultFilename)
        result = utils.rasterMaximum(resultFilename)
        assert result != "mv"
        result = float(result)
        utils.testNumberWithinRange(result, 0.0, 1.0)
        return result

      self.__fractionUnchangedCellsInModelSetSample = determineFraction(
         fractionUnchangedCellsRasterFilename)
      os.remove(fractionUnchangedCellsRasterFilename)

      self.__fractionChangedCellsInModelSetSample = determineFraction(
         fractionChangedCellsRasterFilename)
      os.remove(fractionChangedCellsRasterFilename)
    except AssertionError, exception:
      raise