Skip to content

Commit

Permalink
Merge pull request orbisgis#7 from SPalominos/master
Browse files Browse the repository at this point in the history
Rework the typo class and test.
  • Loading branch information
ebocher authored Dec 13, 2019
2 parents 2b27d48 + 45946ea commit 73e8b52
Show file tree
Hide file tree
Showing 37 changed files with 188 additions and 200 deletions.
24 changes: 0 additions & 24 deletions geoclimate/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,41 +22,17 @@
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy</artifactId>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
</dependency>
<dependency>
<groupId>org.orbisgis.orbisdata.datamanager</groupId>
<artifactId>jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.orbisgis.orbisdata.processmanager</groupId>
<artifactId>process</artifactId>
</dependency>
<dependency>
<groupId>org.orbisgis.orbisprocess.geoclimate</groupId>
<artifactId>preparedata</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.orbisgis.orbisprocess.geoclimate</groupId>
<artifactId>geoindicators</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.orbisgis.orbisprocess.geoclimate</groupId>
<artifactId>processingchain</artifactId>
<version>${project.version}</version>
</dependency>
<!-- Test dependencies -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
</dependency>
</dependencies>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,13 @@
package org.orbisgis.orbisprocess.geoclimate

import org.orbisgis.orbisprocess.geoclimate.geoindicators.BlockIndicators
import org.orbisgis.orbisprocess.geoclimate.geoindicators.BuildingIndicators
import org.orbisgis.orbisprocess.geoclimate.geoindicators.DataUtils
import org.orbisgis.orbisprocess.geoclimate.geoindicators.GenericIndicators
import org.orbisgis.orbisprocess.geoclimate.geoindicators.Geoindicators
import org.orbisgis.orbisprocess.geoclimate.geoindicators.RsuIndicators
import org.orbisgis.orbisprocess.geoclimate.geoindicators.SpatialUnits
import org.orbisgis.orbisprocess.geoclimate.geoindicators.TypologyClassification
import org.orbisgis.orbisprocess.geoclimate.geoindicators.*
import org.orbisgis.orbisprocess.geoclimate.preparedata.PrepareData
import org.orbisgis.orbisprocess.geoclimate.preparedata.bdtopo.BDTopoGISLayers
import org.orbisgis.orbisprocess.geoclimate.preparedata.common.AbstractTablesInitialization
import org.orbisgis.orbisprocess.geoclimate.preparedata.common.InputDataFormatting
import org.orbisgis.orbisprocess.geoclimate.preparedata.osm.FormattingForAbstractModel
import org.orbisgis.orbisprocess.geoclimate.preparedata.osm.OSMGISLayers
import org.orbisgis.orbisprocess.geoclimate.processingchain.BuildGeoIndicators
import org.orbisgis.orbisprocess.geoclimate.processingchain.BuildSpatialUnits
import org.orbisgis.orbisprocess.geoclimate.processingchain.Workflow
import org.orbisgis.orbisprocess.geoclimate.processingchain.PrepareBDTopo
import org.orbisgis.orbisprocess.geoclimate.processingchain.PrepareOSM
import org.orbisgis.orbisprocess.geoclimate.processingchain.ProcessingChain
import org.orbisgis.orbisprocess.geoclimate.processingchain.*

