Rename features in EOPatch

I am not sure if I get this class right or not but please bear with me. So I want to rename one of my features from VALID_DATA to IS_DATA. the code source RenameFeature is provided as follows:

class RenameFeature(EOTask):
    """Renames one or multiple features from the given EOPatch.
    """
    def __init__(self, features):
        """
        :param features: A collection of features to be renamed.
        :type features: an object supported by the :class:`FeatureParser<eolearn.core.utilities.FeatureParser>`
        """
        self.feature_gen = self._parse_features(features, new_names=True)

[docs]    def execute(self, eopatch):
        """Returns the EOPatch with renamed features.

        :param eopatch: input EOPatch
        :type eopatch: EOPatch
        :return: input EOPatch with the renamed features
        :rtype: EOPatch
        """
        for feature_type, feature_name, new_feature_name in self.feature_gen(eopatch):
            eopatch[feature_type][new_feature_name] = eopatch[feature_type][feature_name]
            del eopatch[feature_type][feature_name]

        return eopatch

So from what I see here, you can only specify the feature that you want to rename (and not the new name). So I thought that it will generate a random name or something (which I don’t mind).
My code is the following:

eopatch=EOPatch.load('./New_L2A_eopatch_folder/eopatch_0')
EOPatch_0=RenameFeature((FeatureType.MASK,'VALID_DATA'))         # New_EOPatch
EOPatch_0=EOPatch_0.execute(eopatch)

# TASK FOR SAVING TO OUTPUT 
save3 = SaveTask('New_L2A_eopatch_folder/eopatch_0',overwrite_permission=OverwritePermission.OVERWRITE_PATCH)
EOPatch_0=save3.execute(EOPatch_0) 
       

And these are the before and after (respectively) Eopatches:

EOPatch(
  data: {
    BANDS: numpy.ndarray(shape=(28, 500, 500, 12), dtype=float32)
    CLP: numpy.ndarray(shape=(28, 500, 500, 1), dtype=uint8)
    NDVI: numpy.ndarray(shape=(28, 500, 500, 1), dtype=float32)
    NDWI: numpy.ndarray(shape=(28, 500, 500, 1), dtype=float32)
    NORM: numpy.ndarray(shape=(28, 500, 500, 1), dtype=float32)
  }
  mask: {
    AdaptiveCLM: numpy.ndarray(shape=(28, 500, 500, 1), dtype=bool)
    CLM: numpy.ndarray(shape=(28, 500, 500, 1), dtype=uint8)
    IS_DATA: numpy.ndarray(shape=(28, 500, 500, 1), dtype=uint8)
    VALID_DATA: numpy.ndarray(shape=(28, 500, 500, 1), dtype=bool)
  }
  scalar: {}
  label: {}
  vector: {}
  data_timeless: {
    ARGMAX_B4: numpy.ndarray(shape=(500, 500, 1), dtype=int64)
    ARGMAX_NDVI: numpy.ndarray(shape=(500, 500, 1), dtype=int64)
    ARGMAX_NDVI_SLOPE: numpy.ndarray(shape=(500, 500, 1), dtype=uint8)
    ARGMIN_B4: numpy.ndarray(shape=(500, 500, 1), dtype=int64)
    ARGMIN_NDVI: numpy.ndarray(shape=(500, 500, 1), dtype=int64)
    ARGMIN_NDVI_SLOPE: numpy.ndarray(shape=(500, 500, 1), dtype=uint8)
    STF: numpy.ndarray(shape=(500, 500, 20), dtype=float32)
  }
  mask_timeless: {
    LULC: numpy.ndarray(shape=(500, 500, 1), dtype=uint8)
    VALID_COUNT: numpy.ndarray(shape=(500, 500, 1), dtype=int64)
  }
  scalar_timeless: {}
  label_timeless: {}
  vector_timeless: {}
  meta_info: {
    maxcc: 0.1
    service_type: 'processing'
    size_x: 500
    size_y: 500
    time_difference: datetime.timedelta(0, 7200)
    time_interval: ('2019-01-01T00:00:00', '2019-12-31T23:59:59')
  }
  bbox: BBox(((480000.0, 4060000.0), (485000.0, 4065000.0)), crs=CRS('32632'))
  timestamp: [datetime.datetime(2019, 1, 19, 10, 20, 45), ..., datetime.datetime(2019, 12, 25, 10, 20, 40)], length=28
)
EOPatch(
  data: {
    BANDS: numpy.ndarray(shape=(28, 500, 500, 12), dtype=float32)
    CLP: numpy.ndarray(shape=(28, 500, 500, 1), dtype=uint8)
    NDVI: numpy.ndarray(shape=(28, 500, 500, 1), dtype=float32)
    NDWI: numpy.ndarray(shape=(28, 500, 500, 1), dtype=float32)
    NORM: numpy.ndarray(shape=(28, 500, 500, 1), dtype=float32)
  }
  mask: {
    AdaptiveCLM: numpy.ndarray(shape=(28, 500, 500, 1), dtype=bool)
    CLM: numpy.ndarray(shape=(28, 500, 500, 1), dtype=uint8)
    IS_DATA: numpy.ndarray(shape=(28, 500, 500, 1), dtype=uint8)
  }
  scalar: {}
  label: {}
  vector: {}
  data_timeless: {
    ARGMAX_B4: numpy.ndarray(shape=(500, 500, 1), dtype=int64)
    ARGMAX_NDVI: numpy.ndarray(shape=(500, 500, 1), dtype=int64)
    ARGMAX_NDVI_SLOPE: numpy.ndarray(shape=(500, 500, 1), dtype=uint8)
    ARGMIN_B4: numpy.ndarray(shape=(500, 500, 1), dtype=int64)
    ARGMIN_NDVI: numpy.ndarray(shape=(500, 500, 1), dtype=int64)
    ARGMIN_NDVI_SLOPE: numpy.ndarray(shape=(500, 500, 1), dtype=uint8)
    STF: numpy.ndarray(shape=(500, 500, 20), dtype=float32)
  }
  mask_timeless: {
    LULC: numpy.ndarray(shape=(500, 500, 1), dtype=uint8)
    VALID_COUNT: numpy.ndarray(shape=(500, 500, 1), dtype=int64)
  }
  scalar_timeless: {}
  label_timeless: {}
  vector_timeless: {}
  meta_info: {
    maxcc: 0.1
    service_type: 'processing'
    size_x: 500
    size_y: 500
    time_difference: datetime.timedelta(0, 7200)
    time_interval: ('2019-01-01T00:00:00', '2019-12-31T23:59:59')
  }
  bbox: BBox(((475000.0, 4060000.0), (480000.0, 4065000.0)), crs=CRS('32632'))
  timestamp: [datetime.datetime(2019, 1, 19, 10, 20, 45), ..., datetime.datetime(2019, 12, 25, 10, 20, 40)], length=28
)

It basically deleted it !

Hi @rim.sleimi,

To rename a mask (say VALID_DATA in your example) in your Eopatch, this is how you would proceed:

from eolearn.core.constants import FeatureType

# Load patch
eopatch=EOPatch.load('./eopatches/eopatch_0/')

# Rename feature
eopatch.rename_feature(FeatureType.MASK, "VALID_DATA", "NEW_NAME")

However, take care not to rename your feature to an existing one otherwise the existing feature will be overwritten!

This documentation page should help you more than the source code to understand how the function works.

Cheers,

1 Like