forked from openml/server-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatasets.py
More file actions
186 lines (161 loc) · 4.52 KB
/
datasets.py
File metadata and controls
186 lines (161 loc) · 4.52 KB
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
"""Translation from https://github.com/openml/OpenML/blob/c19c9b99568c0fabb001e639ff6724b9a754bbc9/openml_OS/models/api/v1/Api_data.php#L707"""
import datetime
from sqlalchemy import text
from sqlalchemy.engine import Row
from sqlalchemy.ext.asyncio import AsyncConnection
from schemas.datasets.openml import Feature
async def get(id_: int, connection: AsyncConnection) -> Row | None:
row = await connection.execute(
text(
"""
SELECT *
FROM dataset
WHERE did = :dataset_id
""",
),
parameters={"dataset_id": id_},
)
return row.one_or_none()
async def get_file(*, file_id: int, connection: AsyncConnection) -> Row | None:
row = await connection.execute(
text(
"""
SELECT *
FROM file
WHERE id = :file_id
""",
),
parameters={"file_id": file_id},
)
return row.one_or_none()
async def get_tags_for(id_: int, connection: AsyncConnection) -> list[str]:
rows = await connection.execute(
text(
"""
SELECT *
FROM dataset_tag
WHERE id = :dataset_id
""",
),
parameters={"dataset_id": id_},
)
return [row.tag for row in rows]
async def tag(id_: int, tag_: str, *, user_id: int, connection: AsyncConnection) -> None:
await connection.execute(
text(
"""
INSERT INTO dataset_tag(`id`, `tag`, `uploader`)
VALUES (:dataset_id, :tag, :user_id)
""",
),
parameters={
"dataset_id": id_,
"user_id": user_id,
"tag": tag_,
},
)
async def get_description(
id_: int,
connection: AsyncConnection,
) -> Row | None:
"""Get the most recent description for the dataset."""
row = await connection.execute(
text(
"""
SELECT *
FROM dataset_description
WHERE did = :dataset_id
ORDER BY version DESC
""",
),
parameters={"dataset_id": id_},
)
return row.first()
async def get_status(id_: int, connection: AsyncConnection) -> Row | None:
"""Get most recent status for the dataset."""
row = await connection.execute(
text(
"""
SELECT *
FROM dataset_status
WHERE did = :dataset_id
ORDER BY status_date DESC
""",
),
parameters={"dataset_id": id_},
)
return row.first()
async def get_latest_processing_update(dataset_id: int, connection: AsyncConnection) -> Row | None:
row = await connection.execute(
text(
"""
SELECT *
FROM data_processed
WHERE did = :dataset_id
ORDER BY processing_date DESC
""",
),
parameters={"dataset_id": dataset_id},
)
return row.one_or_none()
async def get_features(dataset_id: int, connection: AsyncConnection) -> list[Feature]:
rows = await connection.execute(
text(
"""
SELECT `index`,`name`,`data_type`,`is_target`,
`is_row_identifier`,`is_ignore`,`NumberOfMissingValues` as `number_of_missing_values`
FROM data_feature
WHERE `did` = :dataset_id
""",
),
parameters={"dataset_id": dataset_id},
)
return [Feature(**row, nominal_values=None) for row in rows.mappings()]
async def get_feature_values(
dataset_id: int,
*,
feature_index: int,
connection: AsyncConnection,
) -> list[str]:
rows = await connection.execute(
text(
"""
SELECT `value`
FROM data_feature_value
WHERE `did` = :dataset_id AND `index` = :feature_index
""",
),
parameters={"dataset_id": dataset_id, "feature_index": feature_index},
)
return [row.value for row in rows]
async def update_status(
dataset_id: int,
status: str,
*,
user_id: int,
connection: AsyncConnection,
) -> None:
await connection.execute(
text(
"""
INSERT INTO dataset_status(`did`,`status`,`status_date`,`user_id`)
VALUES (:dataset, :status, :date, :user)
""",
),
parameters={
"dataset": dataset_id,
"status": status,
"date": datetime.datetime.now(),
"user": user_id,
},
)
async def remove_deactivated_status(dataset_id: int, connection: AsyncConnection) -> None:
await connection.execute(
text(
"""
DELETE FROM dataset_status
WHERE `did` = :data AND `status`='deactivated'
""",
),
parameters={"data": dataset_id},
)