Q: Getting maximum value of a cumulativeCost Image in Earth Engine I'm trying to get the max value of a generated cumulativeCost Image in Earth Engine, via the Python API. I thought reduceRegion should work, but it dies.
My relevant code:
malawi = ee.FeatureCollection('USDOS/LSIB/2017').filter(ee.Filter.inList('COUNTRY_NA', ["Malawi"]))  
malawi_geometry = malawi.geometry()  

nl_raw = ee.ImageCollection('NOAA/DMSP-OLS/NIGHTTIME_LIGHTS')
nl_three_newest = nl_raw.sort('system:time_start', False).limit(3)
nl_stable_lights_only = nl_three_newest.select('stable_lights')
nl_quality_mosaic = nl_stable_lights_only.qualityMosaic('stable_lights')
nl_clipped_to_malawi = nl_quality_mosaic.clip(malawi_geometry)
has_light_at_all = nl_clipped.gte(1)
no_light_at_all = nl_clipped.lt(1)
has_light_at_all_mask = ee.Image().toByte().paint(has_light_at_all.geometry(), 1).updateMask(has_light_at_all)
max_dist = 70 * 1000
dist_to_light_at_all_from_light_mask = no_light_at_all.cumulativeCost(has_light_at_all_mask, max_dist)
dist_to_light_at_all_from_light_mask_max = dist_to_light_at_all_from_light_mask.reduceRegion(
    reducer=ee.Reducer.max(),
    geometry=malawi_geometry,
    scale=30,
    maxPixels=10**9
)
print(dist_to_light_at_all_from_light_mask_max.getInfo())

You can check the whole code with comments and displayed maps in Python here:
https://colab.research.google.com/drive/1z_3_a1UfVwEy1jJEd2Ms1xxw-76HXBPn?usp=sharing
Or in the web editor in Javascript:
https://code.earthengine.google.com/?scriptPath=users%2Fattilastevekopias%2Fdebug%3Anight-lights-debug
The problem is that the final print dies with this message:
HttpError 400 when requesting https://earthengine.googleapis.com/v1alpha/projects/earthengine-legacy/value:compute?prettyPrint=false&alt=json returned "EEException: Object too large (133984872 bytes)."

What I've tried:

*

*I've checked all the intermediary ImageCollections and Images, put them on a map and they look exactly like they should.

*I've tried the exact same reduceRegion function with other images, like the nl_quality_mosaic_max or the nl_clipped_to_malawi_max (the results of the previous steps before the cumulativeCost) and with everything else it works as it should: the print(*.getInfo()) gives back a dictionary like "{'stable_lights': 63}".

*I've tried the same code in the web editor (converted it to javascript, see link above) and exactly the same thing happens.

*I've tried everything to get any kind of information of the dist_to_light_at_all_from_light_mask_max object like size, type, keys, tried to serialize it and print a slice of that etc etc, but nothing works, I get similar errors, or sometimes just a timeout.

*Tried to search for the problem, but google and stack came up with exactly nothing.

So what I'm hoping somebody will be able to help me with:

*

*Why is it not working?

*What should I do to make this solution work?

*If that's not possible, what's the best way to find out the maximum value in a cumulative_cost image?

The motivation for this that I will generate many similar images in different areas, with different light-intensity cutoffs (or even other types of objects from other ImageCollections) etc, ergo the "maximum distance any point can have in the selected region from a lightsource at least this intense" value will range from hundred to the hundred-thousands meters. And of course I want to display these maps on a colorscale, but to do that I need to know the range it has to cover. And also this exact number is a useful information I need to store for later use.

A: For some operations you simply need to export an intermediate result before you can continue.  In this case, just export the output of the cumulative cost as an asset.
