-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRCA.py
184 lines (140 loc) · 5.66 KB
/
RCA.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
from RCALayout import RCALayout
import GenGIS
from rpy2 import robjects
from CABIN_RCA import *
import wx
import math
import os
class RCA( RCALayout ):
def __init__(self, parent=None):
RCALayout.__init__ ( self, parent )
self.SetIcon(wx.Icon(GenGIS.mainWindow.GetExeDir() + "images/CrazyEye.ico", wx.BITMAP_TYPE_ICO))
# check required data has been loaded
if GenGIS.layerTree.GetNumMapLayers() == 0 or GenGIS.layerTree.GetNumLocationSetLayers() == 0 or GenGIS.layerTree.GetNumSequenceLayers() == 0:
wx.MessageBox("This plugin requires map, location, and sequence data to be loaded.", "Additional data required.")
self.Close()
return
#Diversity measure choices
metric_choices=("Richness","Shannon","Simpson","Pielou","Berger_Parker")
self.metrics=metric_choices
#Populate RCA Model Choices
self.cboModel.Append("atlantic_rca_model")
self.cboModel.SetSelection(0)
# populate Diversity and Count combo boxes with sequence column names
sequenceLayer = GenGIS.layerTree.GetSequenceLayer(0)
seqMetadataFields = sequenceLayer.GetController().GetMetadataFields()
for field in seqMetadataFields:
self.cboDiversity.Append(field)
self.cboCount.Append(field)
#try and set intelligent default choices for combo boxes
good_diversity_choice = "Label"
if good_diversity_choice in seqMetadataFields:
self.cboDiversity.SetSelection(seqMetadataFields.index(good_diversity_choice))
else:
self.cboDiversity.SetSelection(0)
good_count_choice = "Count"
if good_count_choice in seqMetadataFields:
self.cboCount.SetSelection(seqMetadataFields.index(good_count_choice))
else:
self.cboCount.SetSelection(0)
# initialize table
self.table.Freeze()
#allow selecting of columns only
self.table.SetSelectionMode(wx.grid.Grid.wxGridSelectColumns)
#allow scrolling and resizing
self.table.EnableScrolling(True, True)
self.table.EnableDragRowSize(True)
#Create column labels
self.table.DeleteCols(0, self.table.GetNumberCols())
self.table.AppendCols(len(metric_choices))
for i,field in enumerate(metric_choices):
self.table.SetColLabelValue(i, field)
#get site ids
locs = GenGIS.layerTree.GetLocationSetLayer(0).GetAllActiveLocationLayers()
site_ids=[loc.GetController().GetData()["Site ID"] for loc in locs]
#Create row labels
self.table.DeleteRows(0, self.table.GetNumberRows())
self.table.AppendRows(len(site_ids))
for i,value in enumerate(site_ids):
self.table.SetRowLabelValue(i, value)
#Resize row labels
self.table.SetRowLabelSize(wx.grid.GRID_AUTOSIZE)
self.table.SetCellHighlightPenWidth(0)
self.table.AutoSizeColumns()
self.table.Update()
self.table.Thaw()
def OnRun( self, event ):
wx.BeginBusyCursor()
if hasattr(self,'rca'):
self.rca.clearLines()
del self.rca
rca = CABIN_RCA(self.cboDiversity.GetStringSelection(), self.cboCount.GetStringSelection())
rca.Run_RCA(self.cboModel.GetStringSelection())
self.rca=rca
# place results in table
self.table.Freeze()
for col_id in xrange(0,self.table.GetNumberCols()):
col_name = self.table.GetColLabelValue(col_id)
for row_id in xrange(0, self.table.GetNumberRows()):
row_name = self.table.GetRowLabelValue(row_id)
self.table.SetCellValue(row_id,col_id,'%.3f' % self.rca.results[col_name,row_name])
self.table.AutoSizeColumns()
self.table.Thaw()
wx.EndBusyCursor()
def OnPlot(self, event):
if len(self.selectedCols) != 1:
wx.MessageBox("Select one (and only one) column to plot!")
return
if hasattr(self,'rca'):
metric=self.table.GetColLabelValue(self.selectedCols[0])
#Update the viewport using the metric selected by user
self.rca.ViewportPlot(metric)
else:
wx.MessageBox("Please 'Run' this plugin before attempting to update the plot!")
return
def OnOK( self, event ):
self.Close()
def OnHelp( self, event ):
wx.LaunchDefaultBrowser( 'http://kiwi.cs.dal.ca/GenGIS/Description_of_GenGIS_plugins#RCA' )
def OnClose( self, event ):
if hasattr(self,'rca'):
self.rca.clearLines()
del self.rca
#self.Destroy()
event.Skip()
def OnAdd(self, event):
if not hasattr(self,'rca'):
wx.MessageBox("Please 'Run' this plugin before attempting to add results!")
return
if len(self.selectedCols) ==0:
wx.MessageBox("Select one or more columns to add!")
return
for selectedCol in self.selectedCols:
#get name of selected metric
metric=self.table.GetColLabelValue(selectedCol)
data=[]
for locLayer in GenGIS.layerTree.GetLocationSetLayer(0).GetAllActiveLocationLayers():
site_id = locLayer.GetName()
data.append(str(self.rca.results[metric,site_id]))
GenGIS.layerTree.GetLocationSetLayer(0).GetController().AddMetadata(metric, data)
print '\n'+' Selected results added to location metadata table as "' + metric + '".\n\n'
def onSaveFile( self, event ):
try:
self.rca.SaveResults(self.filePicker.GetPath())
except AttributeError:
wx.MessageBox("Please 'Run' this plugin before attempting to save the results!")
return
def OnLabelClick(self, event):
#disable the ability to select the column using the label
pass
def OnSelectedRange( self, event ):
colL = event.GetLeftCol()
colR = event.GetRightCol()
if colL != colR:
self.table.ClearSelection()
# for i in range(self.table.GetNumberCols()):
# self.table.DeselectCol(i)
def OnSelectedCell( self, event ):
"""Internal update to the selection tracking list"""
self.selectedCols = [ event.GetCol() ]
event.Skip()