File: gmDataMiningWidgets.py

package info (click to toggle)
gnumed-client 0.2.8.10-1
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 5,028 kB
  • ctags: 4,693
  • sloc: python: 38,024; sh: 286; makefile: 67
file content (433 lines) | stat: -rw-r--r-- 14,804 bytes parent folder | download
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
"""GNUmed data mining related widgets.
"""
#================================================================
# $Source: /sources/gnumed/gnumed/gnumed/client/wxpython/gmDataMiningWidgets.py,v $
# $Id: gmDataMiningWidgets.py,v 1.6 2007/11/21 14:33:40 ncq Exp $
__version__ = '$Revision: 1.6 $'
__author__ = 'karsten.hilbert@gmx.net'
__license__ = 'GPL (details at http://www.gnu.org)'


# stdlib
import sys, os, fileinput, webbrowser


# 3rd party
import wx


# GNUmed
if __name__ == '__main__':
	sys.path.insert(0, '../../')
from Gnumed.pycommon import gmLog, gmDispatcher, gmMimeLib, gmTools, gmSignals, gmPG2, gmMatchProvider, gmI18N
from Gnumed.business import gmPerson, gmDataMining
from Gnumed.wxpython import gmGuiHelpers, gmListWidgets
from Gnumed.wxGladeWidgets import wxgPatientListingPnl, wxgDataMiningPnl


_log = gmLog.gmDefLog
_log.Log(gmLog.lInfo, __version__)

#================================================================
class cPatientListingCtrl(gmListWidgets.cReportListCtrl):

	def __init__(self, *args, **kwargs):
		"""<patient_key> must index or name a column in self.__data"""
		try:
			self.patient_key = kwargs['patient_key']
			del kwargs['patient_key']
		except KeyError:
			self.patient_key = None

		gmListWidgets.cReportListCtrl.__init__(self, *args, **kwargs)

		self.Bind(wx.EVT_LIST_ITEM_ACTIVATED, self._on_list_item_activated, self)
	#------------------------------------------------------------
	# event handling
	#------------------------------------------------------------
	def _on_list_item_activated(self, evt):
		if self.patient_key is None:
			gmDispatcher.send(signal = 'statustext', msg = _('List not known to be patient-related.'))
			return
		data = self.get_selected_item_data(only_one=True)
		try:
			pat_data = data[self.patient_key]
		except (KeyError, IndexError, TypeError):
			gmGuiHelpers.gm_show_info (
				_(
				'Cannot activate patient.\n\n'
				'The row does not contain a column\n'
				'named or indexed "%s".\n\n'
				) % self.patient_key,
				_('activating patient from list')
			)
			return
		try:
			pat_pk = int(pat_data)
			pat = gmPerson.cIdentity(aPK_obj = pat_pk)
		except (ValueError, TypeError):
			searcher = gmPerson.cPatientSearcher_SQL()
			idents = searcher.get_identities(pat_data)
			if len(idents) == 0:
				gmDispatcher.send(signal = 'statustext', msg = _('No matching patient found.'))
				return
			if len(idents) == 1:
				pat = idents[0]
			else:
				from Gnumed.wxpython import gmPatSearchWidgets
				dlg = gmPatSearchWidgets.cSelectPersonFromListDlg(parent=wx.GetTopLevelParent(self), id=-1)
				dlg.set_persons(persons=idents)
				result = dlg.ShowModal()
				if result == wx.ID_CANCEL:
					dlg.Destroy()
					return
				pat = dlg.get_selected_person()
				dlg.Destroy()

		gmPerson.set_active_patient(patient = pat)
