Ejemplo n.º 1
0
class FrameBasedEvolutionWindow(wx.Frame):
    """ Window for temporal evolution of parameters obtained from interpolated HR"""

    sbDefaultText = ""

    def __init__(self, parent, id, title, dm):

        self.dm = dm

        wx.Frame.__init__(self, parent, -1, title)

        if platform != "darwin":
            icon = wx.Icon("LogoIcon.ico", wx.BITMAP_TYPE_ICO)
            self.SetIcon(icon)

        self.Bind(wx.EVT_CLOSE, self.OnEnd)

        self.panel = wx.Panel(self)
        self.WindowParent = parent

        if ColoredBGPlots:
            self.fig = matplotlib.figure.Figure(facecolor=TemporalBGColor)
        else:
            self.fig = matplotlib.figure.Figure()

        self.canvas = FigureCanvas(self.panel, -1, self.fig)

        self.mainBox = wx.BoxSizer(wx.HORIZONTAL)
        self.mainBox.Add(self.canvas, 1, wx.LEFT | wx.TOP | wx.GROW)

        self.vboxRightArea = wx.BoxSizer(wx.VERTICAL)

        self.AllBands, self.VisibleBands = self.dm.GetVisibleBands()
        self.insertBandsSelector()

        self.refreshButton = wx.Button(self.panel, -1, "Refresh")
        self.vboxRightArea.Add(self.refreshButton,
                               0,
                               border=borderSmall,
                               flag=wx.ALL | wx.ALIGN_LEFT)
        self.Bind(wx.EVT_BUTTON, self.onRefresh, self.refreshButton)

        self.vboxRightArea.AddStretchSpacer(prop=1)

        self.signifButton = wx.Button(self.panel,
                                      -1,
                                      "Significance...",
                                      size=buttonSizeFrameBased)
        self.Bind(wx.EVT_BUTTON, self.OnSignif, id=self.signifButton.GetId())
        self.signifButton.SetToolTip(
            wx.ToolTip("Click to perform significance analysis"))
        self.vboxRightArea.Add(self.signifButton,
                               0,
                               border=borderSmall,
                               flag=wx.ALL | wx.ALIGN_RIGHT)
        if platform != 'darwin' and ColoredButtons:
            self.signifButton.SetBackgroundColour(SignifBGColor)
        if self.dm.HasVisibleEpisodes():
            self.signifButton.Enable()
        else:
            self.signifButton.Disable()

        self.exportButton = wx.Button(self.panel,
                                      -1,
                                      "Export txt...",
                                      size=buttonSizeFrameBased)
        self.Bind(wx.EVT_BUTTON, self.OnExport, id=self.exportButton.GetId())
        self.exportButton.SetToolTip(wx.ToolTip("Click to export to txt file"))
        self.vboxRightArea.Add(self.exportButton,
                               0,
                               border=borderSmall,
                               flag=wx.ALL | wx.ALIGN_RIGHT)
        #self.exportButton.Disable()

        self.endButton = wx.Button(self.panel,
                                   -1,
                                   "End",
                                   size=buttonSizeFrameBased)
        self.Bind(wx.EVT_BUTTON, self.OnEnd, id=self.endButton.GetId())
        self.endButton.SetToolTip(wx.ToolTip("Click to close window"))
        self.vboxRightArea.Add(self.endButton,
                               0,
                               border=borderSmall,
                               flag=wx.ALL | wx.ALIGN_RIGHT)

        self.mainBox.Add(self.vboxRightArea,
                         0,
                         flag=wx.EXPAND | wx.ALL,
                         border=borderBig)

        self.panel.SetSizer(self.mainBox)

        self.sb = self.CreateStatusBar()
        self.sbDefaultText = "Window size: " + str(
            dm.data["windowsize"]) + " sec."
        self.sbDefaultText += "   -   "
        self.sbDefaultText += "Window shift: " + str(
            dm.data["windowshift"]) + " sec."
        self.sbDefaultText += "   -   "
        numFrames = dm.GetNumFrames(dm.data["interpfreq"],
                                    dm.data["windowsize"],
                                    dm.data["windowshift"])
        self.sbDefaultText += "No. of frames: " + str(numFrames)

        self.sb.SetStatusText(self.sbDefaultText)

        self.dm.CreatePlotFBEmbedded(self.fig)
        self.canvas.draw()

        defSize, minSize = Utils.RecalculateWindowSizes(
            mainWindowSize, mainWindowMinSize)
        self.SetSize(defSize)
        self.SetMinSize(minSize)

        self.Show(True)
        self.Layout()
        self.canvas.SetFocus()

    def insertBandsSelector(self):

        # Begin of select/unselect bands checklist box

        sbBands = wx.StaticBox(self.panel, label="Visible bands")
        sbBandsSizer = wx.StaticBoxSizer(sbBands, wx.VERTICAL)

        self.bandsRB = []
        for band in self.AllBands:
            if len(self.bandsRB) == 0:
                tmp = wx.CheckBox(self.panel, label=band, style=wx.RB_GROUP)
            else:
                tmp = wx.CheckBox(self.panel, label=band)
            tmp.SetValue(False)
            if band in self.VisibleBands:
                tmp.SetValue(True)
            if band == "Heart rate":
                tmp.Disable()
            self.bandsRB.append(tmp)
            sbBandsSizer.Add(tmp, wx.EXPAND)

        self.vboxRightArea.Insert(0,
                                  sbBandsSizer,
                                  flag=wx.ALL,
                                  border=borderSmall)

    def onRefresh(self, event):
        checkedBands = []
        for indexband in range(len(self.AllBands)):
            bandname = self.AllBands[indexband]
            bandstatus = self.bandsRB[indexband].GetValue()
            if bandstatus:
                checkedBands.append(bandname)
            #print "Band: ",bandname,"  - Checked: ",bandstatus

        self.dm.SetVisibleBands(checkedBands)

        self.dm.CreatePlotFBEmbedded(self.fig)
        self.canvas.draw()

    def Refresh(self):
        self.dm.CreatePlotFBEmbedded(self.fig)
        self.canvas.draw()

        if self.dm.HasVisibleEpisodes():
            self.signifButton.Enable()
        else:
            self.signifButton.Disable()

    def OnEnd(self, event):
        self.WindowParent.OnFrameBasedEnded()
        self.Destroy()

    def OnExport(self, event):
        exportSettingsWindow = FrameBasedExportSettings(
            self, -1, "Export options", self.dm)
        self.exportButton.Disable()

    def OnExportEnded(self):
        self.exportButton.Enable()

    def OnSignif(self, event):
        SignificanceWindow(self, -1, 'Significance analysis', self.dm)
        self.signifButton.Disable()
        self.WindowParent.signifWindowPresent = True
        self.WindowParent.RefreshMainWindowButtons()
        # exportSettingsWindow=FrameBasedExportSettings(self,-1,"Export options", self.dm)
        # self.exportButton.Disable()
    def OnSignifEnded(self):
        self.signifButton.Enable()
        self.WindowParent.signifWindowPresent = False
        self.WindowParent.RefreshMainWindowButtons()
