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

creating person #36

Merged
merged 6 commits into from
May 16, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
64 changes: 62 additions & 2 deletions bids2openminds/main.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import re
import os
import pathlib
from warnings import warn

import pandas as pd
from nameparser import HumanName

import openminds.latest.core as omcore
import openminds.latest.controlled_terms as controlled_terms
Expand All @@ -12,6 +14,64 @@
from .mapping import bids2openminds_instance


def create_openminds_person(full_name):
# Regex for detecting any unwanted characters.
name_regex = re.compile(
"^[\w'\-, .][^0-9_!¡?÷?¿/\\+=@#$%^&*(){}|~<>;:[\]]{1,}$")
alternate_names = []
person = HumanName(full_name)
given_name = person.first
family_name = person.last

# Handle the situation in which there is no given name or the given name consists of unwanted characters.
if not (given_name and name_regex.match(given_name)):
return None

# Handle the situation in which the family name consists of unwanted characters.
if not (name_regex.match(family_name)):
family_name = None

if person.middle:
given_name = f"{given_name} {person.middle}"

if person.nickname:
alternate_names.append(person.nickname)

if not alternate_names:
alternate_names = None

openminds_person = omcore.Person(
alternate_names=alternate_names, given_name=given_name, family_name=family_name)

return openminds_person


def create_persons(dataset_description, collection):

if "Authors" in dataset_description:
person_list = dataset_description["Authors"]
else:
return None

if not (isinstance(person_list, list)):
# handel's only one name
if isinstance(person_list, str):
openminds_person = create_openminds_person(person_list)
if openminds_person is not None:
collection.add(openminds_person)
return openminds_person
else:
return None

openminds_list = []
for person in person_list:
openminds_person = create_openminds_person(person)
openminds_list.append(openminds_person)
collection.add(openminds_person)

return openminds_list


def create_techniques(layout_df):
suffixs = layout_df["suffix"].unique().tolist()
techniques = []
Expand Down Expand Up @@ -56,8 +116,7 @@ def create_dataset_version(bids_layout, dataset_description, layout_df, studied_
else:
digital_identifier = None

# TODO extract person
# author=person_create(dataset_description["Authors"])
authors = create_persons(dataset_description, collection)

if "Acknowledgements" in dataset_description:
other_contribution = dataset_description["Acknowledgements"]
Expand Down Expand Up @@ -92,6 +151,7 @@ def create_dataset_version(bids_layout, dataset_description, layout_df, studied_
experimental_approaches=experimental_approaches,
short_name=dataset_description["Name"],
studied_specimens=studied_specimens,
authors=authors,
techniques=techniques,
how_to_cite=how_to_cite,
repository=file_repository,
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ dependencies = [
"openminds >= 0.2.2",
"click>=8.1",
"pandas",
"nameparser >= 1.1.3"
]
requires-python = ">=3.9"
authors = [
Expand Down
2 changes: 1 addition & 1 deletion test/test_example_datasets_click.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from bids2openminds.converter import convert_click
from click.testing import CliRunner

(test_data_set, number_of_openminds_files) = ("ds003", 98)
(test_data_set, number_of_openminds_files) = ("ds003", 100)


def test_example_datasets_click():
Expand Down
32 changes: 32 additions & 0 deletions test/test_person.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# test for create_openminds_person function in the main
import pytest
from bids2openminds.main import create_openminds_person
import openminds.latest.core as omcore

# Test data: (full_name, given_name, family_name)
example_names = [("John Ronald Reuel Tolkien", "John Ronald Reuel", "Tolkien"),
("Bilbo Baggins", "Bilbo", "Baggins"), ("Xue, G.", "G.", "Xue"),
("Arndís Þórarinsdóttir", "Arndís", "Þórarinsdóttir"),
("Loïc Le Clézio", "Loïc", "Le Clézio")]

example_not_names = ["42", "#", "", "A34 hajb"]


@pytest.mark.parametrize("full_name,given_name,family_name", example_names)
def test_create_openminds_person(full_name, given_name, family_name):
openminds_person_object = omcore.Person(given_name=given_name,
family_name=family_name)
bids2openminds_person_object = create_openminds_person(full_name)
assert openminds_person_object.given_name == bids2openminds_person_object.given_name, \
f"Given names don't match for input '{full_name}'"
assert openminds_person_object.family_name == bids2openminds_person_object.family_name, \
f"Family names don't match for input '{full_name}'"
assert openminds_person_object.type_ == bids2openminds_person_object.type_, \
f"Person types don't match for input '{full_name}'"
# assert openminds_person_object == bids2openminds_person_object


@pytest.mark.parametrize("full_not_name", example_not_names)
def test_create_openminds_person_not_names(full_not_name):
bids2openminds_person_object = create_openminds_person(full_not_name)
assert bids2openminds_person_object is None