-
Notifications
You must be signed in to change notification settings - Fork 2
/
CircuitscapeUtils.py
216 lines (182 loc) · 8.59 KB
/
CircuitscapeUtils.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
# -*- coding: utf-8 -*-
"""
***************************************************************************
CircuitscapeUtils.py
---------------------
Date : May 2014
Copyright : (C) 2014 by Alexander Bruy
Email : alexander dot bruy at gmail dot 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__ = 'Alexander Bruy'
__date__ = 'May 2014'
__copyright__ = '(C) 2014, Alexander Bruy'
# This will get replaced with a git SHA1 when you do a git archive
__revision__ = '$Format:%H$'
import os
import stat
import subprocess
import ConfigParser
from qgis.core import QgsApplication
from processing.core.ProcessingLog import ProcessingLog
from processing.core.ProcessingConfig import ProcessingConfig
from processing.tools.system import *
class CircuitscapeUtils:
LOG_COMMANDS = 'LOG_COMMANDS'
LOG_CONSOLE = 'LOG_CONSOLE'
CIRCUITSCAPE_FOLDER = 'CIRCUITSCAPE_FOLDER'
FOUR_NEIGHBOURS = 'FOUR_NEIGHBOURS'
AVERAGE_CONDUCTANCE = 'AVERAGE_CONDUCTANCE'
PREEMPT_MEMORY = 'PREEMPT_MEMORY'
MAX_CURRENT_MAPS = 'MAX_CURRENT_MAPS'
CUM_MAX_MAPS = 'CUM_MAX_MAPS'
ZERO_FOCAL = 'ZERO_FOCAL'
COMPRESS_OUTPUT = 'COMPRESS_OUTPUT'
LOG_TRANSFORM = 'LOG_TRANSFORM'
@staticmethod
def circuitscapePath():
folder = ProcessingConfig.getSetting(
CircuitscapeUtils.CIRCUITSCAPE_FOLDER)
if folder is None or folder == '':
folder = ''
if isWindows():
testPath = 'C:/Program Files/Circuitscape'
if os.path.exists(os.path.join(testPath, 'cs_run.exe')):
folder = testPath
return folder
@staticmethod
def writeConfiguration():
cfg = ConfigParser.SafeConfigParser()
cfg.add_section('Options for advanced mode')
cfg.set(
'Options for advanced mode', 'ground_file_is_resistances', 'True')
cfg.set('Options for advanced mode', 'remove_src_or_gnd', 'keepall')
cfg.set('Options for advanced mode', 'ground_file', '')
cfg.set('Options for advanced mode', 'use_unit_currents', 'False')
cfg.set('Options for advanced mode', 'source_file', '')
cfg.set('Options for advanced mode', 'use_direct_grounds', 'False')
cfg.add_section('Mask file')
cfg.set('Mask file', 'mask_file', '')
cfg.set('Mask file', 'use_mask', 'False')
cfg.add_section('Calculation options')
cfg.set('Calculation options', 'low_memory_mode', 'False')
cfg.set('Calculation options', 'parallelize', 'False')
cfg.set('Calculation options', 'solver', 'cg+amg')
cfg.set('Calculation options', 'print_timings', 'True')
value = str(
ProcessingConfig.getSetting(CircuitscapeUtils.PREEMPT_MEMORY))
cfg.set('Calculation options', 'preemptive_memory_release', value)
cfg.set('Calculation options', 'print_rusages', 'False')
cfg.set('Calculation options', 'max_parallel', '0')
cfg.add_section('Short circuit regions (aka polygons)')
cfg.set('Short circuit regions (aka polygons)', 'polygon_file', '')
cfg.set('Short circuit regions (aka polygons)',
'use_polygons', 'False')
cfg.add_section('Options for one-to-all and all-to-one modes')
cfg.set('Options for one-to-all and all-to-one modes',
'use_variable_source_strengths', 'False')
cfg.set('Options for one-to-all and all-to-one modes',
'variable_source_file', '')
cfg.add_section('Output options')
cfg.set('Output options', 'set_null_currents_to_nodata', 'False')
value = str(ProcessingConfig.getSetting(CircuitscapeUtils.ZERO_FOCAL))
cfg.set('Output options', 'set_focal_node_currents_to_zero', value)
cfg.set('Output options', 'set_null_voltages_to_nodata', 'False')
value = str(
ProcessingConfig.getSetting(CircuitscapeUtils.COMPRESS_OUTPUT))
cfg.set('Output options', 'compress_grids', value)
cfg.set('Output options', 'write_cur_maps', 'True')
cfg.set('Output options', 'write_volt_maps', 'True')
cfg.set('Output options', 'output_file', '')
value = str(
ProcessingConfig.getSetting(CircuitscapeUtils.CUM_MAX_MAPS))
cfg.set('Output options', 'write_cum_cur_map_only', value)
value = str(
ProcessingConfig.getSetting(CircuitscapeUtils.LOG_TRANSFORM))
cfg.set('Output options', 'log_transform_maps', value)
value = str(
ProcessingConfig.getSetting(CircuitscapeUtils.MAX_CURRENT_MAPS))
cfg.set('Output options', 'write_max_cur_maps', value)
cfg.add_section('Options for reclassification of habitat data')
cfg.set('Options for reclassification of habitat data',
'reclass_file', '')
cfg.set('Options for reclassification of habitat data',
'use_reclass_table', 'False')
cfg.add_section('Logging Options')
cfg.set('Logging Options', 'log_level', 'INFO')
cfg.set('Logging Options', 'log_file', 'None')
cfg.set('Logging Options', 'profiler_log_file', 'None')
cfg.set('Logging Options', 'screenprint_log', 'False')
cfg.add_section(
'Options for pairwise and one-to-all and all-to-one modes')
cfg.set('Options for pairwise and one-to-all and all-to-one modes',
'included_pairs_file', '')
cfg.set('Options for pairwise and one-to-all and all-to-one modes',
'use_included_pairs', 'False')
cfg.set('Options for pairwise and one-to-all and all-to-one modes',
'point_file', '')
cfg.add_section('Connection scheme for raster habitat data')
value = str(
ProcessingConfig.getSetting(CircuitscapeUtils.AVERAGE_CONDUCTANCE))
cfg.set('Connection scheme for raster habitat data',
'connect_using_avg_resistances', value)
value = str(
ProcessingConfig.getSetting(CircuitscapeUtils.FOUR_NEIGHBOURS))
cfg.set('Connection scheme for raster habitat data',
'connect_four_neighbors_only', value)
cfg.add_section('Habitat raster or graph')
cfg.set('Habitat raster or graph',
'habitat_map_is_resistances', 'True')
cfg.set('Habitat raster or graph', 'habitat_file', '')
cfg.add_section('Circuitscape mode')
cfg.set('Circuitscape mode', 'data_type', 'raster')
cfg.set('Circuitscape mode', 'scenario', '')
iniPath = getTempFilename('.ini')
with open(iniPath, 'wb') as f:
cfg.write(f)
return iniPath
@staticmethod
def batchJobFilename():
if isWindows():
fileName = 'circuitscape_batch_job.bat'
else:
fileName = 'circuitscape_batch_job.sh'
batchFile = userFolder() + os.sep + fileName
return batchFile
@staticmethod
def createBatchJobFileFromCommands(commands):
batchFile = open(CircuitscapeUtils.batchJobFilename(), 'w')
for command in commands:
batchFile.write(command.encode('utf8') + '\n')
batchFile.write('exit')
batchFile.close()
@staticmethod
def executeCircuitscape(command, progress):
if isWindows():
command = ['cmd.exe', '/C ', CircuitscapeUtils.batchJobFilename()]
else:
os.chmod(CircuitscapeUtils.batchJobFilename(), stat.S_IEXEC
| stat.S_IREAD | stat.S_IWRITE)
command = [CircuitscapeUtils.batchJobFilename()]
fused_command = ''.join(['"%s" ' % c for c in command])
loglines = []
loglines.append('Circuitscape execution console output')
proc = subprocess.Popen(
fused_command,
shell=True,
stdout=subprocess.PIPE,
stdin=subprocess.PIPE,
stderr=subprocess.STDOUT,
universal_newlines=True,
).stdout
for line in iter(proc.readline, ''):
loglines.append(line)
if ProcessingConfig.getSetting(CircuitscapeUtils.LOG_CONSOLE):
ProcessingLog.addToLog(ProcessingLog.LOG_INFO, loglines)