Skip to content

Commit deb2799

Browse files
authored
Merge pull request #36 from Peyman-N/openminds_persons
creating person
2 parents df64ded + c29d5de commit deb2799

File tree

4 files changed

+97
-3
lines changed

4 files changed

+97
-3
lines changed

bids2openminds/main.py

+62-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
import re
12
import os
23
import pathlib
34
from warnings import warn
45

56
import pandas as pd
7+
from nameparser import HumanName
68

79
import openminds.latest.core as omcore
810
import openminds.latest.controlled_terms as controlled_terms
@@ -12,6 +14,64 @@
1214
from .mapping import bids2openminds_instance
1315

1416

17+
def create_openminds_person(full_name):
18+
# Regex for detecting any unwanted characters.
19+
name_regex = re.compile(
20+
"^[\w'\-, .][^0-9_!¡?÷?¿/\\+=@#$%^&*(){}|~<>;:[\]]{1,}$")
21+
alternate_names = []
22+
person = HumanName(full_name)
23+
given_name = person.first
24+
family_name = person.last
25+
26+
# Handle the situation in which there is no given name or the given name consists of unwanted characters.
27+
if not (given_name and name_regex.match(given_name)):
28+
return None
29+
30+
# Handle the situation in which the family name consists of unwanted characters.
31+
if not (name_regex.match(family_name)):
32+
family_name = None
33+
34+
if person.middle:
35+
given_name = f"{given_name} {person.middle}"
36+
37+
if person.nickname:
38+
alternate_names.append(person.nickname)
39+
40+
if not alternate_names:
41+
alternate_names = None
42+
43+
openminds_person = omcore.Person(
44+
alternate_names=alternate_names, given_name=given_name, family_name=family_name)
45+
46+
return openminds_person
47+
48+
49+
def create_persons(dataset_description, collection):
50+
51+
if "Authors" in dataset_description:
52+
person_list = dataset_description["Authors"]
53+
else:
54+
return None
55+
56+
if not (isinstance(person_list, list)):
57+
# handel's only one name
58+
if isinstance(person_list, str):
59+
openminds_person = create_openminds_person(person_list)
60+
if openminds_person is not None:
61+
collection.add(openminds_person)
62+
return openminds_person
63+
else:
64+
return None
65+
66+
openminds_list = []
67+
for person in person_list:
68+
openminds_person = create_openminds_person(person)
69+
openminds_list.append(openminds_person)
70+
collection.add(openminds_person)
71+
72+
return openminds_list
73+
74+
1575
def create_techniques(layout_df):
1676
suffixs = layout_df["suffix"].unique().tolist()
1777
techniques = []
@@ -56,8 +116,7 @@ def create_dataset_version(bids_layout, dataset_description, layout_df, studied_
56116
else:
57117
digital_identifier = None
58118

59-
# TODO extract person
60-
# author=person_create(dataset_description["Authors"])
119+
authors = create_persons(dataset_description, collection)
61120

62121
if "Acknowledgements" in dataset_description:
63122
other_contribution = dataset_description["Acknowledgements"]
@@ -92,6 +151,7 @@ def create_dataset_version(bids_layout, dataset_description, layout_df, studied_
92151
experimental_approaches=experimental_approaches,
93152
short_name=dataset_description["Name"],
94153
studied_specimens=studied_specimens,
154+
authors=authors,
95155
techniques=techniques,
96156
how_to_cite=how_to_cite,
97157
repository=file_repository,

pyproject.toml

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ dependencies = [
66
"openminds >= 0.2.2",
77
"click>=8.1",
88
"pandas",
9+
"nameparser >= 1.1.3"
910
]
1011
requires-python = ">=3.9"
1112
authors = [

test/test_example_datasets_click.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from bids2openminds.converter import convert_click
44
from click.testing import CliRunner
55

6-
(test_data_set, number_of_openminds_files) = ("ds003", 98)
6+
(test_data_set, number_of_openminds_files) = ("ds003", 100)
77

88

99
def test_example_datasets_click():

test/test_person.py

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# test for create_openminds_person function in the main
2+
import pytest
3+
from bids2openminds.main import create_openminds_person
4+
import openminds.latest.core as omcore
5+
6+
# Test data: (full_name, given_name, family_name)
7+
example_names = [("John Ronald Reuel Tolkien", "John Ronald Reuel", "Tolkien"),
8+
("Bilbo Baggins", "Bilbo", "Baggins"),
9+
("Xue, G.", "G.", "Xue"),
10+
("Arndís Þórarinsdóttir", "Arndís", "Þórarinsdóttir"),
11+
("Loïc Le Clézio", "Loïc", "Le Clézio")]
12+
13+
example_not_names = ["42", "#", "", "A34 hajb"]
14+
15+
16+
@pytest.mark.parametrize("full_name, given_name, family_name", example_names)
17+
def test_create_openminds_person(full_name, given_name, family_name):
18+
openminds_person_object = omcore.Person(given_name=given_name,
19+
family_name=family_name)
20+
bids2openminds_person_object = create_openminds_person(full_name)
21+
assert openminds_person_object.given_name == bids2openminds_person_object.given_name, \
22+
f"Given names don't match for input '{full_name}'"
23+
assert openminds_person_object.family_name == bids2openminds_person_object.family_name, \
24+
f"Family names don't match for input '{full_name}'"
25+
assert openminds_person_object.type_ == bids2openminds_person_object.type_, \
26+
f"Person types don't match for input '{full_name}'"
27+
# assert openminds_person_object == bids2openminds_person_object
28+
29+
30+
@pytest.mark.parametrize("full_not_name", example_not_names)
31+
def test_create_openminds_person_not_names(full_not_name):
32+
bids2openminds_person_object = create_openminds_person(full_not_name)
33+
assert bids2openminds_person_object is None

0 commit comments

Comments
 (0)