r-spatial / rgee

Google Earth Engine for R
https://r-spatial.github.io/rgee/
Other
668 stars 146 forks source link

TypeError: 'ComputedObject' object is not callable #312

Open TianyaImpression opened 1 year ago

TianyaImpression commented 1 year ago

At submit an issue, please attached the following information of your rgee session:

library(rgee)

# Initialize the Earth Engine module.
ee_Initialize()

# Print metadata for a DEM dataset.
print(ee$Image('USGS/SRTMGL1_003')$getInfo())

Attach your Python (reticulate) configuration:

library(reticulate)
py_config()

Description

I am trying to convert JS version of GEE codes to rgee codes: JS code: https://code.earthengine.google.com/aa97a56d2df0dfc6796175859da470fe

JS code 160-170 convert rgee codes report error:TypeError: 'ComputedObject' object is not callable

What I Did

library(rgee)

ee_Initialize(drive = T)

#掩膜掉过火和农田区域
#过火区域
firedata = ee$ImageCollection("ESA/CCI/FireCCI/5_1")
years = ee$List$sequence(2001,2008)

byYears = ee$ImageCollection$fromImages(
  years$map(
    ee_utils_pyfunc(function(y){
      return(
        firedata$filter(ee$Filter$calendarRange(y, y, 'year'))$
          select('ConfidenceLevel')$max()$        #选择过火数据的置信度波段,最高置信度
          set('year', y)      #set函数用于覆盖元数据属性
        )
    })
  )
)

byYearsc = ee$ImageCollection$fromImages(
  years$map(
    ee_utils_pyfunc(function(y){
      return(
        firedata$filter(ee$Filter$calendarRange(y, y, 'year'))$
          select('BurnDate')$max()$        
          set('year', y)      #set函数用于覆盖元数据属性
      )
    })
  )
)

filtereq = ee$Filter$equals(leftField = 'system:index', rightField = 'system:index')
join = ee$Join$saveFirst(matchKey = 'match')

datam = ee$ImageCollection(join$apply(byYears,byYearsc,filtereq))$
  map(function(image){
    return(image$addBands(image$get('match')))
  })

mask = function(img){
  img1 = img$select('ConfidenceLevel')$gte(90)   #大于或登玉
  img2 = img$select('BurnDate')$updateMask(img1)
  return(img2)
}

yearsM = datam$map(mask)
ee_print(yearsM)

firearea_o = yearsM$sum()
Map$addLayer(firearea_o)

firearea_0 = firearea_o$eq(0)
firearea = firearea_0$unmask(1)   #获取过火区域掩膜

Map$addLayer(firearea)

#农田区域
cropdata = ee$ImageCollection("COPERNICUS/Landcover/100m/Proba-V-C3/Global")$
  select('discrete_classification')

cb = function(img){
  croparea = img$eq(40)$
    Not()   #哥白尼土地覆被农田像元为40,eq相等的返回1,not:输入非0返回0,其他1,最终结果农田设为0,其余1
  build = img$eq(50)$
    Not()  #建筑设为0,其他1
  Mask = croparea$multiply(build)   #建筑或农田部分为0
  return(Mask)
}

mask = cropdata$map(cb)
Mask = mask$mean()$gt(0)
Mask = firearea$multiply(Mask)
m1 = cropdata$first()
Map$addLayer(m1)

#计算逐月NDVI数据
#计算MOD13Q1数据,250米分辨率,年份:2001-2018
modis = ee$ImageCollection("MODIS/006/MOD13Q1")$
  select('NDVI')
noaa = ee$ImageCollection("NOAA/CDR/AVHRR/NDVI/V5")$
  select('NDVI')

#创建MODIS时间序列
startyear = 2001
endyear = 2018
startmonth = 1
endmonth= 12
startdate = ee$Date$fromYMD(startyear, startmonth, 1)
enddate = ee$Date$fromYMD(endyear+1, endmonth, 1)
years = ee$List$sequence(startyear, endyear)
months = ee$List$sequence(startmonth,endmonth)
Modis = modis$filterDate(startdate, enddate)$
 sort('system:time_start', FALSE)   #按时间对MODIS NDVI影像集排序

#MODIS 月度NDVI
Modism = ee$ImageCollection$fromImages(
  years$map(
    ee_utils_pyfunc(function(y){
      return(
        months$map(
          ee_utils_pyfunc(function(m){
            w = Modis$filter(ee$Filter$calendarRange(y, y, 'year'))$
              filter(ee$Filter$calendarRange(m, m, 'month'))$
              max()$
              multiply(0.0001)$
              updateMask(Mask)
            return(
              w$set('year', y)$
                set('month', m)$
                set('system:time_start',ee$Date$fromYMD(y,m,1)$millis())
            )
          })
        )
      )
    })
  )$flatten()
)

# AVHRR NDVI逐月数据
Noaa = noaa$filterDate(startdate, enddate)$
 sort('system:time_start', FALSE)

