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

enforce str dtype for recording_filename/session_id/child_id #295

Merged
merged 3 commits into from
Sep 28, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
15 changes: 12 additions & 3 deletions ChildProject/projects.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class ChildProject:
description="unique child ID -- unique within the experiment (Id could be repeated across experiments to refer to different children)",
unique=True,
required=True,
dtype="str",
),
IndexColumn(
name="child_dob",
Expand Down Expand Up @@ -127,6 +128,7 @@ class ChildProject:
name="child_id",
description="unique child ID -- unique within the experiment (Id could be repeated across experiments to refer to different children)",
required=True,
dtype="str",
),
IndexColumn(
name="date_iso",
Expand All @@ -152,14 +154,17 @@ class ChildProject:
required=True,
filename=True,
unique=True,
dtype="str",
),
IndexColumn(
name="duration",
description="duration of the audio, in milliseconds",
regex=r"([0-9]+)",
),
IndexColumn(
name="session_id", description="identifier of the recording session."
name="session_id",
description="identifier of the recording session.",
dtype="str",
),
IndexColumn(
name="session_offset",
Expand Down Expand Up @@ -207,13 +212,15 @@ class ChildProject:

PROJECT_FOLDERS = ["recordings", "annotations", "metadata", "doc", "scripts"]

def __init__(self, path: str):
def __init__(self, path: str, enforce_dtypes: bool = False):
"""Constructor

:param path: path to the root of the dataset.
:type path: str
"""
self.path = path
self.enforce_dtypes = enforce_dtypes

self.errors = []
self.warnings = []
self.children = None
Expand Down Expand Up @@ -272,7 +279,7 @@ def accumulate_metadata(
if not os.path.exists(md):
continue

table = IndexTable(table, md, columns)
table = IndexTable(table, md, columns, enforce_dtypes=self.enforce_dtypes)
dataframe = table.read()

replaced_columns = (set(df.columns) & set(dataframe.columns)) - {
Expand Down Expand Up @@ -303,11 +310,13 @@ def read(self, verbose=False):
"children",
os.path.join(self.path, "metadata/children.csv"),
self.CHILDREN_COLUMNS,
enforce_dtypes=self.enforce_dtypes
)
self.rt = IndexTable(
"recordings",
os.path.join(self.path, "metadata/recordings.csv"),
self.RECORDINGS_COLUMNS,
enforce_dtypes=self.enforce_dtypes
)

self.children = self.ct.read()
Expand Down
12 changes: 10 additions & 2 deletions ChildProject/tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ def __init__(
datetime=None,
function=None,
choices=None,
dtype=None,
unique=False,
generated=False,
):
Expand All @@ -33,6 +34,7 @@ def __init__(
self.choices = choices
self.unique = unique
self.generated = generated
self.dtype = dtype

def __str__(self):
return "IndexColumn(name = {})".format(self.name)
Expand All @@ -42,11 +44,12 @@ def __repr__(self):


class IndexTable:
def __init__(self, name, path=None, columns=[]):
def __init__(self, name, path=None, columns=[],enforce_dtypes: bool = False):
self.name = name
self.path = path
self.columns = columns
self.df = None
self.enforce_dtypes = enforce_dtypes

def read(self):
pd_flags = {
Expand Down Expand Up @@ -74,7 +77,12 @@ def read(self):
"index_col": False,
}

self.df = pd.read_csv(self.path, **pd_flags)
if self.enforce_dtypes:
dtype = {column.name: column.dtype for column in self.columns if column.dtype}
self.df = pd.read_csv(self.path, dtype=dtype, **pd_flags)
else:
self.df = pd.read_csv(self.path, **pd_flags)

self.df.index = self.df.index + 2
return self.df

Expand Down
14 changes: 14 additions & 0 deletions tests/test_projects.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from ChildProject.projects import ChildProject

def test_enforce_dtypes():
project = ChildProject("examples/valid_raw_data", enforce_dtypes=True)
project.read()

assert project.recordings['child_id'].dtype.kind == 'O'
assert project.children['child_id'].dtype.kind == 'O'

project = ChildProject("examples/valid_raw_data", enforce_dtypes=False)
project.read()

assert project.recordings['child_id'].dtype.kind == 'i'
assert project.children['child_id'].dtype.kind == 'i'
2 changes: 1 addition & 1 deletion tests/truth/aclew_metrics.csv
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
child_id,voc_fem_ph,voc_dur_fem_ph,avg_voc_dur_fem,voc_chi_ph,voc_dur_chi_ph,avg_voc_dur_chi,wc_fem_ph,sc_fem_ph,pc_fem_ph,wc_adu_ph,sc_adu_ph,pc_adu_ph,cry_voc_chi_ph,cry_voc_dur_chi_ph,can_voc_chi_ph,can_voc_dur_chi_ph,avg_can_voc_dur_chi,non_can_voc_chi_ph,non_can_voc_dur_chi_ph,avg_non_can_voc_dur_chi,lp_n,cp_n,lp_dur,cp_dur,duration
1,7200.0,720.0,0.1,18000.0,1800.0,0.1,7200.0,14400.0,28800.0,7200.0,14400.0,28800.0,0,0,9000.0,900.0,0.1,9000.0,900.0,0.1,1.0,0.5,1.0,0.5,4.0
"1",7200.0,720.0,0.1,18000.0,1800.0,0.1,7200.0,14400.0,28800.0,7200.0,14400.0,28800.0,0,0,9000.0,900.0,0.1,9000.0,900.0,0.1,1.0,0.5,1.0,0.5,4.0
24 changes: 12 additions & 12 deletions tests/truth/period_metrics.csv
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
child_id,voc_fem_ph,voc_dur_fem_ph,avg_voc_dur_fem,voc_mal_ph,voc_dur_mal_ph,avg_voc_dur_mal,voc_chi_ph,voc_dur_chi_ph,avg_voc_dur_chi,voc_och_ph,voc_dur_och_ph,avg_voc_dur_och,duration,period
1,720.0,720.0,1.0,0.0,0.0,,0.0,0.0,,0.0,0.0,,7200000,00:00:00
1,720.0,720.0,1.0,0.0,0.0,,0.0,0.0,,0.0,0.0,,7200000,02:00:00
1,720.0,720.0,1.0,0.0,0.0,,0.0,0.0,,0.0,0.0,,7200000,04:00:00
1,720.0000100000007,720.0000100000007,1.0,0.0,0.0,,0.0,0.0,,0.0,0.0,,7199999,06:00:00
1,720.0,720.0,1.0,0.0,0.0,,0.0,0.0,,0.0,0.0,,3600000,08:00:00
1,720.0,720.0,1.0,0.0,0.0,,0.0,0.0,,0.0,0.0,,7200000,10:00:00
1,720.0,720.0,1.0,0.0,0.0,,0.0,0.0,,0.0,0.0,,7200000,12:00:00
1,720.0,720.0,1.0,0.0,0.0,,0.0,0.0,,0.0,0.0,,7200000,14:00:00
1,720.0,720.0,1.0,0.0,0.0,,0.0,0.0,,0.0,0.0,,7200000,16:00:00
1,720.0,720.0,1.0,0.0,0.0,,0.0,0.0,,0.0,0.0,,7200000,18:00:00
1,720.0,720.0,1.0,0.0,0.0,,0.0,0.0,,0.0,0.0,,7200000,20:00:00
1,720.0000100000007,720.0000100000007,1.0,0.0,0.0,,0.0,0.0,,0.0,0.0,,7199999,22:00:00
"1",720.0,720.0,1.0,0.0,0.0,,0.0,0.0,,0.0,0.0,,7200000,00:00:00
"1",720.0,720.0,1.0,0.0,0.0,,0.0,0.0,,0.0,0.0,,7200000,02:00:00
"1",720.0,720.0,1.0,0.0,0.0,,0.0,0.0,,0.0,0.0,,7200000,04:00:00
"1",720.0000100000007,720.0000100000007,1.0,0.0,0.0,,0.0,0.0,,0.0,0.0,,7199999,06:00:00
"1",720.0,720.0,1.0,0.0,0.0,,0.0,0.0,,0.0,0.0,,3600000,08:00:00
"1",720.0,720.0,1.0,0.0,0.0,,0.0,0.0,,0.0,0.0,,7200000,10:00:00
"1",720.0,720.0,1.0,0.0,0.0,,0.0,0.0,,0.0,0.0,,7200000,12:00:00
"1",720.0,720.0,1.0,0.0,0.0,,0.0,0.0,,0.0,0.0,,7200000,14:00:00
"1",720.0,720.0,1.0,0.0,0.0,,0.0,0.0,,0.0,0.0,,7200000,16:00:00
"1",720.0,720.0,1.0,0.0,0.0,,0.0,0.0,,0.0,0.0,,7200000,18:00:00
"1",720.0,720.0,1.0,0.0,0.0,,0.0,0.0,,0.0,0.0,,7200000,20:00:00
"1",720.0000100000007,720.0000100000007,1.0,0.0,0.0,,0.0,0.0,,0.0,0.0,,7199999,22:00:00