From cec14e6d5010c758c6f9f41dbd0ffe4abe40a1c6 Mon Sep 17 00:00:00 2001 From: Chris Kerr Date: Sun, 23 May 2021 21:04:29 +0300 Subject: [PATCH 1/3] Add '%m.%d.%y' date format for .mpr file timestamps Closes #60 --- galvani/BioLogic.py | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/galvani/BioLogic.py b/galvani/BioLogic.py index a3786c0..c91d602 100644 --- a/galvani/BioLogic.py +++ b/galvani/BioLogic.py @@ -416,10 +416,18 @@ def __init__(self, file_or_path): if maybe_log_module: log_module, = maybe_log_module - try: - tm = time.strptime(log_module['date'].decode('ascii'), '%m/%d/%y') - except ValueError: - tm = time.strptime(log_module['date'].decode('ascii'), '%m-%d-%y') + date_string = log_module['date'].decode('ascii') + date_formats = ['%m/%d/%y', '%m-%d-%y', '%m.%d.%y'] + for date_format in date_formats: + try: + tm = time.strptime(date_string, date_format) + except ValueError: + continue + else: + break + else: + raise ValueError(f'Could not parse timestamp {date_string!r}' + f' with any of the formats {date_formats}') self.enddate = date(tm.tm_year, tm.tm_mon, tm.tm_mday) # There is a timestamp at either 465 or 469 bytes From 4ebdc663a9f3d4a4186f540612c028d11fe59822 Mon Sep 17 00:00:00 2001 From: Chris Kerr Date: Fri, 2 Jul 2021 08:05:27 +0300 Subject: [PATCH 2/3] Factor the date parsing code out to a separate function --- galvani/BioLogic.py | 41 ++++++++++++++++++++++------------------- 1 file changed, 22 insertions(+), 19 deletions(-) diff --git a/galvani/BioLogic.py b/galvani/BioLogic.py index c91d602..61586ff 100644 --- a/galvani/BioLogic.py +++ b/galvani/BioLogic.py @@ -244,6 +244,26 @@ def MPTfileCSV(file_or_path): } +def parse_BioLogic_date(date_text): + """Parse a date from one of the various formats used by Bio-Logic files.""" + date_formats = ['%m/%d/%y', '%m-%d-%y', '%m.%d.%y'] + if isinstance(date_text, bytes): + date_string = date_text.decode('ascii') + else: + date_string = date_text + for date_format in date_formats: + try: + tm = time.strptime(date_string, date_format) + except ValueError: + continue + else: + break + else: + raise ValueError(f'Could not parse timestamp {date_string!r}' + f' with any of the formats {date_formats}') + return date(tm.tm_year, tm.tm_mon, tm.tm_mday) + + def VMPdata_dtype_from_colIDs(colIDs): """Get a numpy record type from a list of column ID numbers. @@ -397,12 +417,7 @@ def __init__(self, file_or_path): self.version = int(data_module['version']) self.cols = column_types self.npts = n_data_points - - try: - tm = time.strptime(settings_mod['date'].decode('ascii'), '%m/%d/%y') - except ValueError: - tm = time.strptime(settings_mod['date'].decode('ascii'), '%m-%d-%y') - self.startdate = date(tm.tm_year, tm.tm_mon, tm.tm_mday) + self.startdate = parse_BioLogic_date(settings_mod['date']) if maybe_loop_module: loop_module, = maybe_loop_module @@ -416,19 +431,7 @@ def __init__(self, file_or_path): if maybe_log_module: log_module, = maybe_log_module - date_string = log_module['date'].decode('ascii') - date_formats = ['%m/%d/%y', '%m-%d-%y', '%m.%d.%y'] - for date_format in date_formats: - try: - tm = time.strptime(date_string, date_format) - except ValueError: - continue - else: - break - else: - raise ValueError(f'Could not parse timestamp {date_string!r}' - f' with any of the formats {date_formats}') - self.enddate = date(tm.tm_year, tm.tm_mon, tm.tm_mday) + self.enddate = parse_BioLogic_date(log_module['date']) # There is a timestamp at either 465 or 469 bytes # I can't find any reason why it is one or the other in any From bcd7c5a9b802391e07ec67a533883376f35dacb9 Mon Sep 17 00:00:00 2001 From: Chris Kerr Date: Sat, 3 Jul 2021 16:08:48 +0300 Subject: [PATCH 3/3] Add a test for the parse_BioLogic_date function --- tests/test_BioLogic.py | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/tests/test_BioLogic.py b/tests/test_BioLogic.py index 76e65e8..c4ddca0 100644 --- a/tests/test_BioLogic.py +++ b/tests/test_BioLogic.py @@ -6,7 +6,7 @@ import os.path import re -from datetime import datetime +from datetime import date, datetime import numpy as np from numpy.testing import assert_array_almost_equal, assert_array_equal @@ -77,6 +77,24 @@ def test_colID_to_dtype(colIDs, expected): assert dtype == expected_dtype +@pytest.mark.parametrize('data, expected', [ + ('02/23/17', date(2017, 2, 23)), + ('10-03-05', date(2005, 10, 3)), + ('11.12.20', date(2020, 11, 12)), + (b'01/02/03', date(2003, 1, 2)), + ('13.08.07', ValueError), + ('03-04/05', ValueError), +]) +def test_parse_BioLogic_date(data, expected): + """Test the parse_BioLogic_date function.""" + if isinstance(expected, type) and issubclass(expected, Exception): + with pytest.raises(expected): + BioLogic.parse_BioLogic_date(data) + return + result = BioLogic.parse_BioLogic_date(data) + assert result == expected + + @pytest.mark.parametrize('filename, startdate, enddate', [ ('bio_logic1.mpr', '2011-10-29', '2011-10-31'), ('bio_logic2.mpr', '2012-09-27', '2012-09-27'),