Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue: 'FUNCTION TO REDUCE COLLECTION TO SINGLE IMAGE PER YEAR BY MEDOID' #40

Open
Towhid-Islam opened this issue Sep 26, 2023 · 3 comments

Comments

@Towhid-Islam
Copy link

Towhid-Islam commented Sep 26, 2023

Thank you so much for developing the amazing tool in GEE. I found that medoid method was applied to create yearly landsat composite (article link). I found the relevant codes in GEE for this operation (medoid method):

// make a medoid composite with equal weight among indices
var medoidMosaic = function(inCollection, dummyCollection) {
  
  // fill in missing years with the dummy collection
  // ** braaten edit 2023-05-02: toList and If are resource intensive, simply
  // ** merging the inCollection and dummyCollection should achieve the goal
  // ** of have at least one image for the median operation.
  var imageCount = inCollection.toList(1).length();                                                            // get the number of images 
  var finalCollection = ee.ImageCollection(ee.Algorithms.If(imageCount.gt(0), inCollection, dummyCollection)); // if the number of images in this year is 0, then use the dummy collection, otherwise use the SR collection
  // var finalCollection = inCollection.merge(dummyCollection);
  
  // calculate median across images in collection per band
  var median = finalCollection.median();                                                                       // calculate the median of the annual image collection - returns a single 6 band image - the collection median per band
  
  // calculate the different between the median and the observation per image per band
  var difFromMedian = finalCollection.map(function(img) {
    var diff = ee.Image(img).subtract(median).pow(ee.Image.constant(2));                                       // get the difference between each image/band and the corresponding band median and take to power of 2 to make negatives positive and make greater differences weight more
    return diff.reduce('sum').addBands(img);                                                                   // per image in collection, sum the powered difference across the bands - set this as the first band add the SR bands to it - now a 7 band image collection
  });
  
  // get the medoid by selecting the image pixel with the smallest difference between median and observation per band 
  return ee.ImageCollection(difFromMedian).reduce(ee.Reducer.min(7)).select([1,2,3,4,5,6], ['B1','B2','B3','B4','B5','B7']); // find the powered difference that is the least - what image object is the closest to the median of teh collection - and then subset the SR bands and name them - leave behind the powered difference band
};

Here, .reduce(ee.Reducer.min(7) gave me output with the lowest value of the pixels rather than the closest value to the median. I am not so sure through, this may lead to the inappropriate result. Could you please confirm that the tool actually used medoid method to prepare yearly single image? I will be very grateful for that.
I might be wrong. If so, please accept my apologies and let me know why it is alright. Thank you so much! I will be looking forward to your kind response.

@Towhid-Islam
Copy link
Author

I modified the codes like this:

var medoidMosaic = function(inCollection, dummyCollection) {
  var imageCount = inCollection.toList(1).length();
  var finalCollection = ee.ImageCollection(ee.Algorithms.If(imageCount.gt(0), inCollection, dummyCollection));
  var median = finalCollection.median();
  var difFromMedian = finalCollection.map(function(img) {
    var diff = (ee.Image(img).subtract(median)).abs();
    return diff.reduce('sum').multiply(-1).addBands(img);
  });
  return ee.ImageCollection(difFromMedian).qualityMosaic('sum').select([1,2,3,4,5,6], ['B1','B2','B3','B4','B5','B7']);
};

Could you please let me know what you think about it. Thanks a lot!

@jdbcode
Copy link
Member

jdbcode commented Oct 2, 2023

Can you send a script that shows how you arrived at these conclusions?

  • Shows that the original implementation returns minimum among observations (not observation that is the minimum difference from median).
  • Shows that your proposed implementation returns the observation nearest to the median.

Thank you!
(I may not be able to look or respond until the week of Oct 16th)

@Towhid-Islam
Copy link
Author

Thank you so much for your kind response.
Here is the script link:
(https://code.earthengine.google.com/e7ab3f7ecd228feccc47db989dd3be13)

  • Output_ReducerMin is the image generated from LandTrendR's codes
  • closestToMedianImage is the image generated using modified codes

If you inspect a point, you see band values of both Output_ReducerMin & closestToMedianImage and compare those with the bands in the list of images in ImageCollection: True Color (432) and the median image.

I will be waiting to hear back from you. Thanks Again!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants