Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
skycoco committed Sep 19, 2023
1 parent 4a27f20 commit dce386c
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 30 deletions.
60 changes: 46 additions & 14 deletions examples/example_for_create_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,10 @@ def add_values_dataset_description(dataset_description):
# code_parameters = dataset.get_metadata(metadata_file="code_parameters")
# code_description = dataset.get_metadata(metadata_file="code_description")

des_schema = schema.get_schema("dataset_description")
des_schema.get('subtitle')
print("******************************************")
des_schema = schema.get_schema("dataset_description", name_only=False)
print(des_schema)


# NOTE: Step3.1(optional), remove entire values in dataset_description
dataset_description.clear_values()
Expand Down Expand Up @@ -191,19 +193,49 @@ def add_values_dataset_description(dataset_description):
# add_values_for_subject_metadata(subject_metadata)

# New function for add subjects and samples
# subjects = []
# for subject_user_id in [1, 2]:
# samples = []
# for sample_user_id in [1, 2]:
# sample = sm.Sample()
# sample.add_path(
# "./test_data/bids_data/sub-0{0}/sequence{1}/".format(
# subject_user_id, sample_user_id))
# samples.append(sample)
#
# subject = sm.Subject()
# subject.add_samples(samples)
# subjects.append(subject)

subjects = []
for subject_user_id in [1, 2]:
samples = []
for sample_user_id in [1, 2]:
sample = sm.Sample()
sample.add_path(
"./test_data/bids_data/sub-0{0}/sequence{1}/".format(
subject_user_id, sample_user_id))
samples.append(sample)

subject = sm.Subject()
subject.add_samples(samples)
subjects.append(subject)
samples = []

sample1 = sm.Sample()
sample1.add_path("./test_data/bids_data/sub-01/sequence1/")
sample1.add_path("./test_data/sample2/raw/dummy_sam2.txt")
samples.append(sample1)

sample2 = sm.Sample()
sample2.add_path("./test_data/bids_data/sub-01/sequence2/")
samples.append(sample2)

subject1 = sm.Subject()
subject1.add_samples(samples)
subjects.append(subject1)

samples = []

sample1 = sm.Sample()
sample1.add_path("./test_data/bids_data/sub-02/sequence1/")
samples.append(sample1)

sample2 = sm.Sample()
sample2.add_path("./test_data/bids_data/sub-02/sequence2/")
samples.append(sample2)

subject2 = sm.Subject()
subject2.add_samples(samples)
subjects.append(subject2)

dataset.add_subjects(subjects)

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

