Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
CaosDB Crawler
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
caosdb
Software
CaosDB Crawler
Commits
a4d62851
Verified
Commit
a4d62851
authored
1 year ago
by
Daniel Hornung
Browse files
Options
Downloads
Patches
Plain Diff
ENH: Added spss_to_datamodel script.
parent
eb52eb19
No related branches found
No related tags found
2 merge requests
!178
FIX: #96 Better error output for crawl.py script.
,
!171
sav/spss converter
Pipeline
#51032
failed
1 year ago
Stage: info
Stage: setup
Stage: cert
Stage: style
Stage: test
Changes
3
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
CHANGELOG.md
+1
-0
1 addition, 0 deletions
CHANGELOG.md
setup.cfg
+1
-0
1 addition, 0 deletions
setup.cfg
src/caoscrawler/conv_impl/spss.py
+100
-0
100 additions, 0 deletions
src/caoscrawler/conv_impl/spss.py
with
102 additions
and
0 deletions
CHANGELOG.md
+
1
−
0
View file @
a4d62851
...
...
@@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added ###
*
Support for Python 3.12 and experimental support for 3.13
*
`spss_to_datamodel`
script.
### Changed ###
...
...
This diff is collapsed.
Click to expand it.
setup.cfg
+
1
−
0
View file @
a4d62851
...
...
@@ -40,6 +40,7 @@ per-file-ignores = __init__.py:F401
[options.entry_points]
console_scripts
=
caosdb-crawler
=
caoscrawler.crawl:main
spss_to_datamodel
=
caoscrawler.conv_impl.spss:spss_to_datamodel_main
[options.extras_require]
h5_crawler
=
...
...
This diff is collapsed.
Click to expand it.
src/caoscrawler/conv_impl/spss.py
+
100
−
0
View file @
a4d62851
...
...
@@ -18,14 +18,23 @@
"""
Converter for SAV files (stored by SPSS).
"""
import
argparse
import
pandas
as
pd
import
pyreadstat
import
yaml
from
..
import
converters
from
..stores
import
GeneralStore
from
..structure_elements
import
(
File
,
StructureElement
)
TYPES
=
{
"
double
"
:
"
DOUBLE
"
,
"
string
"
:
"
TEXT
"
,
}
class
SPSSConverter
(
converters
.
TableConverter
):
"""
Converter for SAV files (stored by SPSS).
"""
...
...
@@ -44,3 +53,94 @@ class SPSSConverter(converters.TableConverter):
# embed()
return
self
.
_children_from_dataframe
(
df
)
def
spss_to_yaml
(
savfile
:
str
,
yamlfile
:
str
)
->
None
:
"""
Parse the *.sav and create basic datamodel in ``yamlfile``.
"""
_
,
meta
=
pyreadstat
.
read_sav
(
savfile
,
metadataonly
=
True
)
enums
:
dict
[
str
,
list
[
str
]]
=
{}
properties
=
{}
for
name
in
meta
.
column_names
:
prop
=
{
"
datatype
"
:
TYPES
[
meta
.
readstat_variable_types
[
name
]],
}
desc
=
meta
.
column_names_to_labels
.
get
(
name
)
if
desc
and
desc
!=
name
:
prop
[
"
description
"
]
=
desc
# Handle categorial variables
if
var_label
:
=
meta
.
variable_to_label
.
get
(
name
):
prop
[
"
datatype
"
]
=
var_label
vvl
=
meta
.
variable_value_labels
[
name
]
# reproducible (and sensible) order
label_values
=
[
vvl
[
key
]
for
key
in
sorted
(
vvl
.
keys
())]
if
label_values
not
in
enums
.
values
():
enums
[
var_label
]
=
label_values
properties
[
name
]
=
prop
output
=
f
"""
# auto-generated data model from file
"
{
savfile
}
"
.
# To insert a datamodel into LinkAhead, run:
#
# python3 -m caosadvancedtools.models.parser datamodel.yaml --sync
#
# Code for creating enum records:
#
"""
for
name
,
values
in
enums
.
items
():
for
line
in
f
"""
cont = db.Container()
for value in
{
repr
(
values
)
}
:
rec = db.Record(name=value).add_parent(name=
"
{
name
}
"
)
cont.append(rec)
cont.insert()
"""
.
splitlines
(
keepends
=
True
):
if
line
.
strip
():
output
+=
f
"
#
{
line
}
"
output
+=
"
#
\n
"
# Actual datamodel
output
+=
"""
#########
# Enums #
#########
"""
for
name
,
values
in
enums
.
items
():
output
+=
f
"""
{
name
}
:
description:
# possible values:
{
values
}
\n
"""
output
+=
(
"""
###############
# RecordTypes #
###############
DummyRT:
description: Note: Change name and enter description.
recommended_properties:
"""
+
"
"
.
join
(
yaml
.
dump
(
properties
,
allow_unicode
=
True
,
sort_keys
=
False
).
splitlines
(
keepends
=
True
)))
with
open
(
yamlfile
,
encoding
=
"
utf-8
"
,
mode
=
"
w
"
)
as
myfile
:
myfile
.
write
(
output
)
def
_parse_arguments
():
"""
Parse the arguments.
"""
parser
=
argparse
.
ArgumentParser
(
description
=
__doc__
)
parser
.
add_argument
(
'
-i
'
,
'
--input
'
,
help
=
"
The *.sav file.
"
,
required
=
True
)
parser
.
add_argument
(
'
-o
'
,
'
--outfile
'
,
help
=
"
Yaml filename to save the result
"
,
required
=
True
)
return
parser
.
parse_args
()
def
spss_to_datamodel_main
():
"""
The main function of this script.
"""
args
=
_parse_arguments
()
spss_to_yaml
(
savfile
=
args
.
input
,
yamlfile
=
args
.
outfile
)
print
(
f
"
Written datamodel to:
{
args
.
outfile
}
"
)
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment