Open ggilestro opened 3 years ago
The script above will not work if the t
value is largest at the end of the experiment, for instance in cases like the one below.
What we need to identify is the first peak, which is the only point where id.t < id.t+1 with the following script:
SELECT t FROM (
SELECT
g1.id,
g1.t,
(g2.t - g1.t) as delta
FROM
ROI_1 g1
INNER JOIN
ROI_1 g2 on g2.id = g1.id + 1
WHERE
delta < 0
)
Which in one row becomes
UPDATE ROI_1 SET t = t + (SELECT FIT from (SELECT g1.id as FID, g1.t as FIT, (g2.t - g1.t) as delta FROM ROI_1 g1 INNER JOIN ROI_1 g2 on g2.id = g1.id + 1 WHERE delta < 0)) WHERE id > (SELECT FID FROM (SELECT g1.id as FID, g1.t as FIT, (g2.t - g1.t) as delta FROM ROI_1 g1 INNER JOIN ROI_1 g2 on g2.id = g1.id + 1 WHERE delta < 0));
And this solves the issue
So the full sequence to use is the following:
UPDATE ROI_1 SET t = t + (SELECT FIT from (SELECT g1.id as FID, g1.t as FIT, (g2.t - g1.t) as delta FROM ROI_1 g1 INNER JOIN ROI_1 g2 on g2.id = g1.id + 1 WHERE delta < 0)) WHERE id > (SELECT FID FROM (SELECT g1.id as FID, g1.t as FIT, (g2.t - g1.t) as delta FROM ROI_1 g1 INNER JOIN ROI_1 g2 on g2.id = g1.id + 1 WHERE delta < 0));
UPDATE ROI_2 SET t = t + (SELECT FIT from (SELECT g1.id as FID, g1.t as FIT, (g2.t - g1.t) as delta FROM ROI_2 g1 INNER JOIN ROI_2 g2 on g2.id = g1.id + 1 WHERE delta < 0)) WHERE id > (SELECT FID FROM (SELECT g1.id as FID, g1.t as FIT, (g2.t - g1.t) as delta FROM ROI_2 g1 INNER JOIN ROI_2 g2 on g2.id = g1.id + 1 WHERE delta < 0));
UPDATE ROI_3 SET t = t + (SELECT FIT from (SELECT g1.id as FID, g1.t as FIT, (g2.t - g1.t) as delta FROM ROI_3 g1 INNER JOIN ROI_3 g2 on g2.id = g1.id + 1 WHERE delta < 0)) WHERE id > (SELECT FID FROM (SELECT g1.id as FID, g1.t as FIT, (g2.t - g1.t) as delta FROM ROI_3 g1 INNER JOIN ROI_3 g2 on g2.id = g1.id + 1 WHERE delta < 0));
UPDATE ROI_4 SET t = t + (SELECT FIT from (SELECT g1.id as FID, g1.t as FIT, (g2.t - g1.t) as delta FROM ROI_4 g1 INNER JOIN ROI_4 g2 on g2.id = g1.id + 1 WHERE delta < 0)) WHERE id > (SELECT FID FROM (SELECT g1.id as FID, g1.t as FIT, (g2.t - g1.t) as delta FROM ROI_4 g1 INNER JOIN ROI_4 g2 on g2.id = g1.id + 1 WHERE delta < 0));
UPDATE ROI_5 SET t = t + (SELECT FIT from (SELECT g1.id as FID, g1.t as FIT, (g2.t - g1.t) as delta FROM ROI_5 g1 INNER JOIN ROI_5 g2 on g2.id = g1.id + 1 WHERE delta < 0)) WHERE id > (SELECT FID FROM (SELECT g1.id as FID, g1.t as FIT, (g2.t - g1.t) as delta FROM ROI_5 g1 INNER JOIN ROI_5 g2 on g2.id = g1.id + 1 WHERE delta < 0));
UPDATE ROI_6 SET t = t + (SELECT FIT from (SELECT g1.id as FID, g1.t as FIT, (g2.t - g1.t) as delta FROM ROI_6 g1 INNER JOIN ROI_6 g2 on g2.id = g1.id + 1 WHERE delta < 0)) WHERE id > (SELECT FID FROM (SELECT g1.id as FID, g1.t as FIT, (g2.t - g1.t) as delta FROM ROI_6 g1 INNER JOIN ROI_6 g2 on g2.id = g1.id + 1 WHERE delta < 0));
UPDATE ROI_7 SET t = t + (SELECT FIT from (SELECT g1.id as FID, g1.t as FIT, (g2.t - g1.t) as delta FROM ROI_7 g1 INNER JOIN ROI_7 g2 on g2.id = g1.id + 1 WHERE delta < 0)) WHERE id > (SELECT FID FROM (SELECT g1.id as FID, g1.t as FIT, (g2.t - g1.t) as delta FROM ROI_7 g1 INNER JOIN ROI_7 g2 on g2.id = g1.id + 1 WHERE delta < 0));
UPDATE ROI_8 SET t = t + (SELECT FIT from (SELECT g1.id as FID, g1.t as FIT, (g2.t - g1.t) as delta FROM ROI_8 g1 INNER JOIN ROI_8 g2 on g2.id = g1.id + 1 WHERE delta < 0)) WHERE id > (SELECT FID FROM (SELECT g1.id as FID, g1.t as FIT, (g2.t - g1.t) as delta FROM ROI_8 g1 INNER JOIN ROI_8 g2 on g2.id = g1.id + 1 WHERE delta < 0));
UPDATE ROI_9 SET t = t + (SELECT FIT from (SELECT g1.id as FID, g1.t as FIT, (g2.t - g1.t) as delta FROM ROI_9 g1 INNER JOIN ROI_9 g2 on g2.id = g1.id + 1 WHERE delta < 0)) WHERE id > (SELECT FID FROM (SELECT g1.id as FID, g1.t as FIT, (g2.t - g1.t) as delta FROM ROI_9 g1 INNER JOIN ROI_9 g2 on g2.id = g1.id + 1 WHERE delta < 0));
UPDATE ROI_10 SET t = t + (SELECT FIT from (SELECT g1.id as FID, g1.t as FIT, (g2.t - g1.t) as delta FROM ROI_10 g1 INNER JOIN ROI_10 g2 on g2.id = g1.id + 1 WHERE delta < 0)) WHERE id > (SELECT FID FROM (SELECT g1.id as FID, g1.t as FIT, (g2.t - g1.t) as delta FROM ROI_10 g1 INNER JOIN ROI_10 g2 on g2.id = g1.id + 1 WHERE delta < 0));
UPDATE ROI_11 SET t = t + (SELECT FIT from (SELECT g1.id as FID, g1.t as FIT, (g2.t - g1.t) as delta FROM ROI_11 g1 INNER JOIN ROI_11 g2 on g2.id = g1.id + 1 WHERE delta < 0)) WHERE id > (SELECT FID FROM (SELECT g1.id as FID, g1.t as FIT, (g2.t - g1.t) as delta FROM ROI_11 g1 INNER JOIN ROI_11 g2 on g2.id = g1.id + 1 WHERE delta < 0));
UPDATE ROI_12 SET t = t + (SELECT FIT from (SELECT g1.id as FID, g1.t as FIT, (g2.t - g1.t) as delta FROM ROI_12 g1 INNER JOIN ROI_12 g2 on g2.id = g1.id + 1 WHERE delta < 0)) WHERE id > (SELECT FID FROM (SELECT g1.id as FID, g1.t as FIT, (g2.t - g1.t) as delta FROM ROI_12 g1 INNER JOIN ROI_12 g2 on g2.id = g1.id + 1 WHERE delta < 0));
UPDATE ROI_13 SET t = t + (SELECT FIT from (SELECT g1.id as FID, g1.t as FIT, (g2.t - g1.t) as delta FROM ROI_13 g1 INNER JOIN ROI_13 g2 on g2.id = g1.id + 1 WHERE delta < 0)) WHERE id > (SELECT FID FROM (SELECT g1.id as FID, g1.t as FIT, (g2.t - g1.t) as delta FROM ROI_13 g1 INNER JOIN ROI_13 g2 on g2.id = g1.id + 1 WHERE delta < 0));
UPDATE ROI_14 SET t = t + (SELECT FIT from (SELECT g1.id as FID, g1.t as FIT, (g2.t - g1.t) as delta FROM ROI_14 g1 INNER JOIN ROI_14 g2 on g2.id = g1.id + 1 WHERE delta < 0)) WHERE id > (SELECT FID FROM (SELECT g1.id as FID, g1.t as FIT, (g2.t - g1.t) as delta FROM ROI_14 g1 INNER JOIN ROI_14 g2 on g2.id = g1.id + 1 WHERE delta < 0));
UPDATE ROI_15 SET t = t + (SELECT FIT from (SELECT g1.id as FID, g1.t as FIT, (g2.t - g1.t) as delta FROM ROI_15 g1 INNER JOIN ROI_15 g2 on g2.id = g1.id + 1 WHERE delta < 0)) WHERE id > (SELECT FID FROM (SELECT g1.id as FID, g1.t as FIT, (g2.t - g1.t) as delta FROM ROI_15 g1 INNER JOIN ROI_15 g2 on g2.id = g1.id + 1 WHERE delta < 0));
UPDATE ROI_16 SET t = t + (SELECT FIT from (SELECT g1.id as FID, g1.t as FIT, (g2.t - g1.t) as delta FROM ROI_16 g1 INNER JOIN ROI_16 g2 on g2.id = g1.id + 1 WHERE delta < 0)) WHERE id > (SELECT FID FROM (SELECT g1.id as FID, g1.t as FIT, (g2.t - g1.t) as delta FROM ROI_16 g1 INNER JOIN ROI_16 g2 on g2.id = g1.id + 1 WHERE delta < 0));
UPDATE ROI_17 SET t = t + (SELECT FIT from (SELECT g1.id as FID, g1.t as FIT, (g2.t - g1.t) as delta FROM ROI_17 g1 INNER JOIN ROI_17 g2 on g2.id = g1.id + 1 WHERE delta < 0)) WHERE id > (SELECT FID FROM (SELECT g1.id as FID, g1.t as FIT, (g2.t - g1.t) as delta FROM ROI_17 g1 INNER JOIN ROI_17 g2 on g2.id = g1.id + 1 WHERE delta < 0));
UPDATE ROI_18 SET t = t + (SELECT FIT from (SELECT g1.id as FID, g1.t as FIT, (g2.t - g1.t) as delta FROM ROI_18 g1 INNER JOIN ROI_18 g2 on g2.id = g1.id + 1 WHERE delta < 0)) WHERE id > (SELECT FID FROM (SELECT g1.id as FID, g1.t as FIT, (g2.t - g1.t) as delta FROM ROI_18 g1 INNER JOIN ROI_18 g2 on g2.id = g1.id + 1 WHERE delta < 0));
UPDATE ROI_19 SET t = t + (SELECT FIT from (SELECT g1.id as FID, g1.t as FIT, (g2.t - g1.t) as delta FROM ROI_19 g1 INNER JOIN ROI_19 g2 on g2.id = g1.id + 1 WHERE delta < 0)) WHERE id > (SELECT FID FROM (SELECT g1.id as FID, g1.t as FIT, (g2.t - g1.t) as delta FROM ROI_19 g1 INNER JOIN ROI_19 g2 on g2.id = g1.id + 1 WHERE delta < 0));
UPDATE ROI_20 SET t = t + (SELECT FIT from (SELECT g1.id as FID, g1.t as FIT, (g2.t - g1.t) as delta FROM ROI_20 g1 INNER JOIN ROI_20 g2 on g2.id = g1.id + 1 WHERE delta < 0)) WHERE id > (SELECT FID FROM (SELECT g1.id as FID, g1.t as FIT, (g2.t - g1.t) as delta FROM ROI_20 g1 INNER JOIN ROI_20 g2 on g2.id = g1.id + 1 WHERE delta < 0));
Commit https://github.com/gilestrolab/ethoscope/commit/7853b445f03cc9875f17a7ab2ea8a73c2db98fb6 created an issue. When data are appended to an existing database, the variable
t
restarts from 0 and that creates a wrong series in the column.A workaround for existing databases is to open the db file and issue the following SQL:
However, this needs to be fixed in the code by inheriting and maintaining the same
t0
. We also need to add a new metadata table with a different name (such asmetadata_1
) rather than simply overwrite the content of the metadata table.