Skip to content

Commit

Permalink
added datagroup function
Browse files Browse the repository at this point in the history
fixed output cleanup which removes methodology, system or dataset if no data present. Was causing an error if the output was called more than once. This seems to interfere with the jcamp and rruff tests as those seem to assume those sections are present
  • Loading branch information
JohnsonDylan committed Oct 29, 2021
1 parent 2cc63b1 commit c4fed82
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 21 deletions.
4 changes: 4 additions & 0 deletions example.py
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,10 @@
"dataarray": seriesy}]}
example.dataseries([dataser1])

datagrp1 = {"@id": "datagroup",
"label": "datagroup 1",
"ids": ["datapoint/1/", "datapoint/2/"]}
example.datagroup([datagrp1])

# add source
src = {'citation': 'Chalk Research Group',
Expand Down
73 changes: 52 additions & 21 deletions scidatalib/scidata.py
Original file line number Diff line number Diff line change
Expand Up @@ -577,6 +577,39 @@ def dataseries(self, series: list) -> list:
self.meta['@graph']['scidata'] = scidata
return new_series

def datagroup(self, group: list) -> list:
"""Add one or more datapoints"""
new_group = []
scidata: dict = self.meta['@graph']['scidata']
dataset: dict = scidata['dataset']
if 'datagroup' in dataset.keys():
curr_group: list = dataset['datagroup']
else:
curr_group = []
for listentry in group:
item = self.__iterate_function(listentry)
item_noid = {k: item[k] for k in set(list(item.keys())) - {'@id'}}
matched_group = 0
for groupitem in curr_group:
group_item_noid = {
k: groupitem[k] for k in set(
list(
groupitem.keys())) -
{'@id'}}
if group_item_noid == item_noid:
matched_group = groupitem
if matched_group:
new_group.append(matched_group)
self.uidindex.remove(item['@id'])
else:
new_group.append(item)
curr_group.append(item)

dataset['datagroup'] = curr_group
scidata['dataset'] = dataset
self.meta['@graph']['scidata'] = scidata
return new_group

def sources(self, sources: list, replace=False) -> dict:
"""
Add to or replace the source reference list
Expand Down Expand Up @@ -758,28 +791,26 @@ def output(self) -> dict:
for key in list(self.meta['@graph']['scidata']):
if not self.meta['@graph']['scidata'][key]:
del self.meta['@graph']['scidata'][key]
sects = ['methodology', 'system', 'dataset']
for sect in sects:
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):
del self.meta['@graph']['scidata']['methodology']
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 self.meta['@graph']['scidata'].get('methodology'):
if not self.meta['@graph']['scidata'].get(
'methodology',
{}).get(
'aspects',
False):
del self.meta['@graph']['scidata']['methodology']
if self.meta['@graph']['scidata'].get('system'):
if not self.meta['@graph']['scidata'].get(
'system',
{}).get(
'facets',
False):
del self.meta['@graph']['scidata']['system']
if self.meta['@graph']['scidata'].get('dataset'):
if not self.meta['@graph']['scidata'].get(
'dataset', {}).get('dataseries', False):
del self.meta['@graph']['scidata']['dataset']
'dataset', {}).get('datapoint', False):
if not self.meta['@graph']['scidata'].get(
'dataset', {}).get('dataseries', False):
del self.meta['@graph']['scidata']['dataset']
self.__addtoc()

return self.meta

0 comments on commit c4fed82

Please sign in to comment.