Ejemplo n.º 2
0
class SignificanceWindow(wx.Frame):
    """Window for significance analysis"""

    sbSignifText="  Keys: 'i'/'m' increase/lower no. of bins, '0' resets, 's' saves plot"

    def __init__(self,parent,id,title,dm):
        wx.Frame.__init__(self, parent, -1, title, size=signifWindowSize)
        
        if platform != "darwin":
            icon = wx.Icon("LogoIcon.ico", wx.BITMAP_TYPE_ICO)
            self.SetIcon(icon)

        self.signifNumBins=signifNumBins
        self.dm = dm
        
        self.Bind(wx.EVT_CLOSE,self.OnEnd)  
        
        self.WindowParent=parent
        sizer=wx.BoxSizer(wx.VERTICAL)

        panel = wx.Panel(self)

        # -------------- Begin of parameter selector

        sbParam = wx.StaticBox(panel, label="Parameter")
        sbParamSizer = wx.StaticBoxSizer(sbParam, wx.VERTICAL)
        sbParamSizer1 = wx.BoxSizer(wx.HORIZONTAL)
        sbParamSizer2 = wx.BoxSizer(wx.HORIZONTAL)
        
        AllBandsOrig = self.dm.GetVisibleBands()[0]
        AllBands=list(AllBandsOrig)
        AllBands.remove("Heart rate")

        bandsRB=[]

        for band in AllBands:
            if len(bandsRB)==0:
                tmp = wx.RadioButton(panel, label=band, style=wx.RB_GROUP)
                tmp.SetValue(True)
            else:
                tmp = wx.RadioButton(panel, label=band)
            bandsRB.append(tmp)
            if len(bandsRB)<=6:
                sbParamSizer1.Add(tmp, wx.EXPAND) 
            else:      
                sbParamSizer2.Add(tmp, wx.EXPAND) 
        
        sbParamSizer.Add(sbParamSizer1,flag=wx.EXPAND|wx.TOP|wx.LEFT|wx.RIGHT,border=borderBig)
        sbParamSizer.Add(sbParamSizer2,flag=wx.EXPAND|wx.TOP|wx.LEFT|wx.RIGHT,border=borderBig)
        sizer.Add(sbParamSizer,flag=wx.EXPAND|wx.TOP|wx.LEFT|wx.RIGHT,border=borderBig)

        for eachRB in bandsRB:
            self.Bind(wx.EVT_RADIOBUTTON,self.OnParam,eachRB)

        self.ActiveParam = AllBands[0]

        # -------------- End of parameter selector



        # -------------- Begin of tags selector

        sbTags = wx.StaticBox(panel, label="Episodes")
        sbTagsSizer = wx.StaticBoxSizer(sbTags, wx.HORIZONTAL)

        # tagsRB = []
        self.AllTags = self.dm.GetVisibleEpisodes()[0]
        self.ActiveTagLeft = self.AllTags[0]
        self.ActiveTagRight = None
        self.dm.SetSignifPlotParams(self.ActiveTagLeft,self.ActiveTagRight,self.ActiveParam)
        
        self.cbComboLeft=wx.ComboBox(panel,
            choices=self.AllTags,
            value=self.ActiveTagLeft,
            style=wx.CB_DROPDOWN|wx.CB_READONLY
            )
        sbTagsSizer.Add(self.cbComboLeft,flag=wx.ALL | wx.EXPAND, border=borderSmall)
        self.Bind(wx.EVT_COMBOBOX, self.OnComboLeft, id=self.cbComboLeft.GetId())
        

        sbTagsSizer.AddStretchSpacer(prop=1)

        ChoicesRight = ["Outside "+self.ActiveTagLeft]+self.AllTags
        ChoicesRight.remove(self.ActiveTagLeft)

        self.cbComboRight=wx.ComboBox(panel,
            choices=ChoicesRight, 
            value="Outside "+self.ActiveTagLeft, 
            style=wx.CB_DROPDOWN|wx.CB_READONLY
            )
        sbTagsSizer.Add(self.cbComboRight,flag=wx.ALL | wx.EXPAND, border=borderSmall)
        self.Bind(wx.EVT_COMBOBOX, self.OnComboRight, id=self.cbComboRight.GetId())


        sizer.Add(sbTagsSizer,flag=wx.EXPAND|wx.TOP|wx.LEFT|wx.RIGHT,border=borderBig)

        # -------------- End of tags selector


        # -------------- Begin of figure
        if ColoredBGPlots:
            self.fig = matplotlib.figure.Figure((5.0, 4.0),facecolor=SignifBGColor)
        else:
            self.fig = matplotlib.figure.Figure((5.0, 4.0))
            
        self.fig.subplots_adjust(left=0.05, bottom=0.18, right=0.98, top=0.92, wspace=0.20, hspace=0.15)
        self.canvas = FigureCanvas(panel, -1, self.fig)
        
        self.axes = self.fig.add_subplot(111)

        sizer.Add(self.canvas, 1, wx.LEFT | wx.TOP | wx.GROW)

        # -------------- End of figure

        # -------------- Begin of textbox and buttons

        hbox = wx.BoxSizer(wx.HORIZONTAL)   


        self.textOutput = wx.TextCtrl(panel, id, 'Information', size=(400, 75), style=wx.TE_READONLY|wx.TE_MULTILINE|wx.TE_RICH2)
        self.textOutput.SetFont(wx.Font(11, wx.FONTFAMILY_SWISS, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL));
        hbox.Add(self.textOutput, 1, wx.LEFT | wx.TOP | wx.GROW)

        endButton = wx.Button(panel, -1, "Close", size=buttonSizeSignif)
        self.Bind(wx.EVT_BUTTON, self.OnEnd, id=endButton.GetId())
        endButton.SetToolTip(wx.ToolTip("Click to close window"))
        hbox.Add(endButton, 0, border=borderBig, flag=wx.LEFT|wx.ALIGN_BOTTOM)
                
        sizer.Add(hbox, 0, flag=wx.EXPAND|wx.ALL, border=borderBig)

        # -------------- End of textbox and buttons

        panel.SetSizer(sizer)

        self.SetMinSize(signifWindowMinSize)

