-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from JorisJoBo/develop
Develop
- Loading branch information
Showing
25 changed files
with
1,400 additions
and
5,978 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
node_modules | ||
*.csv | ||
*.zip |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
from sklearn import tree | ||
import csv | ||
import numpy as np | ||
import graphviz | ||
import matplotlib.pylab as plt | ||
|
||
# Converts relative_learning_data.csv to a list | ||
data = [] | ||
with open('relative_learning_data.csv') as f: | ||
f = csv.reader(f, delimiter=',') | ||
for line in f: | ||
data.append(line) | ||
|
||
features = data[0] | ||
classes = [] | ||
data = data[1:] | ||
# ID's of columns that aren't usefull for decision trees. | ||
removed_features = [0, 1, 2, 3, 4, 5] | ||
|
||
# Removes these columns from the feature names and the dataset. | ||
cl_features = [] | ||
cl_data = [] | ||
for i in range(len(features)): | ||
if i not in removed_features: | ||
cl_features.append(features[i]) | ||
for line in data: | ||
newline = [] | ||
for i in range(len(features)): | ||
if i not in removed_features: | ||
newline.append(line[i]) | ||
cl_data.append(newline) | ||
features = cl_features | ||
data = cl_data | ||
|
||
|
||
def decisiontree(data): | ||
Xt = [] | ||
Yt = [] | ||
Xv = [] | ||
Yv = [] | ||
# Adds 90% of the data to the trainingsset, 10% to the validationset. | ||
np.random.shuffle(data) | ||
trainingsize = 0.9 * len(data) | ||
training = data[:int(trainingsize)] | ||
validation = data[int(trainingsize):] | ||
|
||
# Creates the X and Y parts of the training and test sets. | ||
# Also fills the tree species list (classes) with all different species. | ||
for line in training: | ||
if line[-1] not in classes: | ||
classes.append(line[-1]) | ||
Xt.append(line[0:-1]) | ||
Yt.append(line[-1]) | ||
for line in validation: | ||
if line[-1] not in classes: | ||
return decisiontree(data) | ||
Xv.append(line[0:-1]) | ||
Yv.append(line[-1]) | ||
|
||
clf = tree.DecisionTreeClassifier() | ||
clf = clf.fit(Xt, Yt) | ||
return clf, Xt, Yt, Xv, Yv | ||
|
||
|
||
clf, Xt, Yt, Xv, Yv = decisiontree(data) | ||
# Sorts the classes alphabetically, which makes them work as class_names | ||
classes.sort() | ||
|
||
# This creates an image of the decisiontree and exports it as a PDF. | ||
dot_data = tree.export_graphviz(clf, | ||
out_file=None, | ||
class_names=classes, | ||
feature_names=features[:-1], | ||
rounded=True, | ||
special_characters=True) | ||
graph = graphviz.Source(dot_data) | ||
graph.render('tree', view=True) | ||
|
||
|
||
# This calculates the average correctness for the dataset. | ||
def avgcost(data, n): | ||
totalcost = 0 | ||
for i in range(n): | ||
clf, Xt, Yt, Xv, Yv = decisiontree(data) | ||
totalcost = totalcost + clf.score(Xv, Yv) | ||
return totalcost / n | ||
|
||
|
||
print('Average Correctness: ' + str(avgcost(data, 500))) | ||
|
||
|
||
# This calculates the usage (/importance) for all features in the decisiontree. | ||
def avgimportance(data, n, features): | ||
totalimportance = [] | ||
for i in range(n): | ||
clf, _, _, _, _ = decisiontree(data) | ||
importance = clf.feature_importances_ | ||
if len(totalimportance) == 0: | ||
totalimportance = importance | ||
else: | ||
totalimportance = [ | ||
x + y for x, | ||
y in zip( | ||
totalimportance, | ||
importance)] | ||
for i in range(len(importance)): | ||
print(str(features[i]) + ': ' + str(totalimportance[i] / n)) | ||
|
||
|
||
avgimportance(data, 500, features) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
from sklearn import tree | ||
import csv | ||
import numpy as np | ||
import graphviz | ||
import matplotlib.pylab as plt | ||
|
||
# Converts relative_learning_data.csv to a list | ||
data = [] | ||
with open('relative_learning_data.csv') as f: | ||
f = csv.reader(f, delimiter=',') | ||
for line in f: | ||
data.append(line) | ||
|
||
features = data[0] | ||
classes = [] | ||
data = data[1:] | ||
# ID's of columns that aren't usefull for decision trees. | ||
removed_features = [0, 2, 3, 4, 5] | ||
|
||
# Removes these columns from the feature names and the dataset. | ||
cl_features = [] | ||
cl_data = [] | ||
for i in range(len(features)): | ||
if i not in removed_features: | ||
cl_features.append(features[i]) | ||
for line in data: | ||
newline = [] | ||
for i in range(len(features)): | ||
if i not in removed_features: | ||
newline.append(line[i]) | ||
cl_data.append(newline) | ||
features = cl_features | ||
data = cl_data | ||
|
||
|
||
def decisiontree(data): | ||
Xt = [] | ||
Yt = [] | ||
Xv = [] | ||
Yv = [] | ||
# Finds all polygonID's, randomly adds 90% of ID's to the trainingset. | ||
polygonIDs = [] | ||
for line in data: | ||
if line[0] not in polygonIDs: | ||
polygonIDs.append(line[0]) | ||
np.random.shuffle(polygonIDs) | ||
trainingsize = 0.9 * len(polygonIDs) | ||
trainingIDs = polygonIDs[:int(trainingsize)] | ||
|
||
# Assigns each line in the list to the training/test dataset. | ||
# Also fills the tree species list (classes) with all different species. | ||
training = [] | ||
validation = [] | ||
for line in data: | ||
if line[-1] not in classes: | ||
classes.append(line[-1]) | ||
if line[0] in trainingIDs: | ||
training.append(line) | ||
else: | ||
validation.append(line) | ||
# Creates the X and Y parts of the training and test sets. | ||
for line in training: | ||
Xt.append(line[1:-1]) | ||
Yt.append(line[-1]) | ||
for line in validation: | ||
Xv.append(line[1:-1]) | ||
Yv.append(line[-1]) | ||
|
||
clf = tree.DecisionTreeClassifier(min_impurity_split=0.77) | ||
clf = clf.fit(Xt, Yt) | ||
return clf, Xt, Yt, Xv, Yv | ||
|
||
|
||
clf, Xt, Yt, Xv, Yv = decisiontree(data) | ||
|
||
# Sorts the classes alphabetically, which makes them work as class_names | ||
classes.sort() | ||
|
||
# This creates an image of the decisiontree and exports it as a PDF | ||
dot_data = tree.export_graphviz(clf, | ||
out_file=None, | ||
class_names=classes, | ||
feature_names=features[1:-1], | ||
rounded=True, | ||
special_characters=True) | ||
graph = graphviz.Source(dot_data) | ||
graph.render('tree', view=True) | ||
|
||
|
||
# This calculates the average correctness for the dataset. | ||
def avgcost(data, n): | ||
totalcost = 0 | ||
for i in range(n): | ||
clf, Xt, Yt, Xv, Yv = decisiontree(data) | ||
totalcost = totalcost + clf.score(Xv, Yv) | ||
return totalcost / n | ||
|
||
|
||
print('Average Correctness: ' + str(avgcost(data, 500))) | ||
|
||
|
||
# This calculates the usage (/importance) for all features in the decisiontree. | ||
def avgimportance(data, n, features): | ||
totalimportance = [] | ||
for i in range(n): | ||
clf, _, _, _, _ = decisiontree(data) | ||
importance = clf.feature_importances_ | ||
if len(totalimportance) == 0: | ||
totalimportance = importance | ||
else: | ||
totalimportance = [ | ||
x + y for x, | ||
y in zip( | ||
totalimportance, | ||
importance)] | ||
for i in range(len(importance)): | ||
print(str(features[i]) + ': ' + str(totalimportance[i] / n)) | ||
|
||
|
||
avgimportance(data, 500, features) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
@echo off | ||
set outputfolder=LiDAR_data | ||
|
||
echo Running LAScanopy... (this may take a while) | ||
mkdir %outputfolder%\canopy | ||
for %%a in (%outputfolder%\classify\*.laz) do ( | ||
echo - Running canopy on %%~nxa... | ||
lascanopy -i LiDAR_data\classify\%%~nxa -names -lor LiDAR_data\ID_forest_grid_coords.csv -dns -p 5 10 25 50 75 90 -min -max -avg -std -ske -kur -qav -cov -c 2 4 10 50 -int_min -int_max -int_avg -int_qav -int_std -int_ske -int_kur -int_c 128 256 1024 -int_p 25 50 75 -o LiDAR_data\canopy\%%~na.csv | ||
) | ||
|
||
PAUSE |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
''' | ||
add_canopy_files.py | ||
usage for map: add_canopy_files.py input_directory output.csv | ||
usage for single file: add_canopy_files.py input.csv output.csv | ||
Adds together all csv files generated from lascanopy and removes lines | ||
with missing values. Can also be used to remove lines with missing values | ||
from a single csv file. | ||
The map containing the csv files that have to be combined has to be | ||
present in the same directory as this file. | ||
''' | ||
import os | ||
from sys import argv | ||
|
||
dirname = argv[1] | ||
try: | ||
outputfile = argv[2] | ||
except BaseException: | ||
print("no output file given, output saved to 'combined_canopy.csv'") | ||
outputfile = "combined_canopy.csv" | ||
|
||
lines = [] | ||
header = None | ||
if dirname.endswith('.csv'): | ||
with open(dirname, 'r') as f: | ||
for i, line in enumerate(f, 0): | ||
if i != 0: | ||
lines.append(line) | ||
else: | ||
header = line | ||
else: | ||
for file in os.listdir(dirname): | ||
if file.endswith(".csv"): | ||
with open(dirname + '/' + file, 'r') as f: | ||
for i, line in enumerate(f, 0): | ||
if i != 0: | ||
lines.append(line) | ||
else: | ||
header = line | ||
|
||
with open(outputfile, 'w') as f: | ||
f.write(header) | ||
for line in lines: | ||
line2 = line.split(',') | ||
if "-" not in line2: | ||
f.write(line) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import csv | ||
|
||
with open("learning_data.csv") as f1: | ||
with open("common_learning_data.csv", 'w') as f2: | ||
lines = csv.reader(f1, delimiter=";") | ||
data = [] | ||
for line in lines: | ||
data.append(line) | ||
seen = [] | ||
for line1 in data: | ||
if line1 == data[0]: | ||
f2.write(';'.join(line1) + '\n') | ||
c = line1[-1] | ||
if c not in seen: | ||
counter = 0 | ||
for line2 in data: | ||
if line2[-1] == c: | ||
counter += 1 | ||
if counter >= 50: | ||
seen.append(c) | ||
f2.write(';'.join(line1) + '\n') | ||
else: | ||
f2.write(';'.join(line1) + '\n') |
Oops, something went wrong.