执行类时出现TypeError和AttributeError

2024-09-29 21:52:39 发布

您现在位置:Python中文网/ 问答频道 /正文

我是python类的新手,对它们有基本的了解。我试图从卫星图像中提取特征,以便稍后使用机器学习进行作物类型识别。我正在使用一个名为EOlearn的库,这是我用来计算功能的代码片段:

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.data[self.band_a_feature_name][..., self.band_a_feature_idx]
        band_b = eopatch.data[self.band_b_feature_name][..., self.band_b_feature_idx]
        band_c = eopatch.data[self.band_c_feature_name][..., 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, mcari[..., 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.data[self.band_a_feature_name][..., self.band_a_feature_idx]                  
        band_b = eopatch.data[self.band_b_feature_name][..., self.band_b_feature_idx]

        red_edge = (band_a /band_b)**(-1)

        eopatch.add_feature(FeatureType.DATA, self.feature_name, red_edge[..., 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.data[self.band_a_feature_name][..., self.band_a_feature_idx]
        band_b = eopatch.data[self.band_b_feature_name][..., self.band_b_feature_idx]

        msi = band_a / band_b

        eopatch.add_feature(FeatureType.DATA, self.feature_name, msi[..., np.newaxis])

        return eopatch

调用以下任务时:

MCARI=ModifiedCholorophyllAbsorptionInReflectanceIndex((FeatureType.DATA,'BANDS'),(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')])

我发现以下错误:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-106-8e91d24237c9> in <module>()
     48 
     49 MCARI=ModifiedCholorophyllAbsorptionInReflectanceIndex((FeatureType.DATA,'BANDS'),(FeatureType.DATA, 'MCARI'),
---> 50                                                        [band_names.index('B05'), band_names.index('B04'), band_names.index('B03')])
     51 
     52 CRE=CholorophyllRedEdge((FeatureType.DATA,'BANDS'),(FeatureType.DATA, 'CRE'),

TypeError: __init__() missing 1 required positional argument: 'band_c'
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-105-f8397d36a69f> in <module>()
     54 
     55 MSI=CholorophyllRedEdge((FeatureType.DATA,'BANDS'),(FeatureType.DATA, 'MSI'),
---> 56                                    [band_names.index('B11'), band_names.index('B8A')])
     57 
     58 

<ipython-input-104-595640f5ccf8> in __init__(self, feature_name, band_a, band_b)
     85     def __init__(self, feature_name, band_a, band_b):
     86         self.feature_name = feature_name
---> 87         self.band_a_feature_name = band_a.split('/')[0]
     88         self.band_b_feature_name = band_b.split('/')[0]
     89         self.band_a_feature_idx = int(band_a.split('/')[-1])

AttributeError: 'tuple' object has no attribute 'split'

我想不出我做错了什么。 编辑:我试着这样称呼它:

MCARI=ModifiedCholorophyllAbsorptionInReflectanceIndex((FeatureType.DATA, 'MCARI'), band_names.index('B05'), band_names.index('B04'), band_names.index('B03'))
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-11-cece9d98413a> in <module>()
     47                                    [band_names.index('B8A'), band_names.index('B11')])
     48 
---> 49 MCARI=ModifiedCholorophyllAbsorptionInReflectanceIndex((FeatureType.DATA, 'MCARI'), band_names.index('B05'), band_names.index('B04'), band_names.index('B03'))
     50 
     51 #CRE=CholorophyllRedEdge((FeatureType.DATA,'BANDS'),(FeatureType.DATA, 'CRE'),

<ipython-input-9-595640f5ccf8> in __init__(self, feature_name, band_a, band_b, band_c)
     56     def __init__(self, feature_name, band_a, band_b, band_c):
     57         self.feature_name = feature_name
---> 58         self.band_a_feature_name = band_a.split('/')[0]
     59         self.band_b_feature_name = band_b.split('/')[0]
     60         self.band_c_feature_name = band_c.split('/')[0]

AttributeError: 'int' object has no attribute 'split'

编辑2

class NormalisedDifferenceIndex(EOTask):              #GNDVI, NDRE, NDII, NDVI, NDWI
    """
    Defined Normalised Difference Index (NDI) between two bands A and B as:
    NDI = (A-B)/(A+B).
    
    NDVI  = (B08 - B04) / (B08 + B04)
    NDWI  = (B8A - B12) / (B8A + B12) <==> (NIR - MIR) / (NIR + MIR) 
    GNDVI = (B07 - B03) / (B07 + B03)
    NRDE  = (B8A - B05) / (B8A + B05) <==> (NIR - RE)  / (NIR + RE)
    NDII  = (B8A - B11) / (B8A + B11)

    """
    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.data[self.band_a_feature_name][..., self.band_a_feature_idx]
        band_b = eopatch.data[self.band_b_feature_name][..., self.band_b_feature_idx]

        ndi = (band_a - band_b) / (band_a  + band_b)

        eopatch.add_feature(FeatureType.DATA, self.feature_name, ndi[..., np.newaxis])

        return eopatch

Tags: nameselfdatabandindexnamesdeffeature

热门问题