Skip to content

Commit 201c705

Browse files
committed
Add tests and fix time series segmenter
1 parent 452859e commit 201c705

File tree

4 files changed

+193
-1
lines changed

4 files changed

+193
-1
lines changed

label_studio_ml/examples/timeseries_segmenter/model.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,11 @@ def _get_labeling_params(self) -> Dict:
4747
labels = list(tag.labels)
4848
ts_tag = self.label_interface.get_tag(to_name)
4949
time_col = ts_tag.attr.get('timeColumn')
50-
channels = [ch.attr['column'] for ch in ts_tag.children if ch.tag == 'Channel']
50+
# parse channel names from the original config since tag doesn't expose children
51+
import xml.etree.ElementTree as ET
52+
root = ET.fromstring(self.label_config)
53+
ts_elem = root.find(f".//TimeSeries[@name='{to_name}']")
54+
channels = [ch.attrib['column'] for ch in ts_elem.findall('Channel')]
5155
return {
5256
'from_name': from_name,
5357
'to_name': to_name,

label_studio_ml/examples/timeseries_segmenter/tests/__init__.py

Whitespace-only changes.
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
import os
2+
import json
3+
import sys
4+
from unittest.mock import patch
5+
6+
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "../../../..")))
7+
8+
import pytest
9+
10+
from label_studio_ml.examples.timeseries_segmenter.model import TimeSeriesSegmenter
11+
from label_studio_ml.examples.timeseries_segmenter._wsgi import init_app
12+
13+
TEST_DIR = os.path.dirname(__file__)
14+
CSV_PATH = os.path.join(TEST_DIR, "time_series.csv")
15+
16+
LABEL_CONFIG = """
17+
<View>
18+
<TimeSeriesLabels name=\"label\" toName=\"ts\">
19+
<Label value=\"Run\"/>
20+
<Label value=\"Walk\"/>
21+
</TimeSeriesLabels>
22+
<TimeSeries name=\"ts\" valueType=\"url\" value=\"$csv_url\" timeColumn=\"time\">
23+
<Channel column=\"sensorone\" />
24+
<Channel column=\"sensortwo\" />
25+
</TimeSeries>
26+
</View>
27+
"""
28+
29+
@pytest.fixture
30+
31+
def client():
32+
app = init_app(model_class=TimeSeriesSegmenter)
33+
app.config["TESTING"] = True
34+
with app.test_client() as c:
35+
yield c
36+
37+
38+
def make_task():
39+
return {
40+
"id": 1,
41+
"data": {"csv_url": CSV_PATH},
42+
"annotations": [
43+
{
44+
"result": [
45+
{
46+
"from_name": "label",
47+
"to_name": "ts",
48+
"type": "timeserieslabels",
49+
"value": {"start": 0, "end": 85, "instant": False, "timeserieslabels": ["Run"]},
50+
},
51+
{
52+
"from_name": "label",
53+
"to_name": "ts",
54+
"type": "timeserieslabels",
55+
"value": {"start": 85, "end": 99, "instant": False, "timeserieslabels": ["Walk"]},
56+
},
57+
]
58+
}
59+
],
60+
}
61+
62+
63+
def test_train_and_predict(client, tmp_path):
64+
setup_data = {"schema": LABEL_CONFIG, "project": "1"}
65+
resp = client.post("/setup", data=json.dumps(setup_data), content_type="application/json")
66+
assert resp.status_code == 200
67+
68+
task = make_task()
69+
def fake_preload(self, task, value=None, read_file=True):
70+
return open(value).read()
71+
72+
with patch.object(TimeSeriesSegmenter, "_get_tasks", return_value=[task]), \
73+
patch.object(TimeSeriesSegmenter, "preload_task_data", new=fake_preload):
74+
data = {
75+
"action": "START_TRAINING",
76+
"annotation": {"project": 1},
77+
"project": {"id": 1, "label_config": LABEL_CONFIG},
78+
}
79+
resp = client.post("/webhook", data=json.dumps(data), content_type="application/json")
80+
assert resp.status_code == 201
81+
82+
predict_data = {"tasks": [dict(id=1, data={"csv_url": CSV_PATH})], "label_config": LABEL_CONFIG, "project": "1"}
83+
resp = client.post("/predict", data=json.dumps(predict_data), content_type="application/json")
84+
assert resp.status_code == 200
85+
results = resp.json["results"]
86+
assert len(results) == 1
87+
assert len(results[0]["result"]) >= 1
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
"time","sensorone","sensortwo"
2+
0,1.4189132761558783,0.04295890639498677
3+
1,-0.33055464922257033,-1.6825827737197843
4+
2,-0.14783853370359595,-2.2322666159987135
5+
3,-0.4062405819225669,-0.05150876228821425
6+
4,0.009836416152638344,-1.3043933420724472
7+
5,0.6400690688653601,-1.1100417527237179
8+
6,1.1975258885560347,-0.4004677636522311
9+
7,1.8522006993596039,0.08163359196601522
10+
8,0.3392963861525258,0.9899244814628199
11+
9,-1.0115126071870475,-0.34159150117083514
12+
10,0.9982410375352562,-0.9699048201959408
13+
11,2.679527559218824,-0.8587722237381995
14+
12,0.013216310156181774,-0.019850678673966265
15+
13,-0.693434764855518,1.1135560170276098
16+
14,-0.6535434159026227,-0.5846086208358121
17+
15,-2.3706854080111786,-0.8074180772359892
18+
16,-0.22393552888158924,1.6245524612427589
19+
17,-0.4898969503859535,0.1849759589161384
20+
18,1.8637056881305325,0.06670194207046191
21+
19,-1.2127049047002445,-0.7895645379059979
22+
20,1.7442782924941385,0.4417500096828261
23+
21,1.5942822687040412,-1.5769082442130966
24+
22,-0.2497657265025717,-0.7448098021094416
25+
23,-0.1420934091977869,-2.4088354520491175
26+
24,-0.14213239647049,0.13112828030217719
27+
25,-1.0965819247993862,0.342827266123045
28+
26,0.759343465887864,-1.7663750560515499
29+
27,0.6335706556552418,-0.08031271207599207
30+
28,0.19055686011325793,1.8240130028210362
31+
29,1.6663403292611383,-2.803288208553166
32+
30,2.1351311849296994,1.3661472742284986
33+
31,0.6122328854446355,-0.20700657941851835
34+
32,-0.6291845890078631,-0.5183574285122479
35+
33,-1.0649144096383518,0.3322089282154532
36+
34,-0.34846609634281706,-1.7133349790102954
37+
35,-0.5599283645266178,-0.9320578122305454
38+
36,1.1270934663804841,-0.8262581422654193
39+
37,0.33448669054026414,0.22463751951543617
40+
38,-0.5987669481711283,1.4223947379025605
41+
39,0.5965155817635157,0.3909650732822056
42+
40,-0.9898878930520152,0.8467513421650513
43+
41,-0.197225739995064,-0.29910693964014146
44+
42,1.9467353883969274,0.0865200632839562
45+
43,0.5189657094582814,1.1689533706023614
46+
44,-2.398079492264712,-1.2214256048623866
47+
45,1.512289901383852,0.9948929140089912
48+
46,-1.4290073671606731,-0.9218834107919129
49+
47,2.5501383651947167,-0.011340138656272291
50+
48,-1.3950896521271114,-0.6178082237653939
51+
49,-0.6052626954068269,0.6303243259618322
52+
50,-0.4568278777285133,0.7551803811611268
53+
51,-2.9743338912453976,-0.17405155206383244
54+
52,-0.1990767501107347,0.3686509901188214
55+
53,-0.33019958874281996,-0.06333397128615563
56+
54,-0.8257638640298178,-0.2735496917636411
57+
55,0.431810563014142,0.7137582208013141
58+
56,0.9608420489592091,-0.21044949408759278
59+
57,0.4460646474204635,-0.3914746073178722
60+
58,-1.3409747019112366,0.13804754152876467
61+
59,0.009624871181974559,-0.20833122205959562
62+
60,-0.8227297919697916,0.015995969812338275
63+
61,-0.6290855188124698,1.628176219145152
64+
62,0.3971249923292305,-0.17664678681744483
65+
63,0.06548735199921946,-1.2488040970297067
66+
64,-0.828001355401466,-1.6170273706012308
67+
65,3.0333952095653345,-0.22462168322316794
68+
66,0.2112890202888948,0.3823763525423005
69+
67,0.32388705906499354,-0.457699934860096
70+
68,1.4084257130561564,-1.315054307037898
71+
69,0.9968608055650715,-0.4585415229621653
72+
70,0.2542997374687115,0.781795845292396
73+
71,-1.0590263006342107,-0.6189384598194138
74+
72,1.3245138795771965,0.5057859247629972
75+
73,0.22519227205704834,0.1401946860598382
76+
74,0.21039177557209415,0.9488193586730559
77+
75,-0.2830177638494429,0.4037889323527864
78+
76,0.043444673256776645,1.7553525309517564
79+
77,0.7957324066560296,1.2078145739052801
80+
78,0.5162870417917828,0.056516531289523435
81+
79,-0.8718181585732206,-1.9464715414629674
82+
80,-0.9127742448516984,-0.015289683349926354
83+
81,-1.6889666405978612,-0.8009288391698336
84+
82,-0.253281939112707,2.2201211189804577
85+
83,-0.3548150930827909,0.016891879903078676
86+
84,0.22725531874059118,-1.4222676650321666
87+
85,0.5445461287430365,-0.4796985673429262
88+
86,-0.9035151129315625,1.884844870508497
89+
87,0.6912502153612264,-0.9523367651897829
90+
88,-2.6598612159419557,-0.14233638006728697
91+
89,-1.068826870732308,0.18622064601708754
92+
90,-0.8453922518900675,1.3734232673030193
93+
91,-0.013822539457955984,-1.7362475093325307
94+
92,-0.4975829221220581,1.1994227070990628
95+
93,-0.10446826028697691,-0.6092739611197647
96+
94,-1.7774605252452178,0.11428546694080885
97+
95,0.2762819913279855,0.6922121053835153
98+
96,-0.2858644815014069,0.6231874538298001
99+
97,0.03887730431966822,0.060005677506153474
100+
98,0.048571084619710374,0.28089113063736676
101+
99,0.7601016643177171,0.4827603111973345

0 commit comments

Comments
 (0)