2 <!DOCTYPE para PUBLIC "-//OASIS//DTD DocBook XML 5.0//EN"
3 "http://www.oasis-open.org/docbook/xml/5.0/dtd/docbook.dtd" [
4 <!-- Use symbols for repeating text. -->
5 <!ENTITY % entities SYSTEM "../entities">
7 <!ENTITY % constants SYSTEM "../constants">
10 <!-- Copyright (C) 2014, 2018 The Meme Factory, Inc. http://www.meme.com
11 - See document output for full copyright information. -->
13 <!-- Core documentation on the EVENT_BOUTS view.
15 - This is indeed the query that makes the view, so don't break it!
17 - The "code" element must have a 'role="view"' attribute
18 - so that Gombe-MI code generation can get the code
19 - and actually make the view!
23 <para xmlns="http://docbook.org/ns/docbook"
26 <figure xml:id="event_bouts-code">
27 <title>Query Defining the EVENT_BOUTS View: Part I</title>
28 <literallayout><code role="view">
29 -- Note that we don't worry about leap seconds given
30 -- the time values in the db.
33 -- Compute mins_in and mins_out
34 (SELECT bouts.followid AS followid
35 , bouts.intid AS intid
36 , bouts.function AS function
37 , bouts.animid AS animid
39 , bouts.chimpid AS chimpid
41 , bouts.behavior AS behavior
42 , bouts.uncertain AS uncertain
44 , (EXTRACT(EPOCH FROM bouts.time)
45 - EXTRACT(EPOCH FROM FIRST_VALUE(bouts.time) OVER bout_w)
49 , (EXTRACT(EPOCH FROM LAST_VALUE(bouts.time)
50 OVER (bout_w RANGE BETWEEN CURRENT ROW
51 AND UNBOUNDED FOLLOWING))
52 - EXTRACT(EPOCH FROM bouts.time)
56 -- Compute bout number by summing transition ticks
57 (SELECT ticks.followid AS followid
58 , ticks.intid AS intid
59 , ticks.function AS function
60 , ticks.animid AS animid
62 , ticks.chimpid AS chimpid
63 , ticks.behavior AS behavior
64 , ticks.uncertain AS uncertain
67 OVER (PARTITION BY ticks.followid
71 , ticks.bouttick DESC)
72 AS bout</code></literallayout>
74 <figure xml:id="event_bouts-code-part2">
75 <title>Query Defining the EVENT_BOUTS View: Part II</title>
76 <literallayout><code role="view">
78 -- Distinguish one bout from another
79 (SELECT intervals.followid AS followid
80 , intervals.intid AS intid
81 , followroles.function AS function
82 , part_cids.animid AS animid
83 , followparts.role AS role
84 , followparts.chimpid AS chimpid
85 , events.behavior AS behavior
86 , BOOL_AND(events.uncertain) AS uncertain
87 , intervals.time AS time
89 WHEN LAG(MIN(intervals.time)) OVER tick_w
90 = MIN(intervals.time) - '1 minute'::INTERVAL
98 ON (events.intid = intervals.intid)
100 ON (parts.eid = events.eid)
101 JOIN chimpids AS part_cids
102 ON (part_cids.chimpid = parts.participant)
103 JOIN chimpids AS fp_cids
104 ON (fp_cids.animid = part_cids.animid)
106 ON (followparts.followid = intervals.followid
107 AND followparts.chimpid = fp_cids.chimpid)
109 ON (followroles.role = followparts.role)
110 WHERE NOT(events.baboon)
111 GROUP BY intervals.followid
115 -- The values that are unique per above anyway
116 , followroles.function
118 , followparts.chimpid
120 WINDOW tick_w AS (PARTITION BY intervals.followid
123 ORDER BY intervals.time)
126 WINDOW bout_w AS (PARTITION BY bouts.followid
131 )</code></literallayout>
133 <figure xml:id="event_bouts-code-part3">
134 <title>Query Defining the EVENT_BOUTS View: Part III</title>
135 <literallayout><code role="view">
136 -- Distribute aggregated bout info over the bout's minutes
138 , summary.prior_gap AS prior_gap
139 , summary.gap_noobs AS gap_noobs
140 , summary.left_censored AS left_censored
141 , summary.right_censored AS right_censored
142 , summary.day_start AS day_start
143 , summary.day_end AS day_end
147 (-- Aggregate per-bout information
148 SELECT bout_ends.followid AS followid
149 , bout_ends.animid AS animid
150 , bout_ends.behavior AS behavior
151 , bout_ends.bout AS bout
153 WHEN bout_ends.bout = 1 THEN
156 SUM(bout_ends.prior_gap)
160 WHEN bout_ends.bout = 1 THEN
163 SUM(bout_ends.prior_gap)
164 -- Subtract number of minutes the focal had
165 -- any behaviors during the gap.
166 - (SELECT COUNT(DISTINCT intervals.intid)
169 ON (events.intid = intervals.intid)
171 ON (parts.eid = events.eid)
175 WHERE intervals.followid = bout_ends.followid
176 AND MIN(bout_ends.time) > intervals.time
178 >= MIN(bout_ends.time)
179 - (SUM(bout_ends.prior_gap)
180 || ' minutes')::INTERVAL
181 AND chimpids.animid = bout_ends.animid)
184 , BOOL_OR(bout_ends.left_censored) AS left_censored
185 , BOOL_OR(bout_ends.right_censored) AS right_censored</code></literallayout>
187 <figure xml:id="event_bouts-code-part4">
188 <title>Query Defining the EVENT_BOUTS View: Part IV</title>
189 <literallayout><code role="view">
191 WHEN bout_ends.bout = 1 THEN
193 -- Behavioral data on the focal before the bout
197 ON (events.intid = intervals.intid)
199 ON (parts.eid = events.eid)
203 WHERE intervals.followid = bout_ends.followid
204 AND intervals.time < MIN(bout_ends.time)
205 AND chimpids.animid = bout_ends.animid
212 WHEN bout_ends.bout = LAST_VALUE(bout_ends.bout)
217 ORDER BY bout_ends.bout
218 RANGE BETWEEN CURRENT ROW
219 AND UNBOUNDED FOLLOWING)
222 -- Behavioral data on the focal after the bout
226 ON (events.intid = intervals.intid)
228 ON (parts.eid = events.eid)
232 WHERE intervals.followid = bout_ends.followid
234 > MAX(bout_ends.time)
235 AND chimpids.animid = bout_ends.animid
240 AS day_end</code></literallayout>
242 <figure xml:id="event_bouts-code-part5">
243 <title>Query Defining the EVENT_BOUTS View: Part V</title>
244 <literallayout><code role="view">
246 -- Examine the bout's endpoints to compute prior_gap, and
247 -- left and right censoring.
248 (SELECT detail.followid
252 , detail.mins_in AS mins_in
253 , detail.mins_out AS mins_out
255 WHEN detail.mins_in = 1 THEN
256 (EXTRACT(EPOCH FROM detail.time)
263 ORDER BY detail.time))
270 WHEN detail.mins_in = 1 THEN
275 ON (e.intid = i.intid)
278 JOIN chimpids AS p_cids
279 ON (p_cids.chimpid = p.participant)
280 WHERE i.followid = detail.followid
281 AND i.time = detail.time
282 - '1 minute'::INTERVAL
283 AND p_cids.animid = detail.animid
288 AS left_censored</code></literallayout>
290 <figure xml:id="event_bouts-code-part6">
291 <title>Query Defining the EVENT_BOUTS View: Part VI</title>
292 <literallayout><code role="view">
294 WHEN detail.mins_out = 0 THEN
299 ON (e.intid = i.intid)
302 JOIN chimpids AS p_cids
303 ON (p_cids.chimpid = p.participant)
304 WHERE i.followid = detail.followid
305 AND i.time = detail.time
306 + '1 minute'::INTERVAL
307 AND p_cids.animid = detail.animid
313 , detail.time AS time
315 WHERE detail.mins_in = 1
316 OR detail.mins_out = 0
318 GROUP BY bout_ends.followid
323 ON (summary.followid = detail.followid
324 AND summary.animid = detail.animid
325 AND summary.behavior = detail.behavior
326 AND summary.bout = detail.bout)</code><code>;</code></literallayout>
329 <!-- Figure containing EVENT_BOUTS view diagram. -->
331 <!-- Use mediaobject role attribute to select either xhtml (html)
332 or pdf (dblatex) file paths. -->
334 <figure xml:id="event_bouts-erd">
335 <title>Entity Relationship Diagram of the EVENT_BOUTS View</title>
337 <textobject> <phrase>ER diagram intentionally omitted.</phrase>