/**
* Root access point to the Geoindicators processes.
Expand Down
19 changes: 8 additions & 11 deletions geoindicators/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,6 @@
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy</artifactId>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
</dependency>
<dependency>
<groupId>org.orbisgis.orbisdata.datamanager</groupId>
<artifactId>jdbc</artifactId>
Expand All @@ -47,24 +39,29 @@
<dependency>
<groupId>org.orbisgis</groupId>
<artifactId>h2gis-network</artifactId>
<version>2.0.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.github.haifengl</groupId>
<artifactId>smile-core</artifactId>
<version>2.0.0</version>
</dependency>
<dependency>
<groupId>com.thoughtworks.xstream</groupId>
<artifactId>xstream</artifactId>
<version>1.4.11.1</version>
</dependency>

<!-- Test dependencies -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,16 @@ package org.orbisgis.orbisprocess.geoclimate.geoindicators
import com.thoughtworks.xstream.XStream
import groovy.transform.BaseScript
import org.orbisgis.orbisdata.datamanager.api.dataset.ITable
import org.orbisgis.orbisdata.datamanager.dataframe.DataFrame
import org.orbisgis.orbisdata.datamanager.jdbc.JdbcDataSource
import org.orbisgis.orbisdata.processmanager.api.IProcess
import smile.classification.RandomForest;
import smile.data.formula.Formula
import org.orbisgis.orbisdata.datamanager.dataframe.DataFrame
import smile.base.cart.SplitRule
import smile.validation.Validation;
import smile.classification.RandomForest
import smile.data.formula.Formula
import smile.validation.Accuracy
import smile.validation.Validation

import java.util.zip.GZIPOutputStream;


import java.util.zip.GZIPOutputStream

@BaseScript Geoindicators geoindicators

Expand Down Expand Up @@ -42,6 +40,7 @@ import java.util.zip.GZIPOutputStream;
* the American Meteorological Society 93, no. 12 (2012): 1879-1900.
*
* @return A database table name.
*
* @author Jérémy Bernard
*/
IProcess identifyLczType() {
Expand Down Expand Up @@ -265,91 +264,92 @@ IProcess identifyLczType() {
* @param varToModel String where is saved the name of the field to model
* @param save Boolean to save the model into a file if needed
* @param pathAndFileName String of the path and name where the model has to be saved (default "/home/RfModel")
* @param ntrees the number of trees to build the forest
* @param mtry the number of input variables to be used to determine the decision
* @param ntrees The number of trees to build the forest
* @param mtry The number of input variables to be used to determine the decision
* at a node of the tree. p/3 seems to give generally good performance,
* where p is the number of variables
* @param rule Decision tree split rule (The function to measure the quality of a split. Supported rules
* are “gini” for the Gini impurity and “entropy” for the information gain)
* @param maxDepth the maximum depth of the tree.
* @param maxNodes the maximum number of leaf nodes in the tree.
* @param nodeSize the number of instances in a node below which the tree will
* @param maxDepth The maximum depth of the tree.
* @param maxNodes The maximum number of leaf nodes in the tree.
* @param nodeSize The number of instances in a node below which the tree will
* not split, setting nodeSize = 5 generally gives good results.
* @param subsample the sampling rate for training tree. 1.0 means sampling with replacement. < 1.0 means
* @param subsample The sampling rate for training tree. 1.0 means sampling with replacement. < 1.0 means
* sampling without replacement.
* @param datasource A connection to a database
*
* @return RfModel A randomForest model (see smile library for further information about the object)
*
* @author Jérémy Bernard
*/
IProcess createRandomForestClassif() {
return create({
title "Create a Random Forest model"
inputs trainingTableName: String, varToModel: String, save: boolean, pathAndFileName: String,
ntrees: int, mtry: int, rule: "GINI", maxDepth: int, maxNodes: int, nodeSize: int,
subsample: double, datasource: JdbcDataSource
inputs trainingTableName: String, varToModel: String, save: boolean, pathAndFileName: String, ntrees: int,
mtry: int, rule: "GINI", maxDepth: int, maxNodes: int, nodeSize: int, subsample: double,
datasource: JdbcDataSource
outputs RfModel: RandomForest
run { trainingTableName, varToModel, save, pathAndFileName, ntrees, mtry, rule,
maxDepth, maxNodes, nodeSize, subsample, datasource ->
run { String trainingTableName, String varToModel, save, pathAndFileName, ntrees, mtry, rule, maxDepth,
maxNodes, nodeSize, subsample, JdbcDataSource datasource ->

SplitRule splitRule
def splitRule
if(rule){
switch(rule.toUpperCase()) {
case "GINI":
splitRule = SplitRule.GINI
break
case "ENTROPY":
splitRule = SplitRule.ENTROPY
splitRule = SplitRule.valueOf(rule)
break
default:
error "The rule value ${rule} is not supported. Please use GINI or ENTROPY"
error "The rule value ${rule} is not supported. Please use 'GINI' or 'ENTROPY'"
return null;
}
}
else{
error "The rule value cannot be null or empty. Please use GINI or ENTROPY"
error "The rule value cannot be null or empty. Please use 'GINI' or 'ENTROPY'"
return null;
}
info "Create a Random Forest model"

//Check if the column names exists
def columnTypo = "I_TYPO"

ITable trainingTable = datasource.getTable(trainingTableName)
def trainingTable = datasource.getTable(trainingTableName)

assert trainingTable.hasColumn(columnTypo, String.class)
if(!trainingTable.hasColumn(columnTypo, String.class)){
error "The training table should have a String column name 'I_TYPO'"
return null
}

// Read the training table as a DataFrame
def df = DataFrame.of(trainingTable)

Formula formula = Formula.lhs(varToModel)
def formula = Formula.lhs(varToModel)

// Convert the variable to model into factors (if string for example) and remove rows containing null values
df = df.factorize(varToModel).omitNullRows()

// Create the randomForest
RandomForest model = RandomForest.fit(formula, df, ntrees, mtry,
splitRule, maxDepth, maxNodes, nodeSize, subsample)
def model = RandomForest.fit(formula, df, ntrees, mtry, splitRule, maxDepth, maxNodes, nodeSize, subsample)


// Calculate the prediction using the same sample in order to identify what is the
// data rate that has been well classified
int[] prediction = Validation.test(model, df)
int[] truth = df.apply(varToModel).toIntArray()
double accuracy = Accuracy.of(truth, prediction)
def accuracy = Accuracy.of(truth, prediction)
logger.info "The percentage of the data that have been well classified is : ${accuracy*100}%"

try {
if (save) {
def zOut =
new GZIPOutputStream(new FileOutputStream(pathAndFileName));
XStream xs = new XStream()
def zOut = new GZIPOutputStream(new FileOutputStream(pathAndFileName));
def xs = new XStream()
xs.toXML(model, zOut)
zOut.close()
}
}
catch (Exception e){
logger.error("Cannot save the model", e)
return null
}

[RfModel: model]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ package org.orbisgis.orbisprocess.geoclimate.geoindicators

import org.junit.jupiter.api.BeforeAll
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import org.orbisgis.orbisdata.datamanager.jdbc.h2gis.H2GIS

import static org.junit.jupiter.api.Assertions.*
import org.junit.jupiter.api.Test
import static org.junit.jupiter.api.Assertions.assertEquals
import static org.junit.jupiter.api.Assertions.assertTrue

class BuildingIndicatorsTests {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package org.orbisgis.orbisprocess.geoclimate.geoindicators

import org.junit.jupiter.api.Test
import org.orbisgis.orbisdata.datamanager.jdbc.h2gis.H2GIS
import org.orbisgis.orbisdata.processmanager.api.IProcess

import static org.junit.jupiter.api.Assertions.*
import org.junit.jupiter.api.Test
import static org.junit.jupiter.api.Assertions.assertEquals
import static org.junit.jupiter.api.Assertions.assertTrue

class DataUtilsTests {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import org.orbisgis.orbisdata.datamanager.jdbc.h2gis.H2GIS

import static org.junit.jupiter.api.Assertions.assertEquals
import static org.junit.jupiter.api.Assertions.assertNotNull
import static org.junit.jupiter.api.Assertions.assertTrue
import static org.junit.jupiter.api.Assertions.*

class SpatialUnitsTests {

Expand Down
Loading

0 comments on commit 73e8b52

Please sign in to comment.