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 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610
|
"""GNUmed data mining related widgets."""
#================================================================
__author__ = 'karsten.hilbert@gmx.net'
__license__ = 'GPL v2 or later (details at http://www.gnu.org)'
# stdlib
import sys
import os
import fileinput
import logging
# 3rd party
import wx
# GNUmed
if __name__ == '__main__':
sys.path.insert(0, '../../')
from Gnumed.pycommon import gmDispatcher
from Gnumed.pycommon import gmMimeLib
from Gnumed.pycommon import gmTools
from Gnumed.pycommon import gmPG2
from Gnumed.pycommon import gmMatchProvider
from Gnumed.pycommon import gmI18N
from Gnumed.pycommon import gmNetworkTools
from Gnumed.business import gmPerson
from Gnumed.business import gmDataMining
from Gnumed.business import gmPersonSearch
from Gnumed.wxpython import gmGuiHelpers
from Gnumed.wxpython import gmListWidgets
_log = logging.getLogger('gm.ui')
#================================================================
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)
#------------------------------------------------------------
def __get_patient_pk_data_key(self, data=None):
if self.data is None:
return None
if len(self.data) == 0:
return None
if data is None:
data = self.get_selected_item_data(only_one = True)
if data is None:
data = self.get_item_data(item_idx = 0)
if data is None:
return None
if self.patient_key is not None:
try:
data[self.patient_key]
return self.patient_key
except (KeyError, IndexError, TypeError):
# programming error
_log.error('misconfigured identifier column <%s>', self.patient_key)
_log.debug('identifier column not configured, trying to detect')
if data.has_key('pk_patient'):
return u'pk_patient'
if data.has_key('fk_patient'):
return u'fk_patient'
if data.has_key('pk_identity'):
return u'pk_identity'
if data.has_key('fk_identity'):
return u'fk_identity'
if data.has_key('id_identity'):
return u'id_identity'
return gmListWidgets.get_choices_from_list (
parent = self,
msg = _(
'The report result list does not contain any of the following columns:\n'
'\n'
' <%s> / pk_patient / fk_patient\n'
' pk_identity / fk_identity / id_identity\n'
'\n'
'Select the column which contains patient IDs:\n'
) % self.patient_key,
caption = _('Choose column from query results ...'),
choices = data.keys(),
columns = [_('Column name')],
single_selection = True
)
patient_pk_data_key = property(__get_patient_pk_data_key, lambda x:x)
#------------------------------------------------------------
# event handling
#------------------------------------------------------------
def _on_list_item_activated(self, evt):
data = self.get_selected_item_data(only_one = True)
pk_pat_col = self.__get_patient_pk_data_key(data = data)
if pk_pat_col is None:
gmDispatcher.send(signal = 'statustext', msg = _('List not known to be patient-related.'))
return
pat_data = data[pk_pat_col]
try:
pat_pk = int(pat_data)
pat = gmPerson.cIdentity(aPK_obj = pat_pk)
except (ValueError, TypeError):
searcher = gmPersonSearch.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()
from Gnumed.wxpython import gmPatSearchWidgets
gmPatSearchWidgets.set_active_patient(patient = pat)
#================================================================
from Gnumed.wxGladeWidgets import wxgPatientListingPnl
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()
#================================================================
from Gnumed.wxGladeWidgets import wxgDataMiningPnl
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)
self._PRW_report_name.add_callback_on_lose_focus(callback = self._auto_load_report)
#--------------------------------------------------------
def _auto_load_report(self, *args, **kwargs):
if self._TCTRL_query.GetValue() == u'':
if self._PRW_report_name.GetData() is not None:
self._TCTRL_query.SetValue(self._PRW_report_name.GetData())
self._BTN_run.SetFocus()
#--------------------------------------------------------
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]
_log.debug('importing SQL from <%s>', fname)
# act on text files only
mime_type = gmMimeLib.guess_mimetype(fname)
_log.debug('mime type: %s', mime_type)
if not mime_type.startswith('text/'):
_log.debug('not a text file')
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 > 5000:
# 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_contribute_button_pressed(self, evt):
report = self._PRW_report_name.GetValue().strip()
if report == u'':
gmDispatcher.send(signal = 'statustext', msg = _('Report must have a name for contribution.'), beep = False)
return
query = self._TCTRL_query.GetValue().strip()
if query == u'':
gmDispatcher.send(signal = 'statustext', msg = _('Report must have a query for contribution.'), beep = False)
return
do_it = gmGuiHelpers.gm_show_question (
_( 'Be careful that your contribution (the query itself) does\n'
'not contain any person-identifiable search parameters.\n'
'\n'
'Note, however, that no query result data whatsoever\n'
'is included in the contribution that will be sent.\n'
'\n'
'Are you sure you wish to send this query to\n'
'the gnumed community mailing list?\n'
),
_('Contributing custom report')
)
if not do_it:
return
auth = {'user': gmNetworkTools.default_mail_sender, 'password': u'gnumed-at-gmx-net'}
msg = u"""--- This is a report definition contributed by a GNUmed user.
--- Save it as a text file and drop it onto the Report Generator
--- inside GNUmed in order to take advantage of the contribution.
----------------------------------------
--- %s
%s
----------------------------------------
--- The GNUmed client.
""" % (report, query)
if not gmNetworkTools.send_mail (
sender = u'GNUmed Report Generator <gnumed@gmx.net>',
receiver = [u'gnumed-devel@gnu.org'],
subject = u'user contributed report',
message = msg,
encoding = gmI18N.get_encoding(),
server = gmNetworkTools.default_mail_server,
auth = auth
):
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):
# will block when called in text mode (that is, from a terminal, too !)
gmNetworkTools.open_url_in_browser(url = u'http://wiki.gnumed.de/bin/view/Gnumed/DatabaseSchema')
#--------------------------------------------------------
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'')
self._LCTRL_result.set_columns()
#--------------------------------------------------------
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)
if self._PRW_report_name.GetValue().strip() != u'':
gp.title(_('GNUmed report: %s') % self._PRW_report_name.GetValue().strip()[:40])
else:
gp.title(_('GNUmed report results'))
gp.xlabel(x_col)
gp.ylabel(y_col)
try:
gp.plot([ [r[x_col], r[y_col]] for r in self.query_results ])
except StandardError:
_log.exception('unable to plot results from [%s:%s]' % (x_col, y_col))
gmDispatcher.send(signal = 'statustext', msg = _('Error plotting data.'), beep = True)
#--------------------------------------------------------
def _on_waiting_list_button_pressed(self, event):
event.Skip()
pat_pk_key = self._LCTRL_result.patient_pk_data_key
if pat_pk_key is None:
gmGuiHelpers.gm_show_info (
info = _('These report results do not seem to contain per-patient data.'),
title = _('Using report results')
)
return
zone = wx.GetTextFromUser (
_('Enter a waiting zone to put patients in:'),
caption = _('Using report results'),
default_value = _('search results')
)
if zone.strip() == u'':
return
data = self._LCTRL_result.get_selected_item_data(only_one = False)
if data is None:
use_all = gmGuiHelpers.gm_show_question (
title = _('Using report results'),
question = _('No results selected.\n\nTransfer ALL patients from results to waiting list ?'),
cancel_button = True
)
if not use_all:
return
data = self._LCTRL_result.data
comment = self._PRW_report_name.GetValue().strip()
for item in data:
pat = gmPerson.cIdentity(aPK_obj = item[pat_pk_key])
pat.put_on_waiting_list (comment = comment, zone = zone)
#--------------------------------------------------------
def _on_run_button_pressed(self, evt):
self._BTN_visualize.Enable(False)
self._BTN_waiting_list.Enable(False)
user_query = self._TCTRL_query.GetValue().strip().strip(';')
if user_query == u'':
return True
# FIXME: make LIMIT configurable
limit = u'1001'
wrapper_query = u"""
SELECT *
FROM (
%%s
) AS user_query
LIMIT %s
""" % limit
# does user want to insert current patient ID ?
patient_id_token = u'$<ID_active_patient>$'
if user_query.find(patient_id_token) != -1:
# she does, but is it possible ?
curr_pat = gmPerson.gmCurrentPatient()
if not curr_pat.connected:
gmGuiHelpers.gm_show_error (
aMessage = _(
'This query requires a patient to be\n'
'active in the client.\n'
'\n'
'Please activate the patient you are interested\n'
'in and re-run the query.\n'
),
aTitle = _('Active patient query')
)
return False
wrapper_query = u"""
SELECT
%s AS pk_patient,
*
FROM (
%%s
) AS user_query
LIMIT %s
""" % (str(curr_pat.ID), limit)
user_query = user_query.replace(patient_id_token, str(curr_pat.ID))
self._LCTRL_result.set_columns()
query = wrapper_query % user_query
try:
# read-only for safety reasons
rows, idx = gmPG2.run_ro_queries(queries = [{'cmd': query}], get_col_idx = True)
except StandardError:
_log.exception('report query failed')
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 user_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)
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
gmDispatcher.send(signal = 'statustext', msg = _('Found %s results.') % len(rows))
if len(rows) == 1001:
gmGuiHelpers.gm_show_info (
aMessage = _(
'This query returned at least 1001 results.\n'
'\n'
'GNUmed will only show the first 1000 rows.\n'
'\n'
'You may want to narrow down the WHERE conditions\n'
'or use LIMIT and OFFSET to batchwise go through\n'
'all the matching rows.'
),
aTitle = _('Report Generator')
)
rows = rows[:-1] # make it 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:
try:
label = unicode(gmTools.coalesce(row[0], u'')).replace('\n', '<LF>').replace('\r', '<CR>')
except UnicodeDecodeError:
label = _('not unicode()able')
if len(label) > 150:
label = label[:150] + gmTools.u_ellipsis
row_num = self._LCTRL_result.InsertStringItem(sys.maxint, label = label)
for col_idx in range(1, len(row)):
try:
label = unicode(gmTools.coalesce(row[col_idx], u'')).replace('\n', '<LF>').replace('\r', '<CR>')[:250]
except UnicodeDecodeError:
label = _('not unicode()able')
if len(label) > 150:
label = label[:150] + gmTools.u_ellipsis
self._LCTRL_result.SetStringItem (
index = row_num,
col = col_idx,
label = label
)
# must be called explicitely, because string items are set above without calling set_string_items
self._LCTRL_result._invalidate_sorting_metadata()
self._LCTRL_result.set_column_widths()
self._LCTRL_result.set_data(data = rows)
self.query_results = rows
self._BTN_visualize.Enable(True)
self._BTN_waiting_list.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()
|