Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
Next revision Both sides next revision
ego_script [2012/05/29 02:28]
admin
ego_script [2013/01/07 16:46]
admin [Expression Calculation Syntax]
Line 253: Line 253:
 ==== Expression Calculation Syntax ==== ==== Expression Calculation Syntax ====
  
-FIXME Dinamica version: 1.9.0+Since Dinamica ​EGO version ​2.0, expressions in EGO Script can be represented using an additional special syntax. The example below illustrates:​
  
-Expressions can be represented using an additional special syntax. The example below illustrates:​ +<​code ​java>
- +
-<​code ​cpp>+
 if #random <= ($mean - $min) / ($max - $min) then if #random <= ($mean - $min) / ($max - $min) then
    $min + sqrt(#​random * ($max - $min) * ($mean - $min))    $min + sqrt(#​random * ($max - $min) * ($mean - $min))
Line 267: Line 265:
  
 The use of sigils has some advantages including making easy to determine the data type represented by the variables. They also prevent clashes between special names like "​rand",​ "​line"​ and "​column"​ and the user defined ones. ((Using sigils the expression "rand + #rand" is the sum of a random value with a the value of map named "​rand"​)) The use of sigils has some advantages including making easy to determine the data type represented by the variables. They also prevent clashes between special names like "​rand",​ "​line"​ and "​column"​ and the user defined ones. ((Using sigils the expression "rand + #rand" is the sum of a random value with a the value of map named "​rand"​))
 +
 +//However, keep in mind that this syntax is not fully compatible with the graphical interface and you may end up having an invalid model if you edit a model created using this syntax in the graphical interface.//​
  
 === Examples === === Examples ===
Line 272: Line 272:
 The example below represents a fragment of a real model using the original syntax: The example below represents a fragment of a real model using the original syntax:
  
-<​code ​cpp>+<​code ​java>
     patches := LoadCategoricalMap "​../​originals/​landscape.ers"​ .no .no 0 0 .none;     patches := LoadCategoricalMap "​../​originals/​landscape.ers"​ .no .no 0 0 .none;
     // ...     // ...
Line 291: Line 291:
 The model modified to use the special expression calculation syntax can be seen below: The model modified to use the special expression calculation syntax can be seen below:
  
-<​code ​cpp>+<​code ​java>
     patches := LoadCategoricalMap "​../​originals/​landscape.ers"​ .no .no 0 0 .none;     patches := LoadCategoricalMap "​../​originals/​landscape.ers"​ .no .no 0 0 .none;
     // ...     // ...
Line 298: Line 298:
     // Isolate the patches of a single category. ​     // Isolate the patches of a single category. ​
     @collapsed = yes     @collapsed = yes
-    singleCategoryPatches := #[ if #patches = $currenteCategoryId then 1 else null ];+    singleCategoryPatches := #[ if #patches = $currenteCategoryId then 1 else null ] .uint8 0 .no;
 </​code>​ </​code>​
 +
 +===== Complex Example =====
 +
 +<code csharp>
 +@title = Calc Patch Sizes, Mean Patch Sizes and Patch Size Standard Deviations
 +@author = Dinamica Team
 +@organization = CSR / UFMG
 +@metaversion = 1.0
 +@description = Calculate the patch sizes, the mean patch sizes and the patch size standard deviation of different categories.
 +@notes = "The model input is a map where the non-null values identify the patches.
 +
 +The output is a table per category containing the patch sizes and two additional tables containing the mean patch size and the patch size standard deviation per category."​
 +@showproperties = yes
 +@version = 1.9.32
 +Script {{
 +    // The input map. 
 +    //
 +    // Non-null values identify the patches. ​
 +    patches := LoadCategoricalMap "​../​originals/​landscape.ers"​ .no .no 0 0 .none .none;
 +
 +    ForEachCategory patches {{
 +        step = step;
 +
 +        // Patch size standard deviations. ​
 +        currentPatchSizeStandardDeviations := MuxLookupTable [
 +            "​Category"​ "​Patch_Size_Standard_Deviations_In_Hectares"​
 +        ] updatedPatchSizeStandardDeviations;​
 +
 +        // Mean Patch sizes. ​
 +        currentMeanPatchSizes := MuxLookupTable [
 +            "​Category"​ "​Mean_Patch_Sizes_In_Hectares"​
 +        ] updatedPatchMeans;​
 +
 +        // Current category. ​
 +        currentCategory := Step step;
 +
 +        // Calculate the sizes of a set of patches and their corresponding mean and 
 +        // standard deviation. ​
 +        @collapsed = yes
 +        Group {{
 +            // Isolate the patches of a single category. ​
 +            @collapsed = yes
 +            singleCategoryIndividualizedPatches := # [
 +                if #patches = $currentCategory then
 +                    #patches
 +                else 
 +                    null        ​
 +            ] .int32 .default .no .none;
 +
 +            // Calculate the patch size, the mean patch size and patch size standard ​
 +            // deviation. ​
 +            @collapsed = yes
 +            Group {{
 +                // Individualize the patches. ​
 +                individualizedPatches := CalcPatchLabelMap {
 +                    source = singleCategoryIndividualizedPatches,​
 +                    initialPatchLabel = 1,
 +                    onlyOrthogonalsAreAllowed = .no,
 +                    windowLines = 3,
 +                    windowColumns = 3,
 +                    cellType = .int32,
 +                    nullValue = .default,
 +                    patchLabelsAreSparse = .no
 +                };
 +
 +                attributes := ExtractMapAttributes individualizedPatches .yes .yes;
 +
 +                // Calculate the mean patch size. 
 +                //
 +                // The calculation uses the total patch size and the number of individual patches. ​
 +                @collapsed = yes
 +                mean := $ [
 +                    (%attributes["​cellArea"​] * %attributes["​nonNullCells"​] / %attributes["​uniqueCells"​]) ? 0
 +                ] .no 0;
 +
 +                // Calculate the patch sizes. ​
 +                _ patchSizesInHectares _ := CalcAreas individualizedPatches;​
 +
 +                // Calculate the patch size standard deviation. ​
 +                @collapsed = yes
 +                Group {{
 +                    // Calculate the patch size standard deviation. ​
 +                    @collapsed = no
 +                    LogPolicy .warning .no {{
 +                        // Calculate the SUM(Xi - MEAN)^2, where Xi is the current patch size. 
 +                        @collapsed = yes
 +                        ForEachCategory individualizedPatches {{
 +                            step0 = step;
 +
 +                            // Accumulated value of SUM(Xi - MEAN)^2. ​
 +                            currentAccumulatedValue := MuxValue 0 updatedAccumulatedValue;​
 +
 +                            // Current patch id. 
 +                            currentPatchId := Step step0;
 +
 +                            // Calculate the updated accumulated value of SUM(Xi - MEAN)^2 for the current ​
 +                            // patch size. 
 +                            @collapsed = yes
 +                            updatedAccumulatedValue := $ [
 +                                $currentAccumulatedValue + ((%patchSizesInHectares[$currentPatchId] - $mean) ^ 2)
 +                            ] .no 0;
 +                        }};
 +                    }};
 +
 +                    // Provide 0 as the default accumulated value even if the input map has no patches ​
 +                    // for the current categories. ​
 +                    accumulatedValue := ValueJunction updatedAccumulatedValue 0;
 +
 +                    // Calculate the standard deviation using the SUM(Xi - MEAN)^2. ​
 +                    @collapsed = yes
 +                    standardDeviation := $ [
 +                        sqrt($accumulatedValue / %attributes["​uniqueCells"​]) ? 0
 +                    ] .no 0;
 +                }};
 +
 +                // Update the mean table with the mean patch size of the current category. ​
 +                updatedPatchMeans := SetLookupTableValue currentMeanPatchSizes currentCategory mean;
 +
 +                // Update the table of standard deviations with the standard deviation of the 
 +                // current category. ​
 +                updatedPatchSizeStandardDeviations := SetLookupTableValue currentPatchSizeStandardDeviations currentCategory standardDeviation;​
 +            }};
 +        }};
 +
 +        // Save a table of patch sizes per category. ​
 +        SaveLookupTable patchSizesInHectares "​patch_sizes.csv"​ 2 step .none;
 +    }};
 +
 +    // Save the mean patch sizes as a table. ​
 +    SaveLookupTable updatedPatchMeans "​mean_patch_sizes.csv"​ 2 .none .none;
 +
 +    // Save the patch size standard deviations as a table. ​
 +    SaveLookupTable updatedPatchSizeStandardDeviations "​patch_size_standard_deviations.csv"​ 2 .none .none;
 +}};
 +</​code>​
 +