Skip to content
This repository has been archived by the owner on Nov 5, 2020. It is now read-only.

Commit

Permalink
Add user interface for automated classification (#222)
Browse files Browse the repository at this point in the history
  • Loading branch information
paulmueller committed Aug 2, 2018
1 parent b96c58d commit 090de8c
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 37 deletions.
101 changes: 89 additions & 12 deletions shapeout/gui/controls_analyze.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,68 @@
from . import confparms
from .controls_subpanel import SubPanel



class ClassifyStringDialog(wx.Dialog):

def __init__(self, analysis, parent, *args, **kw):
super(ClassifyStringDialog, self).__init__(parent, *args, **kw)
self.analysis = analysis
self.parent = parent
self.InitUI()
self.SetSize((250, 200))
self.SetTitle("Treatment and control identifiers")


def InitUI(self):
grid = wx.GridBagSizer(hgap=3)

grid.Add(wx.StaticText(self, label="Control"), (0,0))
grid.Add(wx.StaticText(self, label="Treatment"), (1,0))
grid.Add(wx.StaticText(self, label="Reservoir Control"), (2,0))
grid.Add(wx.StaticText(self, label="Reservoir Treatment"), (3,0))

self.wxctl = wx.TextCtrl(self, value="control")
self.wxtrt = wx.TextCtrl(self, value="")
self.wxctl_res = wx.TextCtrl(self, value="")
self.wxtrt_res = wx.TextCtrl(self, value="")

grid.Add(self.wxctl, (0,1))
grid.Add(self.wxtrt, (1,1))
grid.Add(self.wxctl_res, (2,1))
grid.Add(self.wxtrt_res, (3,1))

applyButton = wx.Button(self, label='Apply')
closeButton = wx.Button(self, label='Close')

grid.Add(applyButton, (4,0))
grid.Add(closeButton, (4,1))

self.SetSizer(grid)

applyButton.Bind(wx.EVT_BUTTON, self.OnApply)
closeButton.Bind(wx.EVT_BUTTON, self.OnClose)


def OnClose(self, e):
self.Destroy()


def OnApply(self, e):
treatment, repetition = lin_mix_mod.classify_treatment_repetition(
analysis=self.analysis,
id_ctl=self.wxctl.GetValue(),
id_trt=self.wxtrt.GetValue(),
id_ctl_res=self.wxctl_res.GetValue(),
id_trt_res=self.wxtrt_res.GetValue())

for mm, trt, rep in zip(self.analysis, treatment, repetition):
mm.config["analysis"]["regression treatment"] = trt
mm.config["analysis"]["regression repetition"] = rep

self.parent.update_classification()


class SubPanelAnalyze(SubPanel):
def __init__(self, parent, *args, **kwargs):
SubPanel.__init__(self, parent, *args, **kwargs)
Expand Down Expand Up @@ -59,11 +121,16 @@ def make_analysis_choices(self, analysis):
self.WXCB_axes.SetSelection(axid)
sizer_bag.Add(self.WXCB_axes, (1,1), span=wx.GBSpan(1,2),
flag=wx.EXPAND|wx.ALL)



btn_auto = wx.Button(self, label="Autodetect classification")
sizer_bag.Add(btn_auto, (2,1), span=wx.GBSpan(1,2), flag=wx.EXPAND)
self.Bind(wx.EVT_BUTTON, self.OnAuto, btn_auto)

# Header for table
sizer_bag.Add(wx.StaticText(self, label="Data set"), (2,0), span=wx.GBSpan(1,1))
sizer_bag.Add(wx.StaticText(self, label="Interpretation"), (2,1), span=wx.GBSpan(1,1))
sizer_bag.Add(wx.StaticText(self, label="Repetition"), (2,2), span=wx.GBSpan(1,1))
sizer_bag.Add(wx.StaticText(self, label="Data set"), (3,0), span=wx.GBSpan(1,1))
sizer_bag.Add(wx.StaticText(self, label="Interpretation"), (3,1), span=wx.GBSpan(1,1))
sizer_bag.Add(wx.StaticText(self, label="Repetition"), (3,2), span=wx.GBSpan(1,1))

treatments = ["None", "Control", "Treatment",
"Reservoir Control", "Reservoir Treatment"]
Expand All @@ -73,7 +140,7 @@ def make_analysis_choices(self, analysis):

for ii, mm in enumerate(analysis.measurements):
# title
sizer_bag.Add(wx.StaticText(self, label=mm.title), (3+ii,0), span=wx.GBSpan(1,1))
sizer_bag.Add(wx.StaticText(self, label=mm.title), (4+ii,0), span=wx.GBSpan(1,1))
# treatment
cbgtemp = wx.ComboBox(self, -1, choices=treatments,
name=mm.identifier,
Expand All @@ -85,17 +152,14 @@ def make_analysis_choices(self, analysis):
mm.config["experiment"]["date"],
mm.config["experiment"]["time"])
cbgtemp.SetToolTip(wx.ToolTip(tip))
if mm.title.lower().count("control") or ii==0:
cbgtemp.SetSelection(1)
else:
cbgtemp.SetValue(mm.config["analysis"]["regression treatment"])
sizer_bag.Add(cbgtemp, (3+ii,1), flag=wx.EXPAND|wx.ALL)
cbgtemp.SetValue(mm.config["analysis"]["regression treatment"])
sizer_bag.Add(cbgtemp, (4+ii,1), flag=wx.EXPAND|wx.ALL)
# repetition
cbgtemp2 = wx.wx.SpinCtrl(self, -1, min=1, max=999,
initial=1)
cbgtemp2.SetValue(mm.config["analysis"]["regression repetition"]-1)
cbgtemp2.SetValue(mm.config["analysis"]["regression repetition"])

sizer_bag.Add(cbgtemp2, (3+ii,2), flag=wx.EXPAND|wx.ALL)
sizer_bag.Add(cbgtemp2, (4+ii,2), flag=wx.EXPAND|wx.ALL)

self.WXCB_treatment.append(cbgtemp)
self.WXCB_repetition.append(cbgtemp2)
Expand Down Expand Up @@ -156,6 +220,11 @@ def OnApply(self, e=None):
webbrowser.open(fd.name)


def OnAuto(self, e=None):
dlg = ClassifyStringDialog(self.analysis, self)
dlg.ShowModal()


def OnReset(self, e=None):
"""
Reset everything in the analysis tab.
Expand Down Expand Up @@ -193,6 +262,14 @@ def update_info_text(self, e=None):
self.Layout()


def update_classification(self):
for mm, wxtrt, wxrep in zip(self.analysis,
self.WXCB_treatment,
self.WXCB_repetition):
wxtrt.SetValue(mm.config["analysis"]["regression treatment"])
wxrep.SetValue(mm.config["analysis"]["regression repetition"])


def UpdatePanel(self, analysis=None):
if analysis is None:
analysis = self.analysis
Expand Down
9 changes: 7 additions & 2 deletions shapeout/lin_mix_mod.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,13 @@ def classify_treatment_repetition(analysis, id_ctl="co", id_trt="",
else:
idlist.append(["none", mm])

# extract treatment
treatment = [tt if tt != "none" else None for (tt, mm) in idlist]
# extract and rename treatment
treatment = [tt for (tt, mm) in idlist]
treatment = [tt.replace("res", "Reservoir") for tt in treatment]
treatment = [tt.replace("ctl", "Control") for tt in treatment]
treatment = [tt.replace("trt", "Treatment") for tt in treatment]
treatment = [tt.replace("none", "None") for tt in treatment]

assert len(treatment) == len(analysis)
# identify timeunit via similarity analysis
ctl_str = [mm.title if tt == "ctl" else "" for (tt, mm) in idlist]
Expand Down
54 changes: 33 additions & 21 deletions tests/test_linmixmod2.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,32 @@
"""
from __future__ import division, print_function, unicode_literals

import copy

import numpy as np

from shapeout.lin_mix_mod import linmixmod, diffdef


def test_linmixmod():
xs = [
[100, 99, 80, 120, 140, 150, 100, 100, 110, 111,
140, 145], # Larger values (Control Channel1)
[20, 10, 5, 16, 14, 22, 27, 26, 5, 10, 11, 8, 15, 17,
20, 9], # Smaller values (Control Reservoir1)
[115, 110, 90, 110, 145, 155, 110, 120, 115, 120, 120,
150, 100, 90, 100], # Larger values (Control Channel2)
[30, 30, 15, 26, 24, 32, 37, 36, 15, 20, 21, 18, 25,
27, 30, 19], # Smaller values (Control Reservoir2)
[150, 150, 130, 170, 190, 250, 150, 150, 160, 161, 180, 195, 130,
120, 125, 130, 125], # Larger values (Treatment Channel1)
[2, 1, 5, 6, 4, 2, 7, 6, 5, 10, 1, 8, 5, 7, 2, 9, 11,
8, 13], # Smaller values (Treatment Reservoir1)
[155, 155, 135, 175, 195, 255, 155, 155, 165, 165, 185, 200, 135, 125,
130, 135, 140, 150, 135, 140], # Larger values (Treatment Channel2)
[25, 15, 19, 26, 44, 42, 35, 20, 15, 10, 11, 28, 35, 10, 25, 13]] # Smaller values (Treatment Reservoir2)
xs = [
[100, 99, 80, 120, 140, 150, 100, 100, 110, 111,
140, 145], # Larger values (Control Channel1)
[20, 10, 5, 16, 14, 22, 27, 26, 5, 10, 11, 8, 15, 17,
20, 9], # Smaller values (Control Reservoir1)
[115, 110, 90, 110, 145, 155, 110, 120, 115, 120, 120,
150, 100, 90, 100], # Larger values (Control Channel2)
[30, 30, 15, 26, 24, 32, 37, 36, 15, 20, 21, 18, 25,
27, 30, 19], # Smaller values (Control Reservoir2)
[150, 150, 130, 170, 190, 250, 150, 150, 160, 161, 180, 195, 130,
120, 125, 130, 125], # Larger values (Treatment Channel1)
[2, 1, 5, 6, 4, 2, 7, 6, 5, 10, 1, 8, 5, 7, 2, 9, 11,
8, 13], # Smaller values (Treatment Reservoir1)
[155, 155, 135, 175, 195, 255, 155, 155, 165, 165, 185, 200, 135, 125,
130, 135, 140, 150, 135, 140], # Larger values (Treatment Channel2)
[25, 15, 19, 26, 44, 42, 35, 20, 15, 10, 11, 28, 35, 10, 25, 13]] # Smaller values (Treatment Reservoir2)


def test_linmixmod_1():
# 1.: Differential Deformation in a linear mixed model
treatment1 = ['Control', 'Reservoir Control', 'Control', 'Reservoir Control',
'Treatment', 'Reservoir Treatment', 'Treatment', 'Reservoir Treatment']
Expand All @@ -44,15 +47,22 @@ def test_linmixmod():
assert np.allclose(
[Result_1["p-Value (Likelihood Ratio Test)"]], [0.000602622503360039])


def test_linmixmod_2():
# 2.: Differential Deformation in a generalized linear mixed model
Result_2 = linmixmod(xs=xs, treatment=treatment1,
timeunit=timeunit1, model='glmm')
treatment2 = ['Control', 'Reservoir Control', 'Control', 'Reservoir Control',
'Treatment', 'Reservoir Treatment', 'Treatment', 'Reservoir Treatment']
timeunit2 = [1, 1, 2, 2, 1, 1, 2, 2]
Result_2 = linmixmod(xs=xs, treatment=treatment2,
timeunit=timeunit2, model='glmm')
assert np.allclose([Result_2["Estimate"]], [4.5385848573783898])
assert 'BOOTSTAP-DISTRIBUTIONS' in Result_1['Full Summary']
assert 'BOOTSTAP-DISTRIBUTIONS' in Result_2['Full Summary']
assert 'GENERALIZED' in Result_2['Full Summary']
assert np.allclose(
[Result_2["p-Value (Likelihood Ratio Test)"]], [0.000556063024310929])


def test_linmixmod_3():
# 3.: Original values in a linear mixed model
#'Reservoir' Measurements are now Controls and 'Channel' measurements are Treatments
# This does not use differential deformation in linmixmod()
Expand All @@ -67,7 +77,9 @@ def test_linmixmod():
assert np.allclose(
[Result_3["p-Value (Likelihood Ratio Test)"]], [0.000331343267412872])

# 3.: Original values in a generalized linear mixed model

def test_linmixmod_4():
# 4.: Original values in a generalized linear mixed model
# This does not use differential deformation in linmixmod()
treatment4 = ['Treatment', 'Control', 'Treatment', 'Control',
'Treatment', 'Control', 'Treatment', 'Control']
Expand Down
7 changes: 5 additions & 2 deletions tests/test_linmixmod_classification.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def test_classify_treatment_repetition_sinple():
measurements += [ctl, trt]
ana = Analysis(measurements)
treatment, repetition = classify_treatment_repetition(ana, id_ctl="control")
assert treatment == ["ctl", "trt"] * 5
assert treatment == ["Control", "Treatment"] * 5
assert np.all(repetition == np.repeat(np.arange(5), 2) + 1)


Expand Down Expand Up @@ -73,7 +73,10 @@ def test_classify_treatment_repetition_advanced():
# Repetitions 1 and 2 are put in the end b/c "11" and "12" are better
# matches than "1" and "2".
repetition = np.roll(repetition, 8)
assert treatment == ["ctl", "trt", "res ctl", "res trt"] * 12
assert treatment == ["Control",
"Treatment",
"Reservoir Control",
"Reservoir Treatment"] * 12
assert np.all(repetition == np.repeat(np.arange(12), 4) + 1)


Expand Down

0 comments on commit 090de8c

Please sign in to comment.