#================================================================
class cPatientListingPnl(wxgPatientListingPnl.wxgPatientListingPnl):

	def __init__(self, *args, **kwargs):

		try:
			button_defs = kwargs['button_defs'][:5]
			del kwargs['button_defs']
		except KeyError:
			button_defs = []

		try:
			msg = kwargs['message']
			del kwargs['message']
		except KeyError:
			msg = None

		wxgPatientListingPnl.wxgPatientListingPnl.__init__(self, *args, **kwargs)

		if msg is not None:
			self._lbl_msg.SetLabel(msg)

		buttons = [self._BTN_1, self._BTN_2, self._BTN_3, self._BTN_4, self._BTN_5]
		for idx in range(len(button_defs)):
			button_def = button_defs[idx]
			if button_def['label'].strip() == u'':
				continue
			buttons[idx].SetLabel(button_def['label'])
			buttons[idx].SetToolTipString(button_def['tooltip'])
			buttons[idx].Enable(True)

		self.Fit()
	#------------------------------------------------------------
	# event handling
	#------------------------------------------------------------
	def _on_BTN_1_pressed(self, event):
		event.Skip()
	#------------------------------------------------------------
	def _on_BTN_2_pressed(self, event):
		event.Skip()
	#------------------------------------------------------------
	def _on_BTN_3_pressed(self, event):
		event.Skip()
	#------------------------------------------------------------
	def _on_BTN_4_pressed(self, event):
		event.Skip()
	#------------------------------------------------------------
	def _on_BTN_5_pressed(self, event):
		event.Skip()
#================================================================
class cDataMiningPnl(wxgDataMiningPnl.wxgDataMiningPnl):

	def __init__(self, *args, **kwargs):
		wxgDataMiningPnl.wxgDataMiningPnl.__init__(self, *args, **kwargs)

		self.__init_ui()

		# make me a file drop target
		dt = gmGuiHelpers.cFileDropTarget(self)
		self.SetDropTarget(dt)
	#--------------------------------------------------------
	def __init_ui(self):
		mp = gmMatchProvider.cMatchProvider_SQL2 (
			queries = [u'select distinct on (label) cmd, label from cfg.report_query where label %(fragment_condition)s or cmd %(fragment_condition)s']
		)
		mp.setThresholds(2,3,5)
		self._PRW_report_name.matcher = mp
		self._PRW_report_name.add_callback_on_selection(callback = self._on_report_selected)
	#--------------------------------------------------------
	def _on_report_selected(self, *args, **kwargs):
		self._TCTRL_query.SetValue(self._PRW_report_name.GetData())
		self._BTN_run.SetFocus()
	#--------------------------------------------------------
	# file drop target API
	#--------------------------------------------------------
	def add_filenames(self, filenames):
		# act on first file only
		fname = filenames[0]
		# act on text files only
		mime_type = gmMimeLib.guess_mimetype(fname)
		if not mime_type.startswith('text/'):
			gmDispatcher.send(signal='statustext', msg = _('Cannot read SQL from [%s]. Not a text file.') % fname, beep = True)
			return False
		# act on "small" files only
		stat_val = os.stat(fname)
		if stat_val.st_size > 2000:
			gmDispatcher.send(signal='statustext', msg = _('Cannot read SQL from [%s]. File too big (> 2000 bytes).') % fname, beep = True)
			return False
		# all checks passed
		for line in fileinput.input(fname):
			self._TCTRL_query.AppendText(line)
	#--------------------------------------------------------
	# notebook plugin API
	#--------------------------------------------------------
	def repopulate_ui(self):
		pass
	#--------------------------------------------------------
	# event handlers
	#--------------------------------------------------------
	def _on_list_item_activated(self, evt):
		data = self._LCTRL_result.get_selected_item_data()
		try:
			pk_pat = data['pk_patient']
		except KeyError:
			gmGuiHelpers.gm_show_warning (
				_(
				'Cannot activate patient.\n\n'
				'The report result list does not contain\n'
				'a column named "pk_patient".\n\n'
				'You may want to use the SQL "AS" column alias\n'
				'syntax to make your query return such a column.\n'
				),
				_('activating patient from report result')
			)
			return
		pat = gmPerson.cPatient(aPK_obj = pk_pat)
		gmPerson.set_active_patient(patient = pat)
	#--------------------------------------------------------
	def _on_contribute_button_pressed(self, evt):
		report = self._PRW_report_name.GetValue().strip()
		if report == u'':
			return
		query = self._TCTRL_query.GetValue().strip()
		if query == u'':
			return

#		auth = {'user': gmTools.default_mail_sender, 'password': u'gm/bugs/gmx'}
		auth = {'user': gmTools.default_mail_sender, 'password': u'gnumed-at-gmx-net'}
		msg = u"""This is a report definition contributed by a GNUmed user:

#--------------------------------------

%s

%s

#--------------------------------------

The GNUmed client.
""" % (report, query)

		if not gmTools.send_mail (
			message = msg,
			auth = auth,
			sender = u'GNUmed Report Generator <gnumed@gmx.net>',
			receiver = [u'gnumed-devel@gnu.org'],
			subject = u'user contributed report'
		):
			gmDispatcher.send(signal = 'statustext', msg = _('Unable to send mail. Cannot contribute report [%s] to GNUmed community.') % report, beep = True)
			return False

		gmDispatcher.send(signal = 'statustext', msg = _('Thank you for your contribution to the GNUmed community !'), beep = False)
		return True
	#--------------------------------------------------------
	def _on_schema_button_pressed(self, evt):
		# new=2: Python 2.5: open new tab
		# will block when called in text mode (that is, from a terminal, too !)
		webbrowser.open(u'http://wiki.gnumed.de/bin/view/Gnumed/DatabaseSchema', new=2, autoraise=1)
	#--------------------------------------------------------
	def _on_delete_button_pressed(self, evt):
		report = self._PRW_report_name.GetValue().strip()
		if report == u'':
			return True
		if gmDataMining.delete_report_definition(name=report):
			self._PRW_report_name.SetText()
			self._TCTRL_query.SetValue(u'')
			gmDispatcher.send(signal='statustext', msg = _('Deleted report definition [%s].') % report, beep=False)
			return True
		gmDispatcher.send(signal='statustext', msg = _('Error deleting report definition [%s].') % report, beep=True)
		return False
	#--------------------------------------------------------
	def _on_clear_button_pressed(self, evt):
		self._PRW_report_name.SetText()
		self._TCTRL_query.SetValue(u'')
	#--------------------------------------------------------
	def _on_save_button_pressed(self, evt):
		report = self._PRW_report_name.GetValue().strip()
		if report == u'':
			gmDispatcher.send(signal='statustext', msg = _('Cannot save report definition without name.'), beep=True)
			return False
		query = self._TCTRL_query.GetValue().strip()
		if query == u'':
			gmDispatcher.send(signal='statustext', msg = _('Cannot save report definition without query.'), beep=True)
			return False
		# FIXME: check for exists and ask for permission
		if gmDataMining.save_report_definition(name=report, query=query, overwrite=True):
			gmDispatcher.send(signal='statustext', msg = _('Saved report definition [%s].') % report, beep=False)
			return True
		gmDispatcher.send(signal='statustext', msg = _('Error saving report definition [%s].') % report, beep=True)
		return False
	#--------------------------------------------------------
	def _on_visualize_button_pressed(self, evt):

		try:
			# better fail early
			import Gnuplot
		except ImportError:
			gmGuiHelpers.gm_show_info (
				aMessage = _('Cannot import GNUplot python module.'),
				aTitle = _('Query result visualizer')
			)
			return

		x_col = gmListWidgets.get_choices_from_list (
			parent = self,
			msg = _('Choose a column to be used as the X-Axis:'),
			caption = _('Choose column from query results ...'),
			choices = self.query_results[0].keys(),
			columns = [_('column name')],
			single_selection = True
		)
		if x_col is None:
			return

		y_col = gmListWidgets.get_choices_from_list (
			parent = self,
			msg = _('Choose a column to be used as the Y-Axis:'),
			caption = _('Choose column from query results ...'),
			choices = self.query_results[0].keys(),
			columns = [_('column name')],
			single_selection = True
		)
		if y_col is None:
			return

		# FIXME: support debugging (debug=1) depending on --debug
		gp = Gnuplot.Gnuplot(persist=1)
		gp.title(_('GNUmed report results:'))
		gp.xlabel(x_col)
		gp.ylabel(y_col)
		gp.plot([ [r[x_col], r[y_col]] for r in self.query_results ])

		return
	#--------------------------------------------------------
	def _on_run_button_pressed(self, evt):

		self._BTN_visualize.Enable(False)

		query = self._TCTRL_query.GetValue().strip().strip(';')
		if query == u'':
			return True

		self._LCTRL_result.set_columns()
		self._LCTRL_result.patient_key = None

		# FIXME: make configurable
		query = u'select * from (' + query + u') as real_query limit 1024'
		try:
			# read-only for safety reasons
			rows, idx = gmPG2.run_ro_queries(queries = [{'cmd': query}], get_col_idx = True)
		except:
			self._LCTRL_result.set_columns([_('Error')])
			t, v = sys.exc_info()[:2]
			rows = [
				[_('The query failed.')],
				[u''],
				[unicode(t)]
			]
			for line in str(v).decode(gmI18N.get_encoding()).split('\n'):
				rows.append([line])
			rows.append([u''])
			for line in query.split('\n'):
				rows.append([line])
			self._LCTRL_result.set_string_items(rows)
			self._LCTRL_result.set_column_widths()
			gmDispatcher.send('statustext', msg = _('The query failed.'), beep = True)
			_log.LogException('report query failed', verbose=True)
			return False

		if len(rows) == 0:
			self._LCTRL_result.set_columns([_('Results')])
			self._LCTRL_result.set_string_items([[_('Report returned no data.')]])
			self._LCTRL_result.set_column_widths()
			gmDispatcher.send('statustext', msg = _('No data returned for this report.'), beep = True)
			return True

		# swap (col_name, col_idx) to (col_idx, col_name) as needed by
		# set_columns() and sort them according to position-in-query
		cols = [ (value, key) for key, value in idx.items() ]
		cols.sort()
		cols = [ pair[1] for pair in cols ]
		self._LCTRL_result.set_columns(cols)
		for row in rows:
			label = unicode(gmTools.coalesce(row[0], u''))
			row_num = self._LCTRL_result.InsertStringItem(sys.maxint, label = label)
			for col_idx in range(1, len(row)):
				self._LCTRL_result.SetStringItem(index = row_num, col = col_idx, label = unicode(gmTools.coalesce(row[col_idx], u'')))
		self._LCTRL_result.set_column_widths()
		self._LCTRL_result.set_data(data = rows)
		try:
			self._LCTRL_result.patient_key = idx['pk_patient']
		except KeyError:
			pass

		self.query_results = rows
		self._BTN_visualize.Enable(True)

		return True
#================================================================
# main
#----------------------------------------------------------------
if __name__ == '__main__':
	from Gnumed.pycommon import gmI18N, gmDateTime

	gmI18N.activate_locale()
	gmI18N.install_domain()
	gmDateTime.init()

	#------------------------------------------------------------
	def test_pat_list_ctrl():
		app = wx.PyWidgetTester(size = (400, 500))
		lst = cPatientListingCtrl(app.frame, patient_key = 0)
		lst.set_columns(['name', 'comment'])
		lst.set_string_items([
			['Kirk', 'Kirk by name'],
			['#12', 'Kirk by ID'],
			['unknown', 'unknown patient']
		])
#		app.SetWidget(cPatientListingCtrl, patient_key = 0)
		app.frame.Show()
		app.MainLoop()
	#------------------------------------------------------------

	test_pat_list_ctrl()

#================================================================
# $Log: gmDataMiningWidgets.py,v $
# Revision 1.6  2007/11/21 14:33:40  ncq
# - fix use of send_mail()
#
# Revision 1.5  2007/09/24 18:31:16  ncq
# - support visualizing data mining results
#
# Revision 1.4  2007/09/10 13:50:05  ncq
# - missing import
#
# Revision 1.3  2007/08/12 00:07:18  ncq
# - no more gmSignals.py
#
# Revision 1.2  2007/07/09 11:06:24  ncq
# - missing import
#
# Revision 1.1  2007/07/09 11:03:49  ncq
# - new file
#
#