parevalo / gee-ccdc-tools

Tools and Earth Engine apps to interact with the outputs from the CCDC algorithm
MIT License
69 stars 31 forks source link

Error "In users/parevalo_bu/gee-ccdc-tools:ccdcUtilities/ccdc.js fit.select is not a function" #17

Open 21Sraboni opened 5 months ago

21Sraboni commented 5 months ago

Hi, thank you very much for your excellent work. I'm having a problem with the new version, so I'm asking for your help. There is this issue "In users/parevalo_bu/gee-ccdc-tools:ccdcUtilities/ccdc.js fit.select is not a function" when I want to get the result from "var ccdImage = utils.CCDC.buildCcdImage(params.Classification.changeResults, params.Classification.segs.length, params.Classification.bandNames)". Looking forward to hearing from you back soon. Thank you.

Regards, Sraboni Belgium.

21Sraboni commented 5 months ago

this is my code:

var utils = require('users/parevalo_bu/gee-ccdc-tools:ccdcUtilities/api'); // Define parameters //Map.centerObject (training_data, 8); //Map.addLayer (training_data); // Change detection parameters var changeDetection = { breakpointBands: ['B3','B4','B5','B6','B7'], tmaskBands: ['B3','B6'], minObservations: 6, chiSquareProbability: .99, minNumOfYearsScaler: 1.33, dateFormat: 2, lambda: 20/10000, maxIterations: 25000 };

// Classification parameters var classification = { bandNames: ["B1","B2","B3","B4","B5","B6","B7"], inputFeatures: ["INTP", "SLP","PHASE","RMSE"], coefs: ["INTP", "SLP","COS", "SIN","RMSE","COS2","SIN2","COS3","SIN3"], ancillaryFeatures: ["ELEVATION","ASPECT","DEM_SLOPE","RAINFALL","TEMPERATURE"], resultFormat: 'SegCollection', classProperty: 'LC_Class', yearProperty: 'Year', classifier: ee.Classifier.smileRandomForest, classifierParams: { numberOfTrees: 150, variablesPerSplit: null, minLeafPopulation: 1, bagFraction: 0.5, maxNodes: null }, outPath: '.', //segs: ["S1", "S2", "S3", "S4", "S5", "S6"], segs: ["S1", "S2", "S3", "S4", "S5", "S6"], trainingPath: 'users/Sraboni_Sarker/sample_test1', // Modify this line with your training data path trainingPathPredictors: 'users/Sraboni_Sarker/Predictors', // Modify this line with your training predictors path changeResults: results, };

var studyRegion = ee.FeatureCollection('USDOS/LSIB_SIMPLE/2017') .filterMetadata('country_na','equals','Belgium').union() //Map.addLayer (studyRegion, {}, 'Study Area'); //Map.centerObject (studyRegion, 8);

var params = { start: '2018-01-01', end: '2020-01-01', ChangeDetection: changeDetection, Classification: classification, StudyRegion: studyRegion }; /* // Filter by date and a location in Belgium var filteredLandsat = utils.Inputs.getLandsat() .filterBounds(params.StudyRegion) .filterDate(params.start, params.end)

print(filteredLandsat.size())

//Map.addLayer (filteredLandsat, {}, 'Image'); //Map.centerObject (filteredLandsat, 7); */ // ** //

// Load Landsat imagery var landsatCollection = ee.ImageCollection('LANDSAT/LC08/C01/T1_SR') // Landsat 8 Surface Reflectance .filterBounds(params.StudyRegion) // Filter by study region .filterDate(params.start, params.end); // Filter by date range

//Map.addLayer (landsatCollection, {}, 'Image'); //Map.centerObject (landsatCollection, 7);

// Print the number of images in the Landsat collection print('Number of Landsat images:', landsatCollection.size());

// Get one image from the Landsat collection var sampleImage = landsatCollection.first();

// Print the bands of the sample image print('Bands of the sample image:', sampleImage.bandNames());

// Optionally, you can sort the collection by date var sortedLandsat = landsatCollection.sort('system:time_start'); //print('Sorted Landsat collection:', sortedLandsat);

// Use the sorted Landsat collection as the input to your change detection algorithm params.ChangeDetection['collection'] = sortedLandsat;

//params.ChangeDetection['collection'] = filteredLandsat params.ChangeDetection['collection'] = landsatCollection; var results = ee.Algorithms.TemporalSegmentation.Ccdc(params.ChangeDetection); print(results)