#2001-2018年逐月AVHRR NDVI
Noaa2 = ee$ImageCollection$fromImages(
  years$map(
    ee_utils_pyfunc(function(y){
      return(
        months$map(
          ee_utils_pyfunc(function(m){
            w = Noaa$filter(ee$Filter$calendarRange(y, y, 'year'))$
              filter(ee$Filter$calendarRange(m, m, 'month'))$
              max()$
              multiply(0.0001)$
              resample('bicubic')$
              updateMask(Mask)
            return(
              w$set('year', y)$
                set('month', m)$
                set('system:time_start',ee$Date$fromYMD(y,m,1)$millis())
            )
          })
        )
      )
    })
  )$flatten()
)

##1982-2000年逐月AVHRR NDVI
startyear = 1982
endyear = 2000
startmonth = 1
endmonth= 12
startdate = ee$Date$fromYMD(startyear, startmonth, 1)
enddate = ee$Date$fromYMD(endyear+1, endmonth, 1)
years = ee$List$sequence(startyear, endyear)
months = ee$List$sequence(startmonth,endmonth)
Noaa = noaa$filterDate(startdate, enddate)$
  sort('system:time_start', FALSE)   #按时间对MODIS NDVI影像集排序

#AVHRR 月度NDVI
Noaa1 = ee$ImageCollection$fromImages(
  years$map(
    ee_utils_pyfunc(function(y){
      return(
        months$map(
          ee_utils_pyfunc(function(m){
            w = Noaa$filter(ee$Filter$calendarRange(y, y, 'year'))$
              filter(ee$Filter$calendarRange(m, m, 'month'))$
              max()$
              multiply(0.0001)$
              resample('bicubic')$
              updateMask(Mask)
            return(
              w$set('year', y)$
                set('month', m)$
                set('system:time_start',ee$Date$fromYMD(y,m,1)$millis())
            )
          })
        )
      )
    })
  )$flatten()
)

#计算逐月中值
sm = 1
em = 12
month_med = ee$List$sequence(1, 12)

#The following code runs an error:Error in py_call_impl(callable, dots$args, dots$keywords) : 
  TypeError: 'ComputedObject' object is not callable
Modis_med = ee$ImageCollection$fromImages(month_med$map(ee_utils_pyfunc(
  function(f){
    month1 = Modism$filter(ee$Filter$eq('month', f))
    return(month1$reduce(ee$Reducer$median())$
             rename('MMD')$
             set('month', f))
  }
)))

#The code inside the function runs without error:
month1 = Modism$filter(ee$Filter$eq('month', 1))
med = month1$reduce(ee$Reducer$median())$
  rename('MMD')$
  set('month', 1)
Map$addLayer(med)

image

I suspect that there is a bug when rgee executes map and reduce, or is there any other reason why the rgee version code cannot be executed?

Thank you for your help!

csaybar commented 1 year ago

Hi @TianyaImpression, thanks for sharing a reproducible example. It's a very weird bug that occurs on the Python side (I will report it on the weekend!). Here is a solution, so basically avoid using 'f' as an argument.

Replace:

month_med = ee$List$sequence(1, 12)

#The following code runs an error:Error in py_call_impl(callable, dots$args, dots$keywords) : 
  TypeError: 'ComputedObject' object is not callable
Modis_med = ee$ImageCollection$fromImages(month_med$map(ee_utils_pyfunc(
  function(f){
    month1 = Modism$filter(ee$Filter$eq('month', f))
    return(month1$reduce(ee$Reducer$median())$
             rename('MMD')$
             set('month', f))
  }
)))

By:

monthly_reducer <- function(x) {
  month1 <- Modism$filter(ee$Filter$eq('month', x))
  month1$reduce(ee$Reducer$median())$
    rename('MMD')$
    set('month', x)
}

Modis_med <- ee$List$sequence(1, 12) %>% 
  ee$List$map(
    ee_utils_pyfunc(monthly_reducer)
  ) %>% 
  ee$ImageCollection$fromImages()

UPDATE: No, this bug happens on the rgee side :face_with_spiral_eyes: , basically it's a dumb mistake in the ee_utils_pyfunc, I will solve it ASAP! Thank you!

TianyaImpression commented 1 year ago

Hi @TianyaImpression, thanks for sharing a reproducible example. It's a very weird bug that occurs on the Python side (I will report it on the weekend!). Here is a solution, so basically avoid using 'f' as an argument.

Replace:

month_med = ee$List$sequence(1, 12)

#The following code runs an error:Error in py_call_impl(callable, dots$args, dots$keywords) : 
  TypeError: 'ComputedObject' object is not callable
Modis_med = ee$ImageCollection$fromImages(month_med$map(ee_utils_pyfunc(
  function(f){
    month1 = Modism$filter(ee$Filter$eq('month', f))
    return(month1$reduce(ee$Reducer$median())$
             rename('MMD')$
             set('month', f))
  }
)))

By:

monthly_reducer <- function(x) {
  month1 <- Modism$filter(ee$Filter$eq('month', x))
  month1$reduce(ee$Reducer$median())$
    rename('MMD')$
    set('month', x)
}

Modis_med <- ee$List$sequence(1, 12) %>% 
  ee$List$map(
    ee_utils_pyfunc(monthly_reducer)
  ) %>% 
  ee$ImageCollection$fromImages()

UPDATE: No, this bug happens on the rgee side 😵‍💫 , basically it's a dumb mistake in the ee_utils_pyfunc, I will solve it ASAP! Thank you!

Thank you for your reply!

I replaced "f" for "x", the code is ok!