-
Notifications
You must be signed in to change notification settings - Fork 3
/
qsdm_provider.py
229 lines (196 loc) · 9.46 KB
/
qsdm_provider.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# -*- coding: utf-8 -*-
"""
/***************************************************************************
QSDM
Species distribution modelling support for the QGIS Processing toolbox
-------------------
begin : 2014-03-31
copyright : (C) 2014 by Martin Jung
email : martinjung-at-zoho.com
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
"""
__author__ = 'Martin Jung'
__date__ = 'April 2014'
__copyright__ = '(C) 2014, Martin Jung'
__revision__ = '$Format:%H$' # This will get replaced with a git SHA1 when you do a git archive
# Import PyQT bindings
from PyQt4.QtCore import *
from PyQt4.QtGui import *
# Processing bindings
from processing.core.AlgorithmProvider import AlgorithmProvider
from processing.core.ProcessingConfig import ProcessingConfig
from processing.core.ProcessingConfig import Setting
from processing.core.ProcessingLog import ProcessingLog
# Custom Processing Settings file
from qsdm_settings import qsdm_settings
# Import modules
import os
# The main algorithm Provider. Modules will be added to Processing (SEXTANTE)
# if their requirements are met
class qsdm_serviceProvider(AlgorithmProvider):
def __init__(self):
AlgorithmProvider.__init__(self)
# Check requirements
self.checkRequirements()
# Create algorithms list
self.createAlgsList()
def getDescription(self):
return "QSDM (species distribution modelling)"
def initializeSettings(self):
'''In this method we add settings needed to configure our provider.
Do not forget to call the parent method, since it takes care or
automatically adding a setting for activating or deactivating the
algorithms in the provider'''
AlgorithmProvider.initializeSettings(self)
# Path to Java Jar
ProcessingConfig.addSetting(Setting(self.getDescription(),
qsdm_settings.JAVA_EXEC,
'Detected JAVA folder with java executable',
qsdm_settings.javaPath()))
# Path to MAXENT
ProcessingConfig.addSetting(Setting(self.getDescription(),
qsdm_settings.MAXENT,
'Path to maxent.jar file',
qsdm_settings.maxent()))
# Working folder
ProcessingConfig.addSetting(Setting(self.getDescription(),
qsdm_settings.WORK_DIR,
'MAXENT Working Folder',
qsdm_settings.workPath()))
# Memory for SDM
ProcessingConfig.addSetting(Setting(self.getDescription(),
qsdm_settings.MEM,
'Available Memory for Computation',
'512'))
# Temporary folder
ProcessingConfig.addSetting(Setting(self.getDescription(),
qsdm_settings.TEMP,
'Temporary Folder',
qsdm_settings.getTEMP()))
# R path
'''To get the parameter of a setting parameter, use SextanteConfig.getSetting(name_of_parameter)'''
def unload(self):
'''Setting should be removed here, so they do not appear anymore
when the plugin is unloaded'''
AlgorithmProvider.unload(self)
ProcessingConfig.removeSetting(qsdm_settings.JAVA_EXEC)
ProcessingConfig.removeSetting(qsdm_settings.MAXENT)
ProcessingConfig.removeSetting(qsdm_settings.MEM)
ProcessingConfig.removeSetting(qsdm_settings.WORK_DIR)
ProcessingConfig.removeSetting(qsdm_settings.TEMP)
def getName(self):
'''This is the name that will appear on the toolbox group.
It is also used to create the command line name of all the algorithms
from this provider'''
return "qsdm"
def getIcon(self):
'''We return the icon for qsdm'''
return QIcon(os.path.dirname(__file__) + os.sep + "icons"+os.sep+"default.png")
def checkRequirements(self):
self.req = {}
# Check for JAVA
if qsdm_settings.javaPath() == '' or None:
self.req["JAVA"] = False
else:
self.req["JAVA"] = True
# Check for MAXENT
if qsdm_settings.maxent() == '' or None or len(qsdm_settings.maxent()) < 4:
self.req["MAXENT"] = False
else:
self.req["MAXENT"] = True
# Check for Numpy
try:
import numpy
self.req["Numpy"] = True
except ImportError:
self.req["Numpy"] = False
# Check for Scipy
try:
import scipy
self.req["Scipy"] = True
except ImportError:
self.req["Scipy"] = False
# Check for Scikits - sklearn
try:
import sklearn
self.req["Scikits"] = True
except ImportError:
self.req["Scikits"] = False
# Pil available
try:
import Image, ImageDraw
self.req["PIL"] = True
except ImportError:
try:
from PIL import Image, ImageDraw
self.req["PIL"] = True
except ImportError:
self.req["PIL"] = False
# R support available?
try:
import rpy2
self.req["R"] = True
except ImportError:
self.req["R"] = False
# # ---- Create Log outputs ---- #
# ProcessingLog.addToLog(ProcessingLog.LOG_INFO,
# "QSDM:JAVA found: " + str(self.req["JAVA"]))
#
# ProcessingLog.addToLog(ProcessingLog.LOG_INFO,
# "QSDM:Scipy found: " + str(self.req["Scipy"]))
def createAlgsList(self):
'''Create list of Arguments based on system-wide configuration and available libraries'''
self.preloadedAlgs = []
# Check available libraries and load tools accordingly
# Data Preperation methods require numpy
if self.req["Numpy"] == True:
from algorithms.DataPreperation import *
#self.preloadedAlgs.append( VectorOutlierSelection() )
# if self.req["PIL"] == True: # Need for range sizes
self.preloadedAlgs.append( CreateRichnessGrid() )
self.preloadedAlgs.append( DataTransformationSimple() )
self.preloadedAlgs.append( RasterUnification() )
# Data Analysis methods
if self.req["Numpy"] == True:
from algorithms.DataAnalysis import *
self.preloadedAlgs.append( RangeShifts() )
self.preloadedAlgs.append( NicheOverlapStatistics() )
# if self.req["Scipy"] == True:
# from algorithms.DataAnalysis import *
# self.preloadedAlgs.append( NovelConditions() )
# Maxent support if JAVA is running and enable the tool. Give Error message to inform user to
# specify the path to the maxent binary if not already set
if self.req["JAVA"] == True:
from algorithms.Maxent import *
self.preloadedAlgs.append( MaxentParameters() ) # Generate optional table displaying the MAXENT parameters
self.preloadedAlgs.append( Maxent() ) # Add Maxent Modelling
self.preloadedAlgs.append( MaxentGUI() ) # Add Maxent Modelling
# Load all the scikit modelling techniques
if self.req["Scikits"] == True:
#from algorithms.sklearn_Regression import *
from algorithms.sklearn_SupportVectorMachines import *
self.preloadedAlgs.append( SupportVectorMachine() ) # Add Support Vector Machines
# if self.req["Scipy"] == True:
# try:
# import algorithms.LinearRegression
for alg in self.preloadedAlgs:
alg.provider = self # reset provider
def _loadAlgorithms(self):
'''Here we fill the list of algorithms in self.algs.
This method is called whenever the list of algorithms should be updated.
If the list of algorithms can change while executing SEXTANTE for QGIS
(for instance, if it contains algorithms from user-defined scripts and
a new script might have been added), you should create the list again
here.
In this case, since the list is always the same, we assign from the pre-made list.
This assignment has to be done in this method even if the list does not change,
since the self.algs list is cleared before calling this method'''
self.algs = self.preloadedAlgs