//*****//

// are they needed?? // **** // Run the Temporal Segmentation algorithm

// Print the segmentation results print('Segmentation Results:', results);

// Visualize the segmentation results on the map //Map.addLayer(results, {}, 'Segmentation Results');

// ** part 2 ****//

/* var params = { Classification: { trainingPath: 'users/Sraboni_Sarker/sample' } }; var trainingData = ee.FeatureCollection(params.Classification.trainingPath) // Assuming 'params' is an object containing classification parameters var trainingPath = params.Classification.trainingPath;

// Load the training data FeatureCollection from the specified path var trainingData = ee.FeatureCollection(trainingPath); */

// // var trainingData = ee.FeatureCollection(params.Classification.trainingPath);

var trainingData = ee.FeatureCollection("users/Sraboni_Sarker/sample_test1"); // Print the size of the training data to check if it's empty print('Number of features in training data:', trainingData.size()); //print(trainingData.first());

// // Print out more features from the training data // // Don't Delete var numFeaturesToPrint = 480; // Change this number as needed var moreTrainingData = trainingData.limit(numFeaturesToPrint); print('More training data:', moreTrainingData); print("First Feature Properties:", trainingData.first().toDictionary());

// Load training data var trainingData = ee.FeatureCollection(params.Classification.trainingPath); // Define the classes and their corresponding numerical values var classMapping = { 'forest': 1, 'crop': 2, 'grass': 3, 'wetland': 4, 'settlement': 5, 'others': 6 };

// Compute histogram before remapping var histogramBefore = ee.Dictionary(trainingData.reduceColumns({ reducer: ee.Reducer.frequencyHistogram(), selectors: ['LC_Class'] }).get('histogram')).map(function(key, value) { return [parseInt(key), value]; });

print('Histogram before remapping:', histogramBefore);

// Remap class labels trainingData = trainingData.map(function(feat) { return feat.set('LC_Class', classMapping[feat.get('LC_Class')]); });

// Compute histogram after remapping var histogramAfter = ee.Dictionary(trainingData.reduceColumns({ reducer: ee.Reducer.frequencyHistogram(), selectors: ['LC_Class'] }).get('histogram')).map(function(key, value) { return [parseInt(key), value]; });

print('Histogram after remapping:', histogramAfter);

// Set year property for all features //trainingData = trainingData.map(function(feat) { //return feat.set('year', 2018); //});

// Set year property for all features //var year = 2018; // Define the year variable //trainingData = trainingData.map(function(feat) { //return feat.set('year', year); // Set the 'year' property for each feature //});

// Print the first feature to verify changes //print('First feature after modifications:', trainingData.first());

/* print(trainingData.aggregate_histogram('LC_Class')) trainingData = trainingData.remap(['forest','crop','grass','wetland','settlement','others'],[1,2,3,4,5,6],'LC_Class') print(trainingData.aggregate_histogram('LC_Class'))

trainingData = trainingData.map(function(feat) { return feat.set('year',2014)}) */
//**//

// Define bands to use in classification var bands = params.Classification.bandNames; print (bands);

// Define coefficients to use in classification var coefs = params.Classification.coefs; print (coefs);

// Segment ids var segs = params.Classification.segs; print (segs);

// Property corresponding to year of training data var yearProperty = params.Classification.yearProperty;

// Define path to change detection results params.Classification.changeResults = 'users/Sraboni_Sarker/Output';

// Load ccd image stack with coefficients and change information //var ccdImage = utils.CCDC.buildCcdImage(params.Classification.changeResults, params.Classification.segs.length, params.Classification.bandNames); var ccdImage = utils.CCDC.buildCcdImage(params.Classification.changeResults, params.Classification.segs.length, params.Classification.bandNames);

print('CCD Image:', ccdImage);

// Finally, get ancillary topographic and climate data var ancillary = utils.Inputs.getAncillary()

var trainingData = utils.Classification.getTrainingCoefsAtDate( trainingData, coefs, bands, yearProperty, ancillary, ccdImage, segs);

// Filter points with no data var testBand = params.Classification.bandNames[0] + '_' + params.Classification.coefs[0] trainingData = trainingData.filter(ee.Filter.notNull([testBand]))

print('First training point with predictors:', trainingData.first());

Export.table.toAsset({ collection: trainingData, description: 'trainingDataProcessed', assetId: params.Classification.trainingPathPredictors});

//trainingData = utils.Classification.assignIds(trainingData, 'ID')