setup(
name="sparc_me",
version="2.2.3",
version="2.2.8",
description='A python tool to explore, enhance, and expand SPARC datasets and their descriptions in accordance with FAIR principles.',
author="Thiranja Prasad Babarenda Gamage, Chinchien Lin, Savindi Wijenayaka, Michael Hoffman, Linkun Gao, Haribalan Kumar",
email="[email protected], [email protected]",
Expand Down
60 changes: 48 additions & 12 deletions sparc_me/core/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import shutil
from sparc_me.core.utils import find_col_element
from datetime import datetime, timezone
from typing import List


class Metadata:
Expand Down Expand Up @@ -406,11 +407,12 @@ class Sample:
_metadata: Metadata = None
_manifest_metadata: Metadata = None


def __init__(self):
self.sample_id = ""
self.subject_id = ""
self.sample_dir = Path()
self.source_sam_dir = Path()
self.source_sample_paths: List[Path] = []
self.index = -1

def set_subject_id(self, sub_id):
Expand Down Expand Up @@ -469,16 +471,42 @@ def add_path(self, source_path):
Add sample source path to sample object
:param source_path: sample folder source path
:type source_path: str
:type source_path: str | list
"""
self.source_sam_dir = Path(source_path)
if isinstance(source_path, list):
for file_path in source_path:
self.source_sample_paths.append(Path(file_path))
else:
self.source_sample_paths.append(Path(source_path))


def set_path(self, source_path):
"""
Add sample source path to sample object
Override the Previous path
:param source_path: sample folder source path
:type source_path: str | list
"""
if isinstance(source_path, list):
self.source_sample_paths = []
for file_path in source_path:
self.source_sample_paths.append(Path(file_path))
else:
self.source_sample_paths = [Path(source_path)]


def set_values(self, metadata={}):
"""
:param metadata: key : value dict (element:value)
:type metadata: dict
"""
if not isinstance(metadata, dict):
msg = f"You should use a dict here, you provide parameter type is {type(metadata)}"
raise TypeError(msg)

for element, value in metadata.items():
if element == 'sample id' or element == 'subject id':
continue
Expand Down Expand Up @@ -511,15 +539,19 @@ def move(self):
if not self.sample_dir.exists():
self.sample_dir.mkdir(parents=True, exist_ok=True)

source_sample_files = self.source_sam_dir.rglob("*")
for file in source_sample_files:
if file.is_file():
relative_path = file.relative_to(self.source_sam_dir)
target_file = self.sample_dir / relative_path
target_file.parent.mkdir(parents=True, exist_ok=True)
shutil.copy(str(file), str(target_file))
self._update_manifest(sample_path=str(target_file))

for source_sam in self.source_sample_paths:
if source_sam.is_dir():
source_sample_files = source_sam.rglob("*")
for file in source_sample_files:
if file.is_file():
relative_path = file.relative_to(source_sam)
target_file = self.sample_dir / relative_path
target_file.parent.mkdir(parents=True, exist_ok=True)
shutil.copy(str(file), str(target_file))
self._update_manifest(sample_path=str(target_file))
elif source_sam.is_file():
shutil.copy(str(source_sam), str(self.sample_dir))
self._update_manifest(sample_path=str(self.sample_dir / source_sam.name))
def _update_manifest(self, sample_path):
"""
Update manifest metadata, after remove samples
Expand Down Expand Up @@ -661,6 +693,10 @@ def set_values(self, metadata={}):
:param metadata: key : value dict (element:value)
:type metadata: dict
"""
if not isinstance(metadata, dict):
msg = f"You should use a dict here, you provide parameter type is {type(metadata)}"
raise TypeError(msg)

for element, value in metadata.items():
if element == 'subject id':
continue
Expand Down
21 changes: 18 additions & 3 deletions sparc_me/core/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ def validate_dataset(self, dataset):
data = schema.load_data(metadata.metadata_file_path)
self.validate(data, metadata_file=metadata_file, version=metadata.version)


def validate(self, data, metadata_file, version):
"""
Validate data instance
Expand Down Expand Up @@ -149,9 +148,10 @@ def get_default_schema(version, metadata_file):

return schema

def get_schema(self, metadata_file, version="2.0.0", print_schema=True):
def get_schema(self, metadata_file, version="2.0.0", print_schema=True, required_only=True, name_only=True):
"""
get a schema via metadata_file/metadate file name
:param metadata_file: the metadata file name
:type metadata_file: str
:param version: "2.0.0"|"1.2.3"
Expand All @@ -171,7 +171,22 @@ def get_schema(self, metadata_file, version="2.0.0", print_schema=True):
with open(schema_path, 'r') as file:
schema_json: Dict = json.load(file)
if print_schema:
print(json.dumps(schema_json.get('properties'), indent=4))
if required_only:
if name_only:
print(f"The required elements for {metadata_file}:")
print(json.dumps(schema_json.get('required'), indent=4))
else:
required_items = []
for key, value in schema_json.get('properties').items():
if "required" in value and value["required"] == "Y":
required_items.append({key: value})

print(f"The required elements for {metadata_file}:")
print(json.dumps(required_items, indent=4))
return required_items
else:
print(json.dumps(schema_json.get('properties'), indent=4))

return CaseInsensitiveDict(schema_json.get('properties'))

def set_schema(self, schema):
Expand Down

0 comments on commit dce386c

Please sign in to comment.