Skip to content

Commit

Permalink
remade __iterate_function. Should fix enumeration issue 63
Browse files Browse the repository at this point in the history
replaced twine with poetry in pypi build
  • Loading branch information
JohnsonDylan committed Aug 9, 2021
1 parent 08f09a9 commit 6d2730b
Show file tree
Hide file tree
Showing 6 changed files with 141 additions and 90 deletions.
7 changes: 4 additions & 3 deletions .github/workflows/actions.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ jobs:
- name: Build Python package and Upload to TestPyPi
shell: bash -l {0}
if: ${{ matrix.python-version == env.PYTHON_MAIN_VERSION }}
env:
TEST_PYPI_TOKEN_PASSWORD: ${{ secrets.TEST_PYPI_TOKEN }}
run: |
Expand Down Expand Up @@ -64,6 +65,6 @@ jobs:
env:
PYPI_TOKEN_PASSWORD: ${{ secrets.PYPI_TOKEN }}
run: |
pip install wheel twine
python setup.py sdist bdist_wheel
twine upload --username "__token__" --password $PYPI_TOKEN_PASSWORD dist/*
poetry update
poetry build
poetry publish --username "__token__" --password $TEST_PYPI_TOKEN_PASSWORD
2 changes: 1 addition & 1 deletion example.py
Original file line number Diff line number Diff line change
Expand Up @@ -328,4 +328,4 @@
lic = 'https://creativecommons.org/licenses/by-nc-nd/4.0/'
example.rights(holder, lic)

print(json.dumps(example.output, ensure_ascii=False))
print(json.dumps(example.output, indent=4, ensure_ascii=False))
6 changes: 3 additions & 3 deletions example2.py
Original file line number Diff line number Diff line change
Expand Up @@ -294,9 +294,9 @@

example.dataseries([dataser1])

example.datagroup(
[{"@id": "datagroup", "@type": "sdo:datagroup",
"datapoints": ["datapoint"]}])
# example.datagroup(
# [{"@id": "datagroup", "@type": "sdo:datagroup",
# "datapoints": ["datapoint"]}])

# add source
src = {'citation': 'Chalk Research Group',
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "SciDataLib"
version = "0.2.3.1"
version = "0.2.3.2"
description = "Python library for development of SciData JSON-LD files"
authors = [
"Stuart Chalk <schalk@unf.edu>",
Expand Down
121 changes: 81 additions & 40 deletions scidatalib/scidata.py
Original file line number Diff line number Diff line change
Expand Up @@ -415,38 +415,29 @@ def evaluation(self, evaln: str) -> str:

def aspects(self, aspects: list) -> list:
"""Add to or replace the aspects of the file"""
cnt_index = {}

scidata: dict = self.meta['@graph']['scidata']
meth: dict = scidata['methodology']
curr_aspects: list = meth['aspects']

for item in aspects:
cat_index = {}
item, category, count, cat_index = self.__iterate_function(
item, 1, cnt_index, cat_index)
cnt_index[category] = count
uidindex = []
for listentry in aspects:
item, uidindex = self.__iterate_function2(listentry, uidindex)
curr_aspects.append(item)

meth['aspects'] = curr_aspects

scidata['methodology'] = meth
self.meta['@graph']['scidata'] = scidata
return curr_aspects

def facets(self, facets: list) -> list:
"""Add to or replace the facets of the file"""
cnt_index = {}

scidata: dict = self.meta['@graph']['scidata']
system: dict = scidata['system']
curr_facets: list = system['facets']

for item in facets:
cat_index = {}
item, category, count, cat_index = self.__iterate_function(
item, 1, cnt_index, cat_index)
cnt_index[category] = count
uidindex = []
for listentry in facets:
item, uidindex = self.__iterate_function2(listentry, uidindex)
curr_facets.append(item)

system['facets'] = curr_facets
Expand All @@ -472,21 +463,17 @@ def scope(self, scope: [str, list]) -> str:

def attribute(self, attributes: list) -> list:
"""Add one or more attributes"""
cnt_index = {}

scidata: dict = self.meta['@graph']['scidata']
dataset: dict = scidata['dataset']
if 'attribute' in dataset.keys():
curr_attributes: list = dataset['attribute']
else:
curr_attributes = []
uidindex = []

for item in attributes:
cat_index = {}
cnt_index = {'attribute': len(curr_attributes)}
item, category, count, cat_index = self.__iterate_function(
item, 1, cnt_index, cat_index)
cnt_index[category] = count
for listentry in attributes:
item, uidindex = self.__iterate_function2(listentry, uidindex)
curr_attributes.append(item)

dataset['attribute'] = curr_attributes
Expand All @@ -496,21 +483,17 @@ def attribute(self, attributes: list) -> list:

def datapoint(self, points: list) -> list:
"""Add one or more datapoints"""
cnt_index = {}

scidata: dict = self.meta['@graph']['scidata']
dataset: dict = scidata['dataset']
if 'datapoint' in dataset.keys():
curr_points: list = dataset['datapoint']
else:
curr_points = []
uidindex = []

for item in points:
cat_index = {}
cnt_index = {'datapoint': len(curr_points)}
item, category, count, cat_index = self.__iterate_function(
item, 1, cnt_index, cat_index)
cnt_index[category] = count
for listentry in points:
item, uidindex = self.__iterate_function2(listentry, uidindex)
curr_points.append(item)

dataset['datapoint'] = curr_points
Expand All @@ -520,21 +503,17 @@ def datapoint(self, points: list) -> list:

def dataseries(self, series: list) -> list:
"""Add one or more datapoints"""
cnt_index = {}

scidata: dict = self.meta['@graph']['scidata']
dataset: dict = scidata['dataset']
if 'dataseries' in dataset.keys():
curr_series: list = dataset['dataseries']
else:
curr_series = []
uidindex = []

for item in series:
cat_index = {}
cnt_index = {'dataseries': len(curr_series)}
item, category, count, cat_index = self.__iterate_function(
item, 1, cnt_index, cat_index)
cnt_index[category] = count
for listentry in series:
item, uidindex = self.__iterate_function2(listentry, uidindex)
curr_series.append(item)

dataset['dataseries'] = curr_series
Expand Down Expand Up @@ -722,6 +701,58 @@ def __make_context(self) -> dict:
self.meta["@context"] = c + [n, b]
return self.meta["@context"]

def __iterate_function2(self, it, uidindex, uid=False):

if isinstance(it, str):
self.__addid(it)
return it, uidindex
prev_uid = uid

# Set the category
if '@id' in it:
category = it['@id']
elif 'descriptors' in it or 'identifiers' in it:
category = 'compound'
else:
category = 'undefined'

if prev_uid:
uid = prev_uid + category + '/' + str(1) + '/'
else:
uid = category + '/' + str(1) + '/'

def enumuid(uid):
if uid in uidindex:
uidsplit = uid.rsplit('/', 2)
uid = uidsplit[0] + '/' + str(int(uidsplit[1]) + 1) + '/'
return uid

while uid in uidindex:
uid = enumuid(uid)
uidindex.append(uid)

temp: dict = {'@id': uid, '@type': 'sdo:' + category}

for key, value in it.items():
if key != '@id':

if isinstance(value, list):
listuid = uid
for i, listentry in enumerate(value):
value[i], uidindex = self.__iterate_function2(
listentry, uidindex, listuid)
temp[key] = value

elif isinstance(value, dict):
temp[key], uidindex = self.__iterate_function2(
value, uidindex, uid)

else:
temp[key] = value
self.__addid(value)

return temp, uidindex

def __iterate_function(self, it, level, cnt_index, cat_index):
"""
Recursive function to iteratively update @id, @type, and categories
Expand Down Expand Up @@ -830,12 +861,22 @@ def output(self) -> dict:
for key in list(self.meta['@graph']['scidata'][sect]):
if not self.meta['@graph']['scidata'][sect][key]:
del self.meta['@graph']['scidata'][sect][key]
if not self.meta['@graph']['scidata'].get('methodology', {}).get('aspects', False):
if not self.meta['@graph']['scidata'].get(
'methodology',
{}).get(
'aspects',
False):
del self.meta['@graph']['scidata']['methodology']
if not self.meta['@graph']['scidata'].get('system', {}).get('facets', False):
if not self.meta['@graph']['scidata'].get(
'system',
{}).get(
'facets',
False):
del self.meta['@graph']['scidata']['system']
if not self.meta['@graph']['scidata'].get('dataset', {}).get('datapoint', False):
if not self.meta['@graph']['scidata'].get('dataset', {}).get('dataseries', False):
if not self.meta['@graph']['scidata'].get(
'dataset', {}).get('datapoint', False):
if not self.meta['@graph']['scidata'].get(
'dataset', {}).get('dataseries', False):
del self.meta['@graph']['scidata']['dataset']
self.__addtoc()

Expand Down
93 changes: 51 additions & 42 deletions tests/test_scidata.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,10 @@ def test_aspects(sd):


def test_facets(sd):
sd.namespaces({'obo': 'http://purl.obolibrary.org/obo/',
"sdo": "https://stuchalk.github.io/scidata/ontology/scidata.owl#"})
sd.namespaces({
'obo': 'http://purl.obolibrary.org/obo/',
"sdo": "https://stuchalk.github.io/scidata/ontology/scidata.owl#"
})
compd = {
"@id": "substance",
"title": "3 ppm cyanide standard solution",
Expand Down Expand Up @@ -241,46 +243,53 @@ def test_facets(sd):
"aggregation": "sub:aq",
"mixtype": "sub:homogeneousSolution",
"phase": "sub:liquid",
"constituents": [{"@id": "substance/1/constituent/1/",
"@type": "sdo:constituent",
"source": "compound/1/",
"role": "chm:analyte",
"properties": [{"@id": "substance/1/constituent/1/property/1/",
"@type": "sdo:property",
"quantity": "mass of substance per volume",
"property": "Concentration (w/v)",
"value": {"@id": "substance/1/constituent/1/property/1/value/1/",
"@type": "sdo:value",
"number": 2.99,
"unitref": "qudt:PPM"}},
{"@id": "substance/1/constituent/1/property/2/",
"@type": "sdo:property",
"quantity": "volume",
"property": "Volume of solution",
"value": {"@id": "substance/1/constituent/1/property/2/value/1/",
"@type": "sdo:value",
"number": 100.0,
"unitref": "qudt:MilliL"}}]},
{"@id": "substance/1/constituent/2/",
"@type": "sdo:constituent",
"source": "compound/2/",
"role": "chm:reagent",
"properties": [{"@id": "substance/1/constituent/2/property/1/",
"@type": "sdo:property",
"quantity": "mass of substance per volume",
"property": "Concentration (w/v)",
"value": {"@id": "substance/1/constituent/2/property/1/value/1/",
"@type": "sdo:value",
"number": 2.99,
"unitref": "qudt:PPM"}},
{"@id": "substance/1/constituent/2/property/2/",
"@type": "sdo:property",
"quantity": "volume",
"property": "Volume of solution",
"value": {"@id": "substance/1/constituent/2/property/2/value/1/",
"@type": "sdo:value",
"number": 100.0,
"unitref": "qudt:MilliL"}}]}],
"constituents":
[{"@id": "substance/1/constituent/1/",
"@type": "sdo:constituent",
"source": "compound/1/",
"role": "chm:analyte",
"properties": [
{"@id": "substance/1/constituent/1/property/1/",
"@type": "sdo:property",
"quantity": "mass of substance per volume",
"property": "Concentration (w/v)",
"value": {
"@id": "substance/1/constituent/1/property/1/value/1/",
"@type": "sdo:value",
"number": 2.99,
"unitref": "qudt:PPM"}},
{"@id": "substance/1/constituent/1/property/2/",
"@type": "sdo:property",
"quantity": "volume",
"property": "Volume of solution",
"value": {
"@id": "substance/1/constituent/1/property/2/value/1/",
"@type": "sdo:value",
"number": 100.0,
"unitref": "qudt:MilliL"}}]},
{"@id": "substance/1/constituent/2/",
"@type": "sdo:constituent",
"source": "compound/2/",
"role": "chm:reagent",
"properties": [
{"@id": "substance/1/constituent/2/property/1/",
"@type": "sdo:property",
"quantity": "mass of substance per volume",
"property": "Concentration (w/v)",
"value": {
"@id": "substance/1/constituent/2/property/1/value/1/",
"@type": "sdo:value",
"number": 2.99,
"unitref": "qudt:PPM"}},
{"@id": "substance/1/constituent/2/property/2/",
"@type": "sdo:property",
"quantity": "volume",
"property": "Volume of solution",
"value": {
"@id": "substance/1/constituent/2/property/2/value/1/",
"@type": "sdo:value",
"number": 100.0,
"unitref": "qudt:MilliL"}}]}],
"properties": [{"@id": "substance/1/property/1/",
"@type": "sdo:property",
"quantity": "volume",
Expand Down

0 comments on commit 6d2730b

Please sign in to comment.