-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.qmd
284 lines (210 loc) · 5.9 KB
/
index.qmd
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
---
title: Python package for metadata schemas
title-slide-attributes:
data-background-image: assets/kul-title-bg.png
data-background-size: contain
author: Mariana Montes and Ronny Moreas
date: 2024-05-29
format:
revealjs:
theme: [default, assets/kul.scss]
footer: iRODS UGM 2024, Amsterdam
logo: assets/kul-logo.png
echo: true
code-annotations: hover
---
```{python}
# | label: setup
# | include: false
import os, os.path
import json
from datetime import date
from irods.session import iRODSSession
from mango_mdschema import Schema
home_dir = "/icts/home/datateam_icts_icts_test/irodsugm_demo"
env_file = ".irods_environment.json"
session = iRODSSession(irods_env_file=env_file)
```
## Outline
- The ManGO Metadata Schema Manager
- From JSON to validation
- From a Python dictionary to AVUs
- Conclusion
# The ManGO Metadata Schema Manager
## Form to add metadata
::: {style="overflow-y: scroll; height: 600px;"}
![](img/empty-mdschema-form.png)
:::
::: footer
[ManGO Schema Manager](github.com/kuleuven/mango-metadata-schemas)
:::
## View of the schema metadata
::: {style="overflow-y: scroll; height: 600px;"}
![](img/view-metadata.png)
:::
::: footer
[ManGO Schema Manager](github.com/kuleuven/mango-metadata-schemas)
:::
## The Schema Manager
::: {style="overflow-y: scroll; height: 600px;"}
![](img/schema-manager.png)
:::
::: footer
[ManGO Schema Manager](github.com/kuleuven/mango-metadata-schemas)
:::
## Metadata Schemas as JSON
book-v2.0.0-published.json
```{python}
#| echo: false
with open("book-v2.0.0-published.json", "r") as f:
schema_json = json.load(f)
schema_json
```
## Minimal example
```sh
pip install mango-mdschema
```
<br>
```{.python filename="add_schema_metadata.py" code-line-numbers="|2,9,10|1,5,6|3,8,11|"}
import json
from irods.session import iRODSSession
from mango_mdschema import Schema
with open("metadata_file.json", "r") as f:
my_metadata = json.load(f) # a dictionary
my_schema = Schema("book-v2.0.0-published.json")
with iRODSSession(irods_env_file=env_file) as session:
irods_object = session.collections.get(home_dir).data_objects[0]
my_schema.apply(irods_object, my_metadata) # includes validation
```
# From JSON to validation
## Metadata schemas as JSON
book-v2.0.0-published.json
```{python}
#| echo: false
schema_json
```
## Interpretation via the Python package
```{python}
book_schema = Schema("book-v2.0.0-published.json") # <1>
print(book_schema)
```
1. The optional `prefix` argument lets you tailor it to your implementation.
## Field requirements
```{python}
book_schema.print_requirements("publishing_date")
```
```{python}
book_schema.print_requirements("publisher")
```
## Field requirements
```{python}
book_schema.print_requirements("cover_colors")
```
# From a Python dictionary to AVUs
## Required fields and defaults
```{python}
#| output-location: fragment
my_metadata = {
"title": "An exemplary book", # <1>
"author": [ # <2>
{"name": "Fulano De Tal", "email": "[email protected]"}, # <2>
{"name": "Jane Doe", "email": "[email protected]"}, # <2>
], # <2>
"ebook": "Available",
"publishing_date": "2024-02-01", # <3>
"cover_colors": ["red", "magenta", "yellow", "turquoise"], # <4>
}
book_schema.validate(my_metadata) # <5>
```
1. A required field without a default
2. A repeatable composite field
3. A repeatable date
4. A field with multiple possible values, not all of them valid
5. Required fields with default are filled in.
## Error messages
```{python}
#| error: true
#| output-location: fragment
book_schema.validate(
{
"title": "Some title",
"author": {"name": "Jane Doe", "email": "[email protected]"},
"publishing_date": date.today(),
}
)
```
```{python}
# | error: true
#| output-location: fragment
book_schema.validate(
{
"title": "Some title",
"author": {"name": "Jane Doe", "email": "[email protected]"},
"publishing_date": "01/01/1990",
}
)
```
## Warnings
```{python}
# | message: true
# | warning: true
#| output-location: fragment
import logging
logger = logging.getLogger("mango_mdschema")
logger.setLevel(logging.INFO)
book_schema.validate(my_metadata)
```
## Write: from dictionaries to namespacing
```{python}
irods_object = session.collections.get(home_dir).data_objects[0]
irods_object.metadata.items()
```
```{python}
#| output-location: fragment
avus = book_schema.to_avus(my_metadata)
avus
```
## Write: from dictionaries to namespacing
```{python}
book_schema.apply(irods_object, my_metadata)
irods_object.metadata.items()
```
## Read: from AVUs back to dictionaries
```{python}
#book_schema.from_avus(avus)
book_schema.extract(irods_object)
```
# Conclusion
## Metadata schemas with Python
:::: {.columns}
::: {.column width="50%"}
### Metadata schemas
- Format validation
- Required fields and default values
- Hierarchical structure
:::
::: {.column width="50%"}
### Python
- Processing data in badges
- Reading metadata from files
- E.g. metadata with data ingestion
:::
::::
::: callout-tip
You don't need ManGO, these are also standalone applications!
:::
## `mango-mdschema`
- Offers validation, writing and reading of structured metadata
- Schemas are described in JSON, can be designed in the manager
- Metadata can be hierarchical, rendered with namespacing
- Input can be automatized, output can be parsed and rendered in the portal
# Thank you! {background-color="#DCE7F0"}
[github.com/kuleuven/mango-mdschema](https://github.com/kuleuven/mango-mdschema)
[github.com/kuleuven/mango-metadata-schemas](https://github.com/kuleuven/mango-metadata-schemas)
::: footer
:::
```{python}
#| include: false
irods_object.metadata.remove_all()
session.cleanup()
```