[med-svn] [aghermann] 52/60: lua WIP

andrei zavada hmmr-guest at alioth.debian.org
Mon Nov 4 23:50:00 UTC 2013


This is an automated email from the git hooks/post-receive script.

hmmr-guest pushed a commit to branch WIP
in repository aghermann.

commit 3417f14234f27e7772979fc3b1d1209f3f569f20
Author: Andrei Zavada <johnhommer at gmail.com>
Date:   Fri Oct 25 02:02:59 2013 +0300

    lua WIP
---
 upstream/configure.ac                    |    6 ++
 upstream/data/sf-rk1968.glade            |   37 +++++-------
 upstream/src/aghermann/Makefile.am       |    4 +-
 upstream/src/aghermann/globals.cc        |   32 +++++++++--
 upstream/src/aghermann/globals.hh        |    6 ++
 upstream/src/aghermann/rk1968/rk1968.cc  |   91 +++++++++++++++++-------------
 upstream/src/aghermann/rk1968/rk1968.hh  |   49 ++++++----------
 upstream/src/aghermann/ui/sf/d/rk1968.cc |   11 ++--
 upstream/src/aghermann/ui/sf/d/rk1968.hh |    6 +-
 upstream/src/aghermann/ui/ui++.hh        |   17 ++++++
 10 files changed, 153 insertions(+), 106 deletions(-)

diff --git a/upstream/configure.ac b/upstream/configure.ac
index 8ee68fc..c919898 100644
--- a/upstream/configure.ac
+++ b/upstream/configure.ac
@@ -81,7 +81,13 @@ AC_CHECK_LIB(
 	[-lpthread -lfftw3_omp -lfftw3])
 AC_SUBST(LIBFFTW3_LDADD, $LIBFFTW3_LDADD)
 
+dnl lua!
+AX_PROG_LUA([5.1])
+AX_LUA_LIBS
+AX_LUA_HEADERS
 
+
+dnl this is needed to compile all our glade files into a nice gresource
 AC_PATH_PROGS([glib_compile_resources], [glib-compile-resources], [:])
 test x$glib_compile_resources = x":" && \
      AC_MSG_ERROR([Huh. Isn't glib-compile-resources not part of glib?])
diff --git a/upstream/data/sf-rk1968.glade b/upstream/data/sf-rk1968.glade
index 75413aa..70be5d1 100644
--- a/upstream/data/sf-rk1968.glade
+++ b/upstream/data/sf-rk1968.glade
@@ -8,6 +8,9 @@
     <property name="step_increment">0.01</property>
     <property name="page_increment">1</property>
   </object>
+  <object class="GtkTextBuffer" id="tSFRKScript">
+    <property name="text" translatable="yes">fafa</property>
+  </object>
   <object class="GtkDialog" id="wSFRK">
     <property name="can_focus">False</property>
     <property name="border_width">10</property>
@@ -196,7 +199,6 @@
                 <property name="visible">True</property>
                 <property name="can_focus">False</property>
                 <property name="n_rows">2</property>
-                <property name="n_columns">2</property>
                 <property name="column_spacing">5</property>
                 <child>
                   <placeholder/>
@@ -211,36 +213,23 @@
                   <placeholder/>
                 </child>
                 <child>
-                  <placeholder/>
-                </child>
-                <child>
-                  <placeholder/>
-                </child>
-                <child>
-                  <placeholder/>
-                </child>
-                <child>
-                  <object class="GtkSpinButton" id="eSFRKNremThetaDeltaRatio">
+                  <object class="GtkLabel" id="label1">
                     <property name="visible">True</property>
-                    <property name="can_focus">True</property>
-                    <property name="invisible_char">•</property>
+                    <property name="can_focus">False</property>
                     <property name="xalign">1</property>
-                    <property name="adjustment">jSFRKNremThetaDeltaRatio</property>
-                    <property name="digits">2</property>
+                    <property name="label" translatable="yes">Script:</property>
                   </object>
-                  <packing>
-                    <property name="left_attach">1</property>
-                    <property name="right_attach">2</property>
-                    <property name="x_options">GTK_FILL</property>
-                  </packing>
                 </child>
                 <child>
-                  <object class="GtkLabel" id="label1">
+                  <object class="GtkTextView" id="eSFRKScript">
                     <property name="visible">True</property>
-                    <property name="can_focus">False</property>
-                    <property name="xalign">1</property>
-                    <property name="label" translatable="yes">NREM Delta/Theta power ratio:</property>
+                    <property name="can_focus">True</property>
+                    <property name="buffer">tSFRKScript</property>
                   </object>
+                  <packing>
+                    <property name="top_attach">1</property>
+                    <property name="bottom_attach">2</property>
+                  </packing>
                 </child>
               </object>
               <packing>
diff --git a/upstream/src/aghermann/Makefile.am b/upstream/src/aghermann/Makefile.am
index 3215a04..dbfce26 100644
--- a/upstream/src/aghermann/Makefile.am
+++ b/upstream/src/aghermann/Makefile.am
@@ -2,6 +2,7 @@ AM_CXXFLAGS := \
 	-Wall -Wdeprecated-declarations -std=c++0x -fno-rtti \
 	$(GTK_CFLAGS) $(VTE_CFLAGS) $(CAIRO_CFLAGS) $(LIBCONFIGXX_CFLAGS) $(UNIQUE_CFLAGS) \
 	$(OPENMP_CXXFLAGS) \
+	$(LUA_INCLUDE) \
 	-I$(top_srcdir)/src \
 	-DPACKAGE_DATADIR=\"$(datadir)\" \
 	-DBUILT_BY=\"@user@\"
@@ -138,7 +139,8 @@ aghermann_LDADD := \
 	$(FFTW3_LIBS) $(ITPP_LIBS) $(SAMPLERATE_LIBS) $(GSL_LIBS) \
 	$(GTK_LIBS) $(CAIRO_LIBS) $(UNIQUE_LIBS) $(VTE_LIBS) \
 	$(LIBCONFIGXX_LIBS) \
-	$(LIBFFTW3_LDADD) $(OPENMP_CXXFLAGS)
+	$(LIBFFTW3_LDADD) $(OPENMP_CXXFLAGS) \
+	$(LUA_LIB)
 
 aghermann_LDFLAGS := \
 	-shared
diff --git a/upstream/src/aghermann/globals.cc b/upstream/src/aghermann/globals.cc
index 5a2013c..c727cb9 100644
--- a/upstream/src/aghermann/globals.cc
+++ b/upstream/src/aghermann/globals.cc
@@ -9,6 +9,7 @@
  *         License:  GPL
  */
 
+#include "globals.hh"
 
 #include <cstdarg>
 #include <cassert>
@@ -19,8 +20,13 @@
 #include <omp.h>
 #endif
 
-#include "globals.hh"
+extern "C" {
+#include <lua.h>
+#include <lualib.h>
+#include <lauxlib.h>
+}
 
+#include "common/string.hh"
 
 #if HAVE_CONFIG_H && !defined(VERSION)
 #  include "config.h"
@@ -34,11 +40,19 @@ gsl_rng *agh::global::rng = nullptr;
 
 int agh::global::num_procs = 1;
 
+lua_State* agh::global::lua_state = nullptr;
+
 
 void
 agh::global::
 init( const string& lf_fname)
 {
+      // 0. log facility
+        {
+                default_log_facility =
+                        lf_fname.empty() ? nullptr : new agh::log::CLogFacility (lf_fname);
+        }
+
       // 1. init rng
         {
                 const gsl_rng_type *T;
@@ -59,10 +73,16 @@ init( const string& lf_fname)
         }
 #endif
 
-      // 3. log facility
+      // 3. lua
         {
-                default_log_facility =
-                        lf_fname.empty() ? nullptr : new agh::log::CLogFacility (lf_fname);
+                lua_state = lua_open();
+
+                // lua_baselibopen ( lua_state);
+                // luaopen_table   ( lua_state);
+                // luaopen_io      ( lua_state);
+                // luaopen_string  ( lua_state);
+                // luaopen_math    ( lua_state);
+                luaL_openlibs( lua_state);
         }
 }
 
@@ -73,6 +93,10 @@ fini()
 {
         gsl_rng_free( rng);
         rng = nullptr;
+
+        lua_close( lua_state);
+        lua_state = nullptr;
+
         if ( default_log_facility )
                 delete default_log_facility;
         default_log_facility = nullptr;
diff --git a/upstream/src/aghermann/globals.hh b/upstream/src/aghermann/globals.hh
index e3ec71f..22bcbf8 100644
--- a/upstream/src/aghermann/globals.hh
+++ b/upstream/src/aghermann/globals.hh
@@ -13,6 +13,9 @@
 #define AGH_AGHERMANN_GLOBALS_H_
 
 #include <gsl/gsl_rng.h>
+extern "C" {
+#include <lua.h>
+}
 #include "common/log-facility.hh"
 
 #if HAVE_CONFIG_H && !defined(VERSION)
@@ -36,6 +39,9 @@ void log( agh::log::TLevel, const char* issuer, const char* fmt, ...) __attribut
 #define APPLOG_WARN(...)  do agh::global::log( agh::log::TLevel::warning, LOG_SOURCE_ISSUER, __VA_ARGS__); while (0)
 #define APPLOG_ERROR(...) do agh::global::log( agh::log::TLevel::error,   LOG_SOURCE_ISSUER, __VA_ARGS__); while (0)
 
+
+extern lua_State* lua_state;
+
 void init( const string& lf_fname);
 void fini();
 
diff --git a/upstream/src/aghermann/rk1968/rk1968.cc b/upstream/src/aghermann/rk1968/rk1968.cc
index abc7542..4afd8e1 100644
--- a/upstream/src/aghermann/rk1968/rk1968.cc
+++ b/upstream/src/aghermann/rk1968/rk1968.cc
@@ -10,45 +10,75 @@
  */
 
 
+#include "rk1968.hh"
+
 #include <forward_list>
+#include <sstream>
+#include <fstream>
+
+extern "C" {
+#include <lua.h>
+#include <lauxlib.h>
+}
 
+#include "libmetrics/bands.hh"
+#include "aghermann/globals.hh"
 #include "libsigfile/page.hh"
 #include "libsigfile/typed-source.hh"
 #include "aghermann/expdesign/recording.hh"
 #include "aghermann/expdesign/subject.hh"
 #include "aghermann/expdesign/expdesign.hh"
-#include "libmetrics/bands.hh"
-
-#include "rk1968.hh"
 
 
 using namespace std;
 using namespace agh::rk1968;
 
+using agh::global::lua_state;
 
 
-
+int
 CScoreAssistant::
-CScoreAssistant (const string& name_,
-                 TExpDirLevel level_, CExpDesign& ED_, const SExpDirLevelId& level_id_)
-      : CStorablePPack (common_subdir, name_, level_, ED_, level_id_)
+load()
 {
-        config ("nrem3_delta_theta_ratio", &Pp.nrem3_delta_theta_ratio);
-
-        load();
+        string full_path = path();
+        ostringstream acc;
+        ifstream oleg (full_path);
+        while ( oleg.good() )
+                acc << oleg;
+        script_contents = acc.str();
+
+        // there is luaL_loadfile, but we would like to keep the contents, too
+        int ret1 = luaL_loadbuffer(
+                lua_state,
+                script_contents.c_str(),
+                script_contents.size(),
+                full_path.c_str());
+        return ret1;
 }
 
-
+int
+CScoreAssistant::
+save()
+{
+        
+        return 0;
+}
 
 int
 CScoreAssistant::
 score( agh::SEpisode& E, size_t* n_pages_scored_p)
 {
+        // make various signal metrics available for lua
+        // and execute a script
+
         size_t n_pages_scored = 0;
 
         sigfile::CHypnogram& F1 = E.sources.front();
 
-      // 1. delta/theta-based NREM3 detection
+      // 1. prepare input vectors
+        forward_list<valarray<TFloat>>
+                courses_delta,
+                courses_theta;
         {
                 forward_list<agh::CRecording*> HH;
                 for ( auto &R : E.recordings )
@@ -57,9 +87,6 @@ score( agh::SEpisode& E, size_t* n_pages_scored_p)
                 if ( HH.empty() )
                         return -1;
 
-                forward_list<valarray<TFloat>>
-                        courses_delta,
-                        courses_theta;
                 for ( auto &H : HH ) {
                         courses_delta.emplace_front(
                                 H->psd_profile.course(
@@ -70,32 +97,18 @@ score( agh::SEpisode& E, size_t* n_pages_scored_p)
                                         agh::CExpDesign::freq_bands[metrics::TBand::theta][0],
                                         agh::CExpDesign::freq_bands[metrics::TBand::theta][1]));
                 }
-
-                for ( size_t p = 0; p < F1.n_pages(); ++p ) {
-                        auto    Di = courses_delta.begin(),
-                                Ti = courses_theta.begin();
-                        int decision = 0;
-                        for ( ; Di != courses_delta.end(); ++Di, ++Ti ) {
-                                decision +=
-                                        ( (*Di)[p] > (*Ti)[p] * Pp.nrem3_delta_theta_ratio );
-                        }
-                        if ( decision > 0 ) {
-                                F1[p].mark( sigfile::SPage::TScore::nrem3);
-                                ++n_pages_scored;
-                        }
-                }
-        }
-
-      // 2. detect REM by EMG amplitude histogram peak shift
-        {
-                // (a) get a whole-episode EMG amplitude distribution
-
-                // (b) 
         }
 
-      // 3. detect Wake where EMG is too wild
-        {
-                
+        // 2. 
+        for ( size_t p = 0; p < F1.n_pages(); ++p ) {
+                // luaL_put...
+                lua_pcall(
+                        agh::global::lua_state,
+                        2, // nargsin
+                        1, // nargsout
+                        0);
+                int score; // = luaL_get...
+                F1[p].mark( (sigfile::SPage::TScore) score);
         }
 
         if ( n_pages_scored_p )
diff --git a/upstream/src/aghermann/rk1968/rk1968.hh b/upstream/src/aghermann/rk1968/rk1968.hh
index e1c234c..280f7f6 100644
--- a/upstream/src/aghermann/rk1968/rk1968.hh
+++ b/upstream/src/aghermann/rk1968/rk1968.hh
@@ -14,6 +14,7 @@
 
 #include <float.h>
 #include <string>
+
 #include "aghermann/expdesign/dirlevel.hh"
 #include "libsigproc/sigproc.hh"
 
@@ -22,27 +23,6 @@ using namespace std;
 namespace agh {
 namespace rk1968 {
 
-struct SScoreAssistantPPack {
-        double  nrem3_delta_theta_ratio;
-
-        SScoreAssistantPPack ()
-              : nrem3_delta_theta_ratio (1.5)
-                {}
-        explicit SScoreAssistantPPack (const SScoreAssistantPPack& rv)
-              : nrem3_delta_theta_ratio (rv.nrem3_delta_theta_ratio)
-                {}
-        SScoreAssistantPPack& operator=( const SScoreAssistantPPack& rv)
-                {
-                        nrem3_delta_theta_ratio = rv.nrem3_delta_theta_ratio;
-                        return *this;
-                }
-
-        bool operator==( const SScoreAssistantPPack& rv)
-                {
-                        return nrem3_delta_theta_ratio == rv.nrem3_delta_theta_ratio;
-                }
-};
-
 class CScoreAssistant
   : public CStorablePPack {
 
@@ -50,13 +30,15 @@ class CScoreAssistant
         static constexpr const char* common_subdir = ".rk1968/";
 
         CScoreAssistant (const string& name_,
-                         TExpDirLevel, CExpDesign&, const SExpDirLevelId&);
-        explicit CScoreAssistant (const CScoreAssistant& rv)
-              : CStorablePPack (common_subdir, rv.name + " (dup)", TExpDirLevel::transient, rv.ED, rv.level_id),
-                Pp (rv.Pp)
+                         TExpDirLevel level_, CExpDesign& ED_, const SExpDirLevelId& level_id_)
+              : CStorablePPack (common_subdir, name_, level_, ED_, level_id_)
                 {
-                        // assign_keys();
+                        load();
                 }
+        explicit CScoreAssistant (const CScoreAssistant& rv)
+              : CStorablePPack (common_subdir, rv.name + " (dup)", TExpDirLevel::transient, rv.ED, rv.level_id),
+                script_contents (rv.script_contents)
+                {}
         explicit CScoreAssistant (CExpDesign& ED_, const SExpDirLevelId& level_id_)
               : CStorablePPack (common_subdir, "(unnamed)", TExpDirLevel::transient, ED_, level_id_)
                 {}
@@ -69,26 +51,29 @@ class CScoreAssistant
         CScoreAssistant&
         operator=( CScoreAssistant&& rv)
                 {
-                        Pp = move(rv.Pp);
+                        script_contents = move(rv.script_contents);
                         return *this;
                 }
         CScoreAssistant&
         operator=( const CScoreAssistant& rv)
                 {
-                        Pp = rv.Pp;
+                        script_contents = rv.script_contents;
                         return *this;
                 }
 
         bool
         operator==( const CScoreAssistant& rv)
                 {
-                        return Pp == rv.Pp;
+                        return script_contents == rv.script_contents;
                 }
 
-        int score( agh::SEpisode&, size_t* pages_scored_successfuly);
+        // this one is not libconfig-based, so override these
+        int load();
+        int save();
 
-        SScoreAssistantPPack
-                Pp;
+        string script_contents;
+
+        int score( agh::SEpisode&, size_t* pages_scored_successfuly);
 };
 
 
diff --git a/upstream/src/aghermann/ui/sf/d/rk1968.cc b/upstream/src/aghermann/ui/sf/d/rk1968.cc
index 30846cf..f16cac3 100644
--- a/upstream/src/aghermann/ui/sf/d/rk1968.cc
+++ b/upstream/src/aghermann/ui/sf/d/rk1968.cc
@@ -47,7 +47,10 @@ SRK1968Dialog (SScoringFacility& p_)
         AGH_GBGETOBJ (bSFRKProfileSave);
         AGH_GBGETOBJ (bSFRKProfileDiscard);
         AGH_GBGETOBJ (eSFRKProfileList);
-        AGH_GBGETOBJ (eSFRKNremThetaDeltaRatio);
+
+        AGH_GBGETOBJ (eSFRKScript);
+        AGH_GBGETOBJ (tSFRKScript);
+
         AGH_GBGETOBJ (lSFRKTopInfo);
         AGH_GBGETOBJ (lSFRKBottomInfo);
         AGH_GBGETOBJ (bSFRKPreview);
@@ -75,9 +78,9 @@ SRK1968Dialog (SScoringFacility& p_)
         G_CONNECT_1 (bSFRKProfileRevert,  clicked);
         G_CONNECT_1 (bSFRKProfileDiscard, clicked);
 
-        for ( auto& W : {eSFRKNremThetaDeltaRatio} )
+        for ( auto& W : {tSFRKScript} )
                 g_signal_connect(
-                        W, "value-changed",
+                        W, "changed",
                         (GCallback)eSFRK_any_profile_value_changed_cb,
                         this);
 
@@ -94,7 +97,7 @@ SRK1968Dialog (SScoringFacility& p_)
                         this);
 
       // 2. dialog
-        W_V.reg( eSFRKNremThetaDeltaRatio, &Pp2.Pp.nrem3_delta_theta_ratio);
+        W_V.reg( tSFRKScript, &Pp2.script_contents);
 }
 
 SScoringFacility::SRK1968Dialog::
diff --git a/upstream/src/aghermann/ui/sf/d/rk1968.hh b/upstream/src/aghermann/ui/sf/d/rk1968.hh
index cf64dc6..06a23bc 100644
--- a/upstream/src/aghermann/ui/sf/d/rk1968.hh
+++ b/upstream/src/aghermann/ui/sf/d/rk1968.hh
@@ -62,8 +62,10 @@ struct SScoringFacility::SRK1968Dialog
                 *eSFRKProfileList;
         gulong  eSFRKProfileList_changed_cb_handler_id;
 
-        GtkSpinButton
-                *eSFRKNremThetaDeltaRatio;
+        GtkTextView
+                *eSFRKScript;
+        GtkTextBuffer
+                *tSFRKScript;
 
         GtkToggleButton
                 *bSFRKPreview;
diff --git a/upstream/src/aghermann/ui/ui++.hh b/upstream/src/aghermann/ui/ui++.hh
index 67c236a..e6f258f 100644
--- a/upstream/src/aghermann/ui/ui++.hh
+++ b/upstream/src/aghermann/ui/ui++.hh
@@ -242,6 +242,20 @@ SUIVar_<GtkEntry, string>::up()          const { gtk_entry_set_text( w, v->c_str
 template <> inline void
 SUIVar_<GtkEntry, string>::down()        const { v->assign( gtk_entry_get_text( w)); }
 
+template <> inline void
+SUIVar_<GtkTextBuffer, string>::up()     const
+{
+        gtk_text_buffer_set_text( w, v->c_str(), -1);
+}
+template <> inline void
+SUIVar_<GtkTextBuffer, string>::down()        const
+{
+        GtkTextIter a, z;
+        gtk_text_buffer_get_start_iter( w, &a);
+        gtk_text_buffer_get_end_iter( w, &z);
+        v->assign( gtk_text_buffer_get_text( w, &a, &z, FALSE /* include_hidden_chars */ ));
+}
+
 
 
 class SUIVarCollection {
@@ -276,6 +290,9 @@ class SUIVarCollection {
         void reg( GtkEntry *w, string* v)
                 { c.push_back( new SUIVar_<GtkEntry, string> (w, v)); }
 
+        void reg( GtkTextBuffer *w, string* v)
+                { c.push_back( new SUIVar_<GtkTextBuffer, string> (w, v)); }
+
         // odd one out
         void reg( GtkListStore *m, list<string> *l)
                 { c.push_back( new SUIVar_<GtkListStore, list<string>> (m, l)); }

-- 
Alioth's /git/debian-med/git-commit-notice on /srv/git.debian.org/git/debian-med/aghermann.git



More information about the debian-med-commit mailing list