pylindas 0.4.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- pylindas-0.4.1/LICENSE +21 -0
- pylindas-0.4.1/PKG-INFO +213 -0
- pylindas-0.4.1/README.md +169 -0
- pylindas-0.4.1/pylindas/__init__.py +7 -0
- pylindas-0.4.1/pylindas/cli.py +144 -0
- pylindas-0.4.1/pylindas/cube/__init__.py +1 -0
- pylindas-0.4.1/pylindas/cube/cube.py +570 -0
- pylindas-0.4.1/pylindas/cube/shared_dimension.py +97 -0
- pylindas-0.4.1/pylindas/example.py +29 -0
- pylindas-0.4.1/pylindas/fetch.py +259 -0
- pylindas-0.4.1/pylindas/getter/__init__.py +0 -0
- pylindas-0.4.1/pylindas/getter/get.py +102 -0
- pylindas-0.4.1/pylindas/lindas/__init__.py +0 -0
- pylindas-0.4.1/pylindas/lindas/namespaces.py +36 -0
- pylindas-0.4.1/pylindas/lindas/query.py +24 -0
- pylindas-0.4.1/pylindas/lindas/upload.py +37 -0
- pylindas-0.4.1/pylindas.egg-info/PKG-INFO +213 -0
- pylindas-0.4.1/pylindas.egg-info/SOURCES.txt +24 -0
- pylindas-0.4.1/pylindas.egg-info/dependency_links.txt +1 -0
- pylindas-0.4.1/pylindas.egg-info/entry_points.txt +2 -0
- pylindas-0.4.1/pylindas.egg-info/requires.txt +9 -0
- pylindas-0.4.1/pylindas.egg-info/top_level.txt +1 -0
- pylindas-0.4.1/pyproject.toml +37 -0
- pylindas-0.4.1/requirements.txt +9 -0
- pylindas-0.4.1/setup.cfg +4 -0
- pylindas-0.4.1/tests/test_cube.py +153 -0
pylindas-0.4.1/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Kronmar-Bafu
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
pylindas-0.4.1/PKG-INFO
ADDED
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: pylindas
|
|
3
|
+
Version: 0.4.1
|
|
4
|
+
Summary: Utilities for working with the linked data service LINDAS of the Swiss Federal Archives. Includes modules for working with cubes.
|
|
5
|
+
Author-email: Marco Kronenberg <kronenberg.marco@bafu.admin.ch>
|
|
6
|
+
License: MIT License
|
|
7
|
+
|
|
8
|
+
Copyright (c) 2024 Kronmar-Bafu
|
|
9
|
+
|
|
10
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
11
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
12
|
+
in the Software without restriction, including without limitation the rights
|
|
13
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
14
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
15
|
+
furnished to do so, subject to the following conditions:
|
|
16
|
+
|
|
17
|
+
The above copyright notice and this permission notice shall be included in all
|
|
18
|
+
copies or substantial portions of the Software.
|
|
19
|
+
|
|
20
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
21
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
22
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
23
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
24
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
25
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
26
|
+
SOFTWARE.
|
|
27
|
+
|
|
28
|
+
Project-URL: Homepage, https://github.com/Kronmar-Bafu/py-cube
|
|
29
|
+
Keywords: linked data,LINDAS,cubes,RDF
|
|
30
|
+
Classifier: Programming Language :: Python :: 3
|
|
31
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
32
|
+
Requires-Python: >=3.12
|
|
33
|
+
Description-Content-Type: text/markdown
|
|
34
|
+
License-File: LICENSE
|
|
35
|
+
Requires-Dist: numpy==2.1.3
|
|
36
|
+
Requires-Dist: pandas==2.2.3
|
|
37
|
+
Requires-Dist: pyshacl==0.26.0
|
|
38
|
+
Requires-Dist: pystardog==0.17.0
|
|
39
|
+
Requires-Dist: PyYAML==6.0.2
|
|
40
|
+
Requires-Dist: rdflib==7.0.0
|
|
41
|
+
Requires-Dist: requests==2.32.3
|
|
42
|
+
Requires-Dist: sparql-dataframe==0.4
|
|
43
|
+
Requires-Dist: SPARQLWrapper==2.0.0
|
|
44
|
+
|
|
45
|
+
# linpy
|
|
46
|
+
|
|
47
|
+
## About
|
|
48
|
+
|
|
49
|
+
`linpy` is a package to build and publish cubes as defined by [cube.link](https://cube.link), describing a schema to describe structured data from tables in [RDF](https://www.w3.org/RDF/). It allows for an alternative to the [Cube-Creator](https://cube-creator.lindas.admin.ch). Currently this project is heavily linked to the [LINDAS](lindas.admin.ch) the Swiss Federal Linked Data Service.
|
|
50
|
+
|
|
51
|
+
For further information, please refer to our [Wiki](https://github.com/Kronmar-Bafu/cubelink/wiki)
|
|
52
|
+
|
|
53
|
+
## Installation
|
|
54
|
+
|
|
55
|
+
There are two ways to install this package, locally or through the [Python Package Index (PyPI)](https://pypi.org).
|
|
56
|
+
|
|
57
|
+
### Locally
|
|
58
|
+
|
|
59
|
+
Clone this repository and `cd` into the directory. You can now install this package locally on your machine - we advise to use a virtual environment to avoid conflicts with other projects. Additionally, install all dependencies as described in `requirements.txt`
|
|
60
|
+
|
|
61
|
+
```
|
|
62
|
+
pip install -e .
|
|
63
|
+
pip install -r requirements.txt
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
### Published Version
|
|
67
|
+
|
|
68
|
+
You are able to install this package through pip without cloning the repository.
|
|
69
|
+
|
|
70
|
+
```
|
|
71
|
+
pip install lindaspy
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
## Contributing and Suggestions
|
|
75
|
+
|
|
76
|
+
If you wish to contribute to this project, feel free to clone this repository and open a pull request to be reviewed and merged.
|
|
77
|
+
|
|
78
|
+
Alternatively feel free to open an issue with a suggestion on what we could implement. We laid out a rough road map for the features ahead on our [Timetable](https://github.com/Kronmar-Bafu/cubelink/wiki/Timetable)
|
|
79
|
+
|
|
80
|
+
## Functionality
|
|
81
|
+
|
|
82
|
+
To avoid the feeling of a black box, our philosophy is to make the construction of cubes modular. The process will take place in multiple steps, outlined below.
|
|
83
|
+
|
|
84
|
+
1. **Initialization**
|
|
85
|
+
|
|
86
|
+
```
|
|
87
|
+
cube = pycube.Cube(dataframe: pd.Dataframe, cube_yaml: dict, shape_yaml: dict)
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
This step sets some need background information about the cube up.
|
|
91
|
+
|
|
92
|
+
2. **Mapping**
|
|
93
|
+
|
|
94
|
+
```
|
|
95
|
+
cube.prepare_data()
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
Adds observation URIs and applies the mappings as described in the shape yaml.
|
|
99
|
+
|
|
100
|
+
3. **Write `cube:Cube`**
|
|
101
|
+
|
|
102
|
+
```
|
|
103
|
+
cube.write_cube()
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
Writes the `cube:Cube`.
|
|
107
|
+
|
|
108
|
+
4. **Write `cube:Observation`**
|
|
109
|
+
|
|
110
|
+
```
|
|
111
|
+
cube.write_observations()
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
Writes the `cube:Observation`s and the `cube:ObservationSet`. The URI for the observations are written as `<cube_URI/observations/[list_of_key_dimensions]>`. This should avoid the possibilities of conflicts in their uniqueness.
|
|
115
|
+
|
|
116
|
+
5. **Write `cube:ObersvationConstraint`**
|
|
117
|
+
|
|
118
|
+
```
|
|
119
|
+
cube.write_shape()
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
Writes the `cube:ObservationConstraint`.
|
|
123
|
+
|
|
124
|
+
### The full work-flow
|
|
125
|
+
|
|
126
|
+
```
|
|
127
|
+
# Write the cube
|
|
128
|
+
cube = pycube.Cube(dataframe: pd.DataFrame, cube_yaml: dict, shape_yaml: dict)
|
|
129
|
+
cube.apply_mapping()
|
|
130
|
+
cube.write_cube()
|
|
131
|
+
cube.write_observations()
|
|
132
|
+
cube.write_shape()
|
|
133
|
+
|
|
134
|
+
# Upload the cube
|
|
135
|
+
cube.upload(endpoint: str, named_graph: str)
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
For an upload, use `cube.upload(endpoint: str, named_graph: str)` with the proper `endpoint` as well as `named_graph`.
|
|
139
|
+
|
|
140
|
+
A `lindas.ini` file is read for this step, containing these information as well as a password. It contains the structure:
|
|
141
|
+
|
|
142
|
+
```
|
|
143
|
+
[TEST]
|
|
144
|
+
endpoint=https://stardog-test.cluster.ldbar.ch
|
|
145
|
+
username=a-lindas-user-name
|
|
146
|
+
password=something-you-don't-need-to-see;)
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
With additional information for the other environments.
|
|
150
|
+
|
|
151
|
+
## Command line
|
|
152
|
+
|
|
153
|
+
If you wish, a command line utility is present, that expects an opinionated way to store
|
|
154
|
+
the data and the description in a directory. It then helps you to perform common operations.
|
|
155
|
+
|
|
156
|
+
### Directory Layout
|
|
157
|
+
|
|
158
|
+
The directory should be structured as follows:
|
|
159
|
+
|
|
160
|
+
- `data.csv`: This file contains the observations.
|
|
161
|
+
- `description.json` or `description.yml`: This file contains the cube and dimension descriptions.
|
|
162
|
+
|
|
163
|
+
### Command Line Usage
|
|
164
|
+
|
|
165
|
+
For example, to serialize the data, use:
|
|
166
|
+
|
|
167
|
+
```
|
|
168
|
+
python cli.py serialize <input_directory> <output_ttl_file>
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
For additional help and options, you can use:
|
|
172
|
+
|
|
173
|
+
```
|
|
174
|
+
python cli.py --help
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
### Fetching from data sources
|
|
178
|
+
|
|
179
|
+
There is the possibility to download datasets from other data sources. Right now, the functionality is basic, but
|
|
180
|
+
it could be possible in the future to extend it.
|
|
181
|
+
|
|
182
|
+
- It supports only datasets coming from data.europa.eu
|
|
183
|
+
- It supports only datasets with a Frictionless datapackage
|
|
184
|
+
|
|
185
|
+
See [Frictionless](https://frictionlessdata.io/introduction/#why-frictionless) for more information on Frictionless.
|
|
186
|
+
|
|
187
|
+
```
|
|
188
|
+
python fetch.py 'https://data.europa.eu/data/datasets/fc49eebf-3750-4c9c-a29e-6696eb644362?locale=en' example/corona/
|
|
189
|
+
```
|
|
190
|
+
|
|
191
|
+
### Examples
|
|
192
|
+
|
|
193
|
+
Multiple cube example are ready in the `example` directory.
|
|
194
|
+
|
|
195
|
+
```bash
|
|
196
|
+
$ python cli.py example list
|
|
197
|
+
corona: Corona Numbers Timeline
|
|
198
|
+
kita: Number of kids in day care facilities
|
|
199
|
+
wind: Wind turbines — operated WKA per year in Schleswig-Holstein
|
|
200
|
+
```
|
|
201
|
+
|
|
202
|
+
To load an example in a Fuseki database, you can use the load subcommand of the example command.
|
|
203
|
+
|
|
204
|
+
```bash
|
|
205
|
+
$ python cli.py example load kita
|
|
206
|
+
```
|
|
207
|
+
|
|
208
|
+
There is a `start-fuseki` command that can be used to start a Fuseki server containing data
|
|
209
|
+
from the examples.
|
|
210
|
+
|
|
211
|
+
```bash
|
|
212
|
+
$ python cli.py example start-fuseki
|
|
213
|
+
```
|
pylindas-0.4.1/README.md
ADDED
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
# linpy
|
|
2
|
+
|
|
3
|
+
## About
|
|
4
|
+
|
|
5
|
+
`linpy` is a package to build and publish cubes as defined by [cube.link](https://cube.link), describing a schema to describe structured data from tables in [RDF](https://www.w3.org/RDF/). It allows for an alternative to the [Cube-Creator](https://cube-creator.lindas.admin.ch). Currently this project is heavily linked to the [LINDAS](lindas.admin.ch) the Swiss Federal Linked Data Service.
|
|
6
|
+
|
|
7
|
+
For further information, please refer to our [Wiki](https://github.com/Kronmar-Bafu/cubelink/wiki)
|
|
8
|
+
|
|
9
|
+
## Installation
|
|
10
|
+
|
|
11
|
+
There are two ways to install this package, locally or through the [Python Package Index (PyPI)](https://pypi.org).
|
|
12
|
+
|
|
13
|
+
### Locally
|
|
14
|
+
|
|
15
|
+
Clone this repository and `cd` into the directory. You can now install this package locally on your machine - we advise to use a virtual environment to avoid conflicts with other projects. Additionally, install all dependencies as described in `requirements.txt`
|
|
16
|
+
|
|
17
|
+
```
|
|
18
|
+
pip install -e .
|
|
19
|
+
pip install -r requirements.txt
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
### Published Version
|
|
23
|
+
|
|
24
|
+
You are able to install this package through pip without cloning the repository.
|
|
25
|
+
|
|
26
|
+
```
|
|
27
|
+
pip install lindaspy
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
## Contributing and Suggestions
|
|
31
|
+
|
|
32
|
+
If you wish to contribute to this project, feel free to clone this repository and open a pull request to be reviewed and merged.
|
|
33
|
+
|
|
34
|
+
Alternatively feel free to open an issue with a suggestion on what we could implement. We laid out a rough road map for the features ahead on our [Timetable](https://github.com/Kronmar-Bafu/cubelink/wiki/Timetable)
|
|
35
|
+
|
|
36
|
+
## Functionality
|
|
37
|
+
|
|
38
|
+
To avoid the feeling of a black box, our philosophy is to make the construction of cubes modular. The process will take place in multiple steps, outlined below.
|
|
39
|
+
|
|
40
|
+
1. **Initialization**
|
|
41
|
+
|
|
42
|
+
```
|
|
43
|
+
cube = pycube.Cube(dataframe: pd.Dataframe, cube_yaml: dict, shape_yaml: dict)
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
This step sets some need background information about the cube up.
|
|
47
|
+
|
|
48
|
+
2. **Mapping**
|
|
49
|
+
|
|
50
|
+
```
|
|
51
|
+
cube.prepare_data()
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
Adds observation URIs and applies the mappings as described in the shape yaml.
|
|
55
|
+
|
|
56
|
+
3. **Write `cube:Cube`**
|
|
57
|
+
|
|
58
|
+
```
|
|
59
|
+
cube.write_cube()
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
Writes the `cube:Cube`.
|
|
63
|
+
|
|
64
|
+
4. **Write `cube:Observation`**
|
|
65
|
+
|
|
66
|
+
```
|
|
67
|
+
cube.write_observations()
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
Writes the `cube:Observation`s and the `cube:ObservationSet`. The URI for the observations are written as `<cube_URI/observations/[list_of_key_dimensions]>`. This should avoid the possibilities of conflicts in their uniqueness.
|
|
71
|
+
|
|
72
|
+
5. **Write `cube:ObersvationConstraint`**
|
|
73
|
+
|
|
74
|
+
```
|
|
75
|
+
cube.write_shape()
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
Writes the `cube:ObservationConstraint`.
|
|
79
|
+
|
|
80
|
+
### The full work-flow
|
|
81
|
+
|
|
82
|
+
```
|
|
83
|
+
# Write the cube
|
|
84
|
+
cube = pycube.Cube(dataframe: pd.DataFrame, cube_yaml: dict, shape_yaml: dict)
|
|
85
|
+
cube.apply_mapping()
|
|
86
|
+
cube.write_cube()
|
|
87
|
+
cube.write_observations()
|
|
88
|
+
cube.write_shape()
|
|
89
|
+
|
|
90
|
+
# Upload the cube
|
|
91
|
+
cube.upload(endpoint: str, named_graph: str)
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
For an upload, use `cube.upload(endpoint: str, named_graph: str)` with the proper `endpoint` as well as `named_graph`.
|
|
95
|
+
|
|
96
|
+
A `lindas.ini` file is read for this step, containing these information as well as a password. It contains the structure:
|
|
97
|
+
|
|
98
|
+
```
|
|
99
|
+
[TEST]
|
|
100
|
+
endpoint=https://stardog-test.cluster.ldbar.ch
|
|
101
|
+
username=a-lindas-user-name
|
|
102
|
+
password=something-you-don't-need-to-see;)
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
With additional information for the other environments.
|
|
106
|
+
|
|
107
|
+
## Command line
|
|
108
|
+
|
|
109
|
+
If you wish, a command line utility is present, that expects an opinionated way to store
|
|
110
|
+
the data and the description in a directory. It then helps you to perform common operations.
|
|
111
|
+
|
|
112
|
+
### Directory Layout
|
|
113
|
+
|
|
114
|
+
The directory should be structured as follows:
|
|
115
|
+
|
|
116
|
+
- `data.csv`: This file contains the observations.
|
|
117
|
+
- `description.json` or `description.yml`: This file contains the cube and dimension descriptions.
|
|
118
|
+
|
|
119
|
+
### Command Line Usage
|
|
120
|
+
|
|
121
|
+
For example, to serialize the data, use:
|
|
122
|
+
|
|
123
|
+
```
|
|
124
|
+
python cli.py serialize <input_directory> <output_ttl_file>
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
For additional help and options, you can use:
|
|
128
|
+
|
|
129
|
+
```
|
|
130
|
+
python cli.py --help
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
### Fetching from data sources
|
|
134
|
+
|
|
135
|
+
There is the possibility to download datasets from other data sources. Right now, the functionality is basic, but
|
|
136
|
+
it could be possible in the future to extend it.
|
|
137
|
+
|
|
138
|
+
- It supports only datasets coming from data.europa.eu
|
|
139
|
+
- It supports only datasets with a Frictionless datapackage
|
|
140
|
+
|
|
141
|
+
See [Frictionless](https://frictionlessdata.io/introduction/#why-frictionless) for more information on Frictionless.
|
|
142
|
+
|
|
143
|
+
```
|
|
144
|
+
python fetch.py 'https://data.europa.eu/data/datasets/fc49eebf-3750-4c9c-a29e-6696eb644362?locale=en' example/corona/
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
### Examples
|
|
148
|
+
|
|
149
|
+
Multiple cube example are ready in the `example` directory.
|
|
150
|
+
|
|
151
|
+
```bash
|
|
152
|
+
$ python cli.py example list
|
|
153
|
+
corona: Corona Numbers Timeline
|
|
154
|
+
kita: Number of kids in day care facilities
|
|
155
|
+
wind: Wind turbines — operated WKA per year in Schleswig-Holstein
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
To load an example in a Fuseki database, you can use the load subcommand of the example command.
|
|
159
|
+
|
|
160
|
+
```bash
|
|
161
|
+
$ python cli.py example load kita
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
There is a `start-fuseki` command that can be used to start a Fuseki server containing data
|
|
165
|
+
from the examples.
|
|
166
|
+
|
|
167
|
+
```bash
|
|
168
|
+
$ python cli.py example start-fuseki
|
|
169
|
+
```
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
import argparse
|
|
2
|
+
import os
|
|
3
|
+
import pandas as pd
|
|
4
|
+
import yaml
|
|
5
|
+
import py_cube
|
|
6
|
+
import logging
|
|
7
|
+
|
|
8
|
+
from py_cube.fetch import fetch
|
|
9
|
+
from py_cube.example import list_examples, load_example
|
|
10
|
+
from py_cube.cube.shared_dimension import convert_geojson_to_ttl
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
logger = logging.getLogger('pycube')
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def serialize(input_directory: str, output_ttl: str, na_values: list[str], sep: str = ",", decimal: str = "."):
|
|
17
|
+
csv_path = os.path.join(input_directory, "data.csv")
|
|
18
|
+
yml_path = os.path.join(input_directory, "description.yml")
|
|
19
|
+
json_path = os.path.join(input_directory, "description.json")
|
|
20
|
+
|
|
21
|
+
if os.path.exists(yml_path):
|
|
22
|
+
with open(yml_path) as file:
|
|
23
|
+
cube_yaml = yaml.safe_load(file)
|
|
24
|
+
elif os.path.exists(json_path):
|
|
25
|
+
with open(json_path) as file:
|
|
26
|
+
cube_yaml = yaml.safe_load(file)
|
|
27
|
+
else:
|
|
28
|
+
raise FileNotFoundError("Neither description.yml nor description.json found in the directory")
|
|
29
|
+
|
|
30
|
+
df = pd.read_csv(csv_path, na_values=na_values, sep=sep, decimal=decimal)
|
|
31
|
+
|
|
32
|
+
cube = py_cube.Cube(dataframe=df, cube_yaml=cube_yaml, environment="TEST", local=True)
|
|
33
|
+
cube.prepare_data()
|
|
34
|
+
cube.write_cube()
|
|
35
|
+
cube.write_observations()
|
|
36
|
+
cube.write_shape()
|
|
37
|
+
cube.serialize(os.path.join(os.getcwd(), output_ttl))
|
|
38
|
+
print(cube)
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
def configure_logging(log_level):
|
|
42
|
+
class CustomFormatter(logging.Formatter):
|
|
43
|
+
"""Custom logging formatter to add colors based on log level."""
|
|
44
|
+
|
|
45
|
+
COLORS = {
|
|
46
|
+
'DEBUG': '\033[0m', # Normal
|
|
47
|
+
'INFO': '\033[94m', # Blue
|
|
48
|
+
'WARNING': '\033[93m', # Yellow
|
|
49
|
+
'ERROR': '\033[91m', # Red
|
|
50
|
+
'CRITICAL': '\033[91m', # Red
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
def format(self, record):
|
|
54
|
+
log_fmt = self.COLORS.get(record.levelname, '\033[0m') + '%(levelname)s: %(message)s\033[0m'
|
|
55
|
+
formatter = logging.Formatter(log_fmt)
|
|
56
|
+
return formatter.format(record)
|
|
57
|
+
|
|
58
|
+
console_handler = logging.StreamHandler()
|
|
59
|
+
logger.setLevel(log_level)
|
|
60
|
+
console_handler.setFormatter(CustomFormatter())
|
|
61
|
+
logger.addHandler(console_handler)
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
def main():
|
|
65
|
+
parser = argparse.ArgumentParser(description="Cube data operations")
|
|
66
|
+
subparsers = parser.add_subparsers(dest="operation", help="Operation to perform")
|
|
67
|
+
|
|
68
|
+
serialize_parser = subparsers.add_parser("serialize", help="Serialize cube data")
|
|
69
|
+
serialize_parser.add_argument("input_directory", help="Directory containing the data files")
|
|
70
|
+
serialize_parser.add_argument("output_ttl", help="Output TTL file")
|
|
71
|
+
serialize_parser.add_argument("--na_value", nargs="+", help="Values to treat as NA")
|
|
72
|
+
serialize_parser.add_argument("--sep", default=",", nargs="?", help="Separator for CSV file")
|
|
73
|
+
serialize_parser.add_argument("--decimal", default=".", nargs="?", help="Decimal separator")
|
|
74
|
+
serialize_parser.add_argument("-v", "--verbose", action="count", default=0, help="Increase verbosity")
|
|
75
|
+
|
|
76
|
+
fetch_parser = subparsers.add_parser("fetch", help="Fetches a dataset from a URL")
|
|
77
|
+
fetch_parser.add_argument("input_url", type=str, help="The URL of the dataset to fetch")
|
|
78
|
+
fetch_parser.add_argument("output", type=str, help="The directory to save the output files")
|
|
79
|
+
fetch_parser.add_argument("-v", "--verbose", action="count", default=0, help="Increase verbosity")
|
|
80
|
+
|
|
81
|
+
shared_parser = subparsers.add_parser("shared", help="Shared Dimension operations")
|
|
82
|
+
shared_subparsers = shared_parser.add_subparsers(dest="suboperation", help="Shared sub-operations")
|
|
83
|
+
|
|
84
|
+
convert_geojson_parser = shared_subparsers.add_parser("convert_geojson", help="Convert GeoJSON to TTL")
|
|
85
|
+
convert_geojson_parser.add_argument("input_geojson", type=str, help="Input GeoJSON file")
|
|
86
|
+
convert_geojson_parser.add_argument("output_ttl", type=str, help="Output TTL file")
|
|
87
|
+
convert_geojson_parser.add_argument("-v", "--verbose", action="count", default=0, help="Increase verbosity")
|
|
88
|
+
|
|
89
|
+
example_parser = subparsers.add_parser("example", help="Example operations")
|
|
90
|
+
example_subparsers = example_parser.add_subparsers(dest="suboperation", help="Example sub-operations")
|
|
91
|
+
|
|
92
|
+
list_parser = example_subparsers.add_parser("list", help="List all examples")
|
|
93
|
+
list_parser.add_argument("-v", "--verbose", action="count", default=0, help="Increase verbosity")
|
|
94
|
+
|
|
95
|
+
start_fuseki_parser = example_subparsers.add_parser("start-fuseki", help="Start a Fuseki database")
|
|
96
|
+
start_fuseki_parser.add_argument("-v", "--verbose", action="count", default=0, help="Increase verbosity")
|
|
97
|
+
|
|
98
|
+
load_parser = example_subparsers.add_parser("load", help="Load an example by name")
|
|
99
|
+
load_parser.add_argument("example_name", type=str, help="The name of the example to load", choices=[example["id"] for example in list_examples()])
|
|
100
|
+
# add optional base_uri argument to load parser
|
|
101
|
+
load_parser.add_argument("--base-uri", type=str, help="The base URI for a SPARQL database (Fuseki supported)", default="http://localhost:3030/dataset")
|
|
102
|
+
load_parser.add_argument("-v", "--verbose", action="count", default=0, help="Increase verbosity")
|
|
103
|
+
|
|
104
|
+
schema_parser = subparsers.add_parser("schema", help="Schema operations")
|
|
105
|
+
schema_subparsers = schema_parser.add_subparsers(dest="suboperation", help="Schema sub-operations")
|
|
106
|
+
schema_subparsers.add_parser("import", help="Import the description schema file")
|
|
107
|
+
schema_parser.add_argument("output", type=str, help="Output file")
|
|
108
|
+
schema_parser.add_argument("-v", "--verbose", action="count", default=0, help="Increase verbosity")
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
args = parser.parse_args()
|
|
112
|
+
log_level = logging.DEBUG if args.verbose == 1 else logging.INFO
|
|
113
|
+
|
|
114
|
+
configure_logging(log_level)
|
|
115
|
+
|
|
116
|
+
if args.operation == "serialize":
|
|
117
|
+
serialize(args.input_directory, args.output_ttl, args.na_value, args.sep, args.decimal)
|
|
118
|
+
elif args.operation == "fetch":
|
|
119
|
+
fetch(args.input_url, args.output)
|
|
120
|
+
elif args.operation == "example":
|
|
121
|
+
if args.suboperation == "list":
|
|
122
|
+
examples = list_examples()
|
|
123
|
+
for example in examples:
|
|
124
|
+
print(f'{example["id"]}: {example["name"]}')
|
|
125
|
+
elif args.suboperation == "load":
|
|
126
|
+
load_example(args.example_name, args.base_uri)
|
|
127
|
+
elif args.suboperation == "start-fuseki":
|
|
128
|
+
os.system("scripts/fuseki/start.sh")
|
|
129
|
+
elif args.operation == "shared":
|
|
130
|
+
if args.suboperation == "convert_geojson":
|
|
131
|
+
convert_geojson_to_ttl(args.input_geojson, args.output_ttl)
|
|
132
|
+
elif args.operation == 'schema':
|
|
133
|
+
if args.suboperation == "import-description":
|
|
134
|
+
description_schema_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'description.schema.json')
|
|
135
|
+
with open(description_schema_path, 'r') as f:
|
|
136
|
+
schema = f.read()
|
|
137
|
+
with open(args.output, 'w') as f:
|
|
138
|
+
f.write(schema)
|
|
139
|
+
logger.debug(f"Imported description into current directory: {args.output}")
|
|
140
|
+
|
|
141
|
+
|
|
142
|
+
|
|
143
|
+
if __name__ == "__main__":
|
|
144
|
+
main()
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
from pylindas.cube.cube import Cube
|