Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: output one workbook per EML file #3

Merged
merged 2 commits into from
Aug 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 9 additions & 34 deletions src/spinneret/workbook.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
"""The workbook module"""

import os
from uuid import uuid4
from lxml import etree
import pandas as pd


def create(
eml: str, elements: list, base_url: str, path_out: str = False
eml_file: str, elements: list, base_url: str, path_out: str = False
) -> pd.core.frame.DataFrame:
"""Create an annotation workbook from EML files
"""Create an annotation workbook from an EML file

:param eml: Path to a directory containing only EML files or a path to a
single EML file
:param eml_file: Path to a single EML file
:param elements: List of EML elements to include in the workbook. Can be
one or more of: 'dataset', 'dataTable', 'otherEntity',
'spatialVector', 'spatialRaster', 'storedProcedure',
Expand All @@ -24,7 +22,8 @@ def create(
linking the data curator to the full human readable
data package landing page.
:param path_out: Path to a directory where the workbook will be written.
Will result in a file 'annotation_workbook.tsv'.
Will result in a file
'[packageId]_annotation_workbook.tsv'.
:returns: DataFrame of the annotation workbook with columns:

* `package_id`: Data package identifier listed in the EML at
Expand Down Expand Up @@ -54,34 +53,7 @@ def create(
* `comment`: Comments related to the annotation. Can be useful
when revisiting an annotation at a later date.
"""
if os.path.isdir(eml):
eml = [eml + "/" + p for p in os.listdir(eml)]
else:
eml = [eml]
res = []
for f in eml:
df = elements_to_df(f, elements, base_url)
res.append(df)
res = pd.concat(res)
if path_out:
path_out = path_out + "/" + "annotation_workbook.tsv"
res.to_csv(path_out, sep="\t", index=False, mode="x")
return res


def elements_to_df(eml: str, elements: list, base_url: str) -> pd.core.frame.DataFrame:
"""Convert a single EML file to a dataframe

This function is called by 'workbook.create()' to form a list of dataframes,
one for each EML file, then concatenate into the full workbook dataframe.

:param eml: See parameter definition in 'workbook.create()'
:param elements: See parameter definition in 'workbook.create()'
:param base_url: See parameter definition in 'workbook.create()'
:returns: DataFrame of the annotation workbook. See column definitions in
`workbook.create`.
"""
eml = etree.parse(eml)
eml = etree.parse(eml_file)
package_id = eml.xpath("./@packageId")[0]
url = base_url + package_id
res = []
Expand Down Expand Up @@ -122,6 +94,9 @@ def elements_to_df(eml: str, elements: list, base_url: str) -> pd.core.frame.Dat
"comment",
]
res = pd.DataFrame(res, columns=colnames)
if path_out:
path_out = path_out + "/" + package_id + "_annotation_workbook.tsv"
res.to_csv(path_out, sep="\t", index=False, mode="x")
return res


Expand Down
72 changes: 0 additions & 72 deletions tests/annotation_workbook.tsv

This file was deleted.

Loading
Loading