File: basic_score.lua

package info (click to toggle)
aghermann 1.1.2-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid
  • size: 3,156 kB
  • sloc: cpp: 27,967; makefile: 495; xml: 20; sh: 18; ansic: 9
file content (135 lines) | stat: -rw-r--r-- 4,066 bytes parent folder | download | duplicates (4)
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
-- ; -*- mode: Lua -*-
-- First, you collect necessary pieces
local A, Fi, Fo = ...

-- (1) A is an opaque structure representing our Scoring Assistant host side;
--     all you need to do with it is pass it as the first arg to all calls
--     of Fi and Fo (see below).

-- (2) Fi, a CFunction, is the proxy to get any data related to or describing
--     the montage being scored. It has the signature (think syscall):
--        Fi( A, opcode, arg1, arg2, ...)
--     These are the supported opcodes:
--      1. Informational.
local op_get_channel_list         = 1
local op_get_channel_list_of_type = 2
local op_get_channel_data         = 3
--      2. Metrics describing a page:
local op_get_psd                  = 100
local op_get_mc                   = 101
local op_get_swu                  = 102
local op_get_signal_envelope      = 103
local op_get_page_dirty           = 104

-- For convenience, here are wrappers

-- common notes:
--  (a) on error, Fi returns {false, error_string}; else, results as described below;
--  (b) all page parameters are 1-baaed.

function get_channel_list ()
   -- returns: n_channels, ch1, ch2, ...
   return Fi(A, op_get_channel_list)
end

function get_channel_list_of_type (t)
   -- args: t: signal type, e.g., "EEG", "EOG", etc.
   -- returns: n_channels, ch1, ch2, ...
   return Fi(A, op_get_channel_list_of_type, t)
end

function get_channel_data (h)
   -- args: h: channel, e.g., "Fz"
   -- returns: n_full_pages, n_total_pages, pagesize, samplerate
   return Fi(A, op_get_channel_data, h)
end

function get_psd (h, pa, pz, fa, fz)
   -- args: h: channel, e.g., "Fz",
   --       pa, pz: starting and ending pages, inclusive;
   --       fa, fz: frequncy range to extract PSD in.
   -- returns: a vector of numbers.
   return Fi(A, op_get_psd, h, pa-1, pz-1, fa, fz)
end

function get_mc (h, pa, pz, fa, _)
   -- args: h, channel, e.g., "Fz",
   --       pa, pz: start and end pages, inclusive;
   --       fa: f0 range to extract MC at.
   -- returns: a vector of numbers.
   return Fi(A, op_get_psd, h, pa-1, pz-1, fa, _)
end

function get_swu (h, pa, pz, fa, _)
   -- args: h: channel, e.g., "Fz",
   --       pa, pz: starting and ending pages, inclusive;
   --       fa: frequncy to extract SWU at.
   -- returns: a vector of numbers.
   return Fi(A, op_get_psd, h, pa-1, pz-1, fa, _)
end

function get_signal_envelope (h, p, scope, dt)
   -- args: h: channel, e.g., "Fz",
   --       p: page,
   --       scope: envelope scope (tightness) parameter, sec (1 should be OK),
   --       dt: time resolution of resulting vector
   -- returns: a vector of signal envelope breadth values, taken at dt intervals.
   return Fi(A, op_get_signal_envelope, h, p-1, scope, dt)
end

function get_page_dirty (h, p)
   -- args: h: channel, e.g., "Fz",
   --       p: page,
   -- returns: a portion [0..1] of signal on page p in channel h marked as artifact
   return Fi(A, op_get_page_dirty, h, p-1)
end


-- (3) Fo, also a CFunction, is used to deliver the scoring decision to the host

local nrem1 = 1
local nrem2 = 2
local nrem3 = 3
local nrem4 = 4
local rem   = 5
local wake  = 6
function put_score (p, s)
   -- args: p: page, 1-based
   --       s: score code as above
   -- returns: nothing
   return Fo(A, p-1, s)
end


--
-- function main
--
local cc = {get_channel_list_of_type ("EEG")}
print( string.format( "Montage has %d EEG channels", cc[1]))

local H = cc[2]
local nf, np, ps, sr = get_channel_data (H)
print( string.format( "Looking at channel %q: %d full pages (%d total) @%d sec, samplerate %d", H, nf, np, ps, sr))

local delta = {get_psd (H, 1, nf, 2, 3)}
local theta = {get_psd (H, 1, nf, 4, 8)}

local n_scored = 0
for p=1,nf-1 do
  -- dirty = get_page_dirty(H, p)
  -- if dirty > 0 then
  --   print( string.format("p %d dirty (%g)", p, dirty))
  -- end
  -- print( get_signal_envelope(H, p, 1, 1))

  if delta[p] > theta[p] * 1.5 then
     n_scored = n_scored+1
     -- print( "mark page ", p)
     put_score (p, nrem3)
  end
end

print( string.format("scored %d pages", n_scored))
print("")

return n_scored