Skip to content

Beta display anno #312

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jan 11, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 3 additions & 6 deletions odmtools/controller/frmDataTable.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,14 @@ def __init__(self, parent, **kwargs):
self.memDB = None
DataTable.__init__(self, parent, **kwargs)

def init(self, memDB):
self.memDB = memDB
self.olvDataTable.init(self.memDB)

def init_publishers(self):
Publisher.subscribe(self.onChangeSelection, "changeTableSelection")
Publisher.subscribe(self.onRefresh, "refreshTable")
Publisher.subscribe(self.olvDataTable.onDeselectAll, "deselectAllDataTable")

def init(self, memDB):
self.memDB = memDB
self.olvDataTable.init(self.memDB)
self.init_publishers()

def onItemSelected(self, event):
pass

Expand Down
106 changes: 82 additions & 24 deletions odmtools/controller/olvDataTable.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ def __init__(self, parent, **kwargs):
self.sortedColumnIndex = -1
self.currentItem = None
self.dataframe = None
self.annotations = None
self.annotations_grouped = {}

def init(self, memDB):
self.memDB = memDB
Expand All @@ -28,21 +30,59 @@ def init(self, memDB):
self.oddRowsBackColor = wx.Colour(191, 217, 217)

self.dataframe = self.memDB.getDataValuesDF()
sort_by_index = list(self.dataframe.columns).index("valuedatetime")
self.annotations = self.memDB.get_annotations()

sort_by_index = self.dataframe.columns.tolist().index("valuedatetime")
self.dataframe.sort_values(self.dataframe.columns[sort_by_index], inplace=True)
self.dataObjects = self.dataframe.values.tolist()

self.annotations_grouped = self.__group_annotations()
self.dataObjects = self.__merge_dataframe_with_annotations()

col = self.memDB.get_columns_with_annotations()

columns = \
[ColumnDefn(x.strip(), align="left", valueGetter=i, minimumWidth=125, width=125,
stringConverter='%Y-%m-%d %H:%M:%S' if "valuedatetime" == x.lower() else '%s')
for x, i in self.memDB.getEditColumns()]
stringConverter='%Y-%m-%d %H:%M:%S' if "valuedatetime" == x.lower() else '%s')
for x, i in col]

self.SetColumns(columns)


self.SetObjectGetter(self.ObjectGetter)
self.SetItemCount(len(self.dataframe))
self.SetItemCount(len(self.dataObjects))

def __merge_dataframe_with_annotations(self):
data_list = self.dataframe.values.tolist()
data = data_list

for key, value in self.annotations_grouped.iteritems():
for i in range(0, len(data_list)):
if key in data[i]:
data[i].append(value)
break

return data

def __group_annotations(self):
"""
Ideally, this method should only be called once. Use self.grouped_annotations after calling this method
:return:
"""
anno_list = self.annotations.values.tolist()

anno = {}
for i in range(0, len(anno_list)):
value_id = anno_list[i][1]
annotation_code = anno_list[i][-1]
if value_id in anno:
anno[value_id].append(annotation_code)
else:
anno[value_id] = [annotation_code]

return anno

def EnableSorting(self):
self.Bind(wx.EVT_LIST_COL_CLICK, self.onColSelected)
self.Bind(wx.EVT_LIST_COL_CLICK, self.on_column_selected)
if not self.smallImageList:
self.SetImageLists()
if (not self.smallImageList.HasName(ObjectListView.NAME_DOWN_IMAGE) and
Expand All @@ -56,40 +96,58 @@ def ObjectGetter(self, index):
"""
return self.dataObjects[index % len(self.dataObjects)]

def onColSelected(self, evt):
def on_column_selected(self, event):
"""
Allows users to sort by clicking on columns
"""
if isinstance(self.dataframe, pd.DataFrame):
if self.dataframe.empty:
return
else:
if not self.dataframe:
return
if not isinstance(self.dataframe, pd.DataFrame):
return

if self.dataframe.empty:
return

logger.debug("Column: %s" % evt.m_col)
self.sortColumn(evt.m_col)
if not len(self.dataObjects):
return

self.sortColumn(event.Column)

def sortColumn(self, selected_column):
self.sortAscending = not self.sortAscending

oldSortColumnIndex = self.sortedColumnIndex
self.sortedColumnIndex = selected_column
ascending = self.sortAscending
if ascending:
self.dataframe.sort_values(self.dataframe.columns[selected_column], inplace=True)
self.sortAscending = False
elif not ascending:
self.dataframe.sort_values(self.dataframe.columns[selected_column], ascending=False, inplace=True)
self.sortAscending = True

self._UpdateColumnSortIndicators(selected_column, oldSortColumnIndex)

self.dataObjects = self.dataframe.values.tolist()
if self.GetItemCount:
if selected_column >= len(self.dataframe.columns):
self.dataObjects = self.sort_columns_by_annotation_code(reverse=self.sortAscending)
else:
self.dataframe.sort_values(self.dataframe.columns[selected_column], ascending=self.sortAscending, inplace=True)
self.dataObjects = self.__merge_dataframe_with_annotations()

if self.GetItemCount():
itemFrom = self.GetTopItem()
itemTo = self.GetTopItem() + 1 + self.GetCountPerPage()
itemTo = min(itemTo, self.GetItemCount() - 1)
self.RefreshItems(itemFrom, itemTo)

def sort_columns_by_annotation_code(self, reverse=False):
rows_with_annotation = []
rows_without_annotation = []

column_number_of_dataframe = len(self.dataframe.columns)

for i in self.dataObjects:
if len(i) > column_number_of_dataframe:
rows_with_annotation.append(i)
else:
rows_without_annotation.append(i)

if reverse:
return rows_without_annotation + rows_with_annotation
else:
return rows_with_annotation + rows_without_annotation

def onItemSelected(self, event):
"""

Expand Down
37 changes: 35 additions & 2 deletions odmtools/gui/mnuRibbon.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ def _init_ctrls(self, prnt):

# -------------------------------------------------------------------------------
editPage = RB.RibbonPage(self, wx.ID_ANY, "Edit")
# editPage.Bind(wx.EVT_ENTER_WINDOW, self.on_mouse_enter)

main_panel = RB.RibbonPanel(editPage, wx.ID_ANY, "Main", wx.NullBitmap, wx.DefaultPosition, wx.DefaultSize,
RB.RIBBON_PANEL_NO_AUTO_MINIMISE)
Expand Down Expand Up @@ -204,14 +205,28 @@ def _init_ctrls(self, prnt):
self.CurrPage = 1
self.SetActivePageByIndex(self.CurrPage)

self.bindEvents()
self.__bind_events()
self.initPubSub()

def __init__(self, parent, id, name):
self.parent = parent
self._init_ctrls(parent)

def bindEvents(self):
def on_mouse_enter(self, event):
ribbon_panel = event.GetEventObject().GetParent()
ribbon_panel._hovered = True

self.Refresh()
event.Skip()

def on_mouse_leave(self, event):
ribbon_panel = event.GetEventObject().GetParent()
ribbon_panel._hovered = False

self.Refresh()
event.Skip()

def __bind_events(self):
###Docking Window Selection
self.Bind(RB.EVT_RIBBONBUTTONBAR_CLICKED, self.onDocking, id=wxID_RIBBONVIEWTABLE)
self.Bind(RB.EVT_RIBBONBUTTONBAR_CLICKED, self.onDocking, id=wxID_RIBBONVIEWSERIES)
Expand Down Expand Up @@ -262,6 +277,24 @@ def bindEvents(self):
###Ribbon Event
self.Bind(RB.EVT_RIBBONBAR_PAGE_CHANGED, self.onFileMenu, id=wxID_PANEL1)

# ENTER
self.main_bar.Bind(wx.EVT_ENTER_WINDOW, self.on_mouse_enter) # 1
self.edit_bar.Bind(wx.EVT_ENTER_WINDOW, self.on_mouse_enter) # 2
self.record_bar.Bind(wx.EVT_ENTER_WINDOW, self.on_mouse_enter) # 3
self.PlotsOptions_bar.Bind(wx.EVT_ENTER_WINDOW, self.on_mouse_enter) # 4
self.plots_bar.Bind(wx.EVT_ENTER_WINDOW, self.on_mouse_enter) # 5
self.dateTime_buttonbar.Bind(wx.EVT_ENTER_WINDOW, self.on_mouse_enter) # 6
self.scriptBar.Bind(wx.EVT_ENTER_WINDOW, self.on_mouse_enter) # 7

# LEAVE
self.main_bar.Bind(wx.EVT_LEAVE_WINDOW, self.on_mouse_leave) # 1
self.edit_bar.Bind(wx.EVT_LEAVE_WINDOW, self.on_mouse_leave) # 2
self.record_bar.Bind(wx.EVT_LEAVE_WINDOW, self.on_mouse_leave) # 3
self.PlotsOptions_bar.Bind(wx.EVT_LEAVE_WINDOW, self.on_mouse_leave) # 4
self.plots_bar.Bind(wx.EVT_LEAVE_WINDOW, self.on_mouse_leave) # 5
self.dateTime_buttonbar.Bind(wx.EVT_LEAVE_WINDOW, self.on_mouse_leave) # 6
self.scriptBar.Bind(wx.EVT_LEAVE_WINDOW, self.on_mouse_leave) # 7

def initPubSub(self):
Publisher.subscribe(self.toggleEditButtons, "EnableEditButtons")
Publisher.subscribe(self.enableButtons, "EnablePlotButtons")
Expand Down
35 changes: 30 additions & 5 deletions odmtools/odmdata/memory_database.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ def __init__(self, taskserver=None):
self.df = None
# Series_Service handles remote database
self.series_service = None
self.results_annotations = None

# Memory_service handles in memory database
sm = ServiceManager()
Expand All @@ -43,18 +44,14 @@ def __init__(self, taskserver=None):
#self.annotation_list = pd.DataFrame() columns =['ResultID', 'ValueDateTime', 'ValueID', 'AnnotationID')
#send in engine


def reset_edit(self):
sm = ServiceManager()
self.mem_service = sm.get_series_service(conn_string="sqlite:///:memory:")
setSchema(self.mem_service._session_factory.engine)


def set_series_service(self, service):
self.series_service = service



##############
# DB Queries
##############
Expand All @@ -77,6 +74,13 @@ def getDataValuesDF(self):
logging.debug("done updating memory dataframe")
return self.df

def get_annotations(self):
result_id = self.df.resultid[0]
setSchema(self.series_service._session_factory.engine)
annotation = self.series_service.get_annotations_by_result(resultid=result_id)
self.results_annotations = annotation
return self.results_annotations

def getDataValues(self):
# TODO: fix me! this commit location is only temoporarily. should be flushing so that we can restore
self.mem_service._session.commit()
Expand All @@ -94,6 +98,27 @@ def getEditColumns(self):
return [(x, i) for (i, x) in enumerate(columns)]
# return [(x, i) for (i, x) in enumerate(self.df.columns)]

def get_columns_with_annotations(self):
"""
If results_annotations has not been set then
:return:
"""

if self.results_annotations is None or self.df is None:
print "self.df and self.results_annotations must be a pandas dataframe. Currently they are None"
return []

columns = []
columns.extend(self.df.columns.tolist())

annotation_columns = self.results_annotations.columns.tolist()
index = annotation_columns.index("annotationcode")
annotation_code_column = annotation_columns[index]

columns.append(annotation_code_column)

return [(x, i) for (i, x) in enumerate(columns)]

def getDataValuesforGraph(self, seriesID, noDataValue, startDate=None, endDate=None):
return self.series_service.get_plot_values(seriesID, noDataValue, startDate, endDate)

Expand Down Expand Up @@ -250,7 +275,7 @@ def initEditValues(self, seriesID):
logger.debug("Load series from db")

self.series = self.series_service.get_series(seriesID)
self.df = self.series_service.get_values(series_id= seriesID)
self.df = self.series_service.get_values(series_id=seriesID)

self.editLoaded = True

Expand Down
Loading