#        self.sb = self.CreateStatusBar()
#        self.sb.SetStatusText(self.sbSignifText)

#        self.canvas.Bind(wx.EVT_KEY_DOWN, self.OnKeyPress)

        self.Show(True)
        self.Layout()
        self.Refresh()



    def ErrorWindow(self,messageStr,captionStr="ERROR"):
        """Generic error window"""
        dial = wx.MessageDialog(self, caption=captionStr, message=messageStr, style=wx.OK | wx.ICON_ERROR)
        result = dial.ShowModal()
        dial.Destroy()
        self.canvas.SetFocus()

    def OnEnd(self,event):
        self.WindowParent.OnSignifEnded()
        self.Destroy()

    def OnParam(self,event):
        (ATL,ATR,AP) = self.dm.GetSignifPlotParams()
        self.ActiveParam = event.GetEventObject().GetLabel()
        self.dm.SetSignifPlotParams(ATL,ATR,self.ActiveParam)
        self.Refresh()

    def OnComboLeft(self,event):
        (ATL,ATR,AP) = self.dm.GetSignifPlotParams()
        self.ActiveTagLeft = self.cbComboLeft.GetValue()
        self.cbComboRight.Clear()
        ChoicesRight = ["Outside "+self.ActiveTagLeft]+self.AllTags
        ChoicesRight.remove(self.ActiveTagLeft)
        for item in ChoicesRight:
            self.cbComboRight.Append(item)
        self.cbComboRight.SetValue("Outside "+self.ActiveTagLeft)
        self.ActiveTagRight = None
        self.dm.SetSignifPlotParams(self.ActiveTagLeft,self.ActiveTagRight,AP)
        self.Refresh()

    def OnComboRight(self,event):
        (ATL,ATR,AP) = self.dm.GetSignifPlotParams()
        self.ActiveTagRight = self.cbComboRight.GetValue()
        if self.ActiveTagRight[0:7] == "Outside":
            self.ActiveTagRight=None
        self.dm.SetSignifPlotParams(ATL,self.ActiveTagRight,AP)
        self.Refresh()
        
        
    def Refresh(self):
        valuesLeft,valuesRight = self.dm.CreatePlotSignifEmbedded(self.fig)
        numValuesLeft=len(valuesLeft)
        numValuesRight=len(valuesRight)

        cad = ""
        if self.ActiveTagRight:
            cad = cad + "%s: %s (%d points) vs. %s  (%d points)\n" % (self.ActiveParam, self.ActiveTagLeft, numValuesLeft, self.ActiveTagRight, numValuesRight)
        else: 
            cad = cad + "%s: %s  -  inside (%d points) vs. outside  (%d points)\n" % (self.ActiveParam, self.ActiveTagLeft, numValuesLeft, numValuesRight)

        if (numValuesLeft>signifNumMinValues) and (numValuesRight>signifNumMinValues):
            if self.ActiveTagRight:
                cad=cad+ u"%s: %g\u00b1%g, %s: %g\u00b1%g\n" % (self.ActiveTagLeft,np.mean(valuesLeft),np.std(valuesLeft,ddof=1),self.ActiveTagRight,np.mean(valuesRight),np.std(valuesRight,ddof=1))
            else:
                cad=cad+ u"Inside: %g\u00b1%g, Outside: %g\u00b1%g\n" % (np.mean(valuesLeft),np.std(valuesLeft,ddof=1),np.mean(valuesRight),np.std(valuesRight,ddof=1))

            # from scipy.stats import normaltest   
            # z,pval = normaltest(valuesLeft)
            # if(pval < 0.055):
            #     print self.ActiveTagLeft, "- not normal distribution"
            # else:
            #     print self.ActiveTagLeft, "- Ok!"
            # z,pval = normaltest(valuesRight)
            # if(pval < 0.055):
            #     print self.ActiveTagRight, "- not normal distribution"
            # else:
            #     print self.ActiveTagRight, "- Ok!"

            from scipy.stats import ks_2samp

            pvalue = ks_2samp(valuesLeft,valuesRight)[1]

            cad=cad+ "KS test: "
            if pvalue<signifPMaxValue:
                cad=cad+ "significative differences found!!" 
                cad=cad+"  (p-value=%g < %g)" % (pvalue, signifPMaxValue)
            else:
                cad=cad+ "significative differences not found"
                cad=cad+"  (p-value=%g >= %g)" % (pvalue, signifPMaxValue)
            
#            
        else:
            cad=cad+"Not enough data: minimum no. of points is "+str(signifNumMinValues)
#            
        self.textOutput.SetValue(cad)
        self.canvas.draw()