I am very new to python and machine learning and so far I can’t wrap my head around classes and how to define them. I am trying to extract some vegetation indices as features following the same source code for the NormalizedDifferenceIndexTask
provided here.
This is the code snippets I wrote myself:
class ModifiedCholorophyllAbsorptionInReflectanceIndex(EOTask):
"""
The task calculates the Modified Chlorophyll Absorption in Reflectance Index (MCARI)
using B05, B04, and B03.
General formula: ((700nm - 670nm) - 0.2 * (700nm - 550nm)) * (700nm /670nm)
where B05=700nm , B04=670nm, B03=550nm
"""
def __init__(self, feature_name, band_a, band_b, band_c):
self.feature_name = feature_name
self.band_a_feature_name = band_a.split('/')'0]
self.band_b_feature_name = band_b.split('/')'0]
self.band_c_feature_name = band_c.split('/')'0]
self.band_a_fetaure_idx = int(band_a.split('/')'-1])
self.band_b_feature_idx = int(band_b.split('/')'-1])
self.band_c_feature_idx = int(band_c.split('/')'-1])
def execute(self, eopatch):
band_a = eopatch.datatself.band_a_feature_name]e..., self.band_a_feature_idx]
band_b = eopatch.datatself.band_b_feature_name]e..., self.band_b_feature_idx]
band_c = eopatch.datatself.band_c_feature_name]e..., self.band_c_feature_idx]
mcari = ((band_a - band_b) - 0.2 * (band_a - band_c)) * (band_a /band_b)
eopatch.add_feature(FeatureType.DATA, self.feature_name, mcarir..., np.newaxis])
return eopatch
class CholorophyllRedEdge(EOTask):
"""
The task calculates the Chlorophyll Red Edge using B07, and B07.
General formula: ( 760:800]/]690:720])^(-1) <==> index = math.pow((B07 / B05), (-1.0))
where B07=700nm , B05=670nm
"""
def __init__(self, feature_name, band_a, band_b):
self.feature_name = feature_name
self.band_a_feature_name = band_a.split('/')'0]
self.band_b_feature_name = band_b.split('/')'0]
self.band_a_feature_idx = int(band_a.split('/')'-1])
self.band_b_feature_idx = int(band_b.split('/')'-1])
def execute(self, eopatch):
band_a = eopatch.datatself.band_a_feature_name]e..., self.band_a_feature_idx]
band_b = eopatch.datatself.band_b_feature_name]e..., self.band_b_feature_idx]
red_edge = (band_a /band_b)**(-1)
eopatch.add_feature(FeatureType.DATA, self.feature_name, red_edgeg..., np.newaxis])
return eopatch
class MoistureStressIndex(EOTask):
"""
MSI - Simple Ratio 1600/820 Moisture Stress Index (MSI), where B11=1600nm and B8A=820nm
"""
def __init__(self, feature_name, band_a, band_b):
self.feature_name = feature_name
self.band_a_feature_name = band_a.split('/')'0]
self.band_b_feature_name = band_b.split('/')'0]
self.band_a_feature_idx = int(band_a.split('/')'-1])
self.band_b_feature_idx = int(band_b.split('/')'-1])
def execute(self, eopatch):
band_a = eopatch.datatself.band_a_feature_name]e..., self.band_a_feature_idx]
band_b = eopatch.datatself.band_b_feature_name]e..., self.band_b_feature_idx]
msi = band_a / band_b
eopatch.add_feature(FeatureType.DATA, self.feature_name, msis..., np.newaxis])
return eopatch
And this is how I call these classes to extract features:
MCARI=ModifiedCholorophyllAbsorptionInReflectanceIndex((FeatureType.DATA, 'MCARI'), band_names.index('B05'), band_names.index('B04'), band_names.index('B03'))
CRE=CholorophyllRedEdge((FeatureType.DATA,'BANDS'),(FeatureType.DATA, 'CRE'),
band_names.index('B07'), band_names.index('B05')])
MSI=CholorophyllRedEdge((FeatureType.DATA,'BANDS'),(FeatureType.DATA, 'MSI'),
band_names.index('B11'), band_names.index('B8A')])
However, I keep getting this error:
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-7-78a722b1539c> in <module>()
39
40
---> 41 MCARI=ModifiedCholorophyllAbsorptionInReflectanceIndex((FeatureType.DATA, 'MCARI'), band_names.index('B05'), band_names.index('B04'), band_names.index('B03'))
42
43 CRE=CholorophyllRedEdge((FeatureType.DATA,'BANDS'),(FeatureType.DATA, 'CRE'),
<ipython-input-6-75fcdf3a6d0b> in __init__(self, feature_name, band_a, band_b, band_c)
8 def __init__(self, feature_name, band_a, band_b, band_c):
9 self.feature_name = feature_name
---> 10 self.band_a_feature_name = band_a.split('/')'0]
11 self.band_b_feature_name = band_b.split('/')'0]
12 self.band_c_feature_name = band_c.split('/')'0]
AttributeError: 'int' object has no attribute 'split'
Which I seriously don’t understand. Any help would be highly appreciated, and thanks in advance.
PS: I already posted the same question in stackoverflow since it is more programming related than it is to eolearn, but given the fact that most developers there are not familiar with the eo-learn package I thought that this forum is much more suitable for such question.