Skip to content

Commit 37c7e8a

Browse files
fix: BMG Mars: wrap keyError when index out of range (#1005)
Co-authored-by: Nathan Stender <nathan.stender@benchling.com>
1 parent ab07280 commit 37c7e8a

3 files changed

Lines changed: 50 additions & 0 deletions

File tree

src/allotropy/parsers/bmg_mars/bmg_mars_reader.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import pandas as pd
44

5+
from allotropy.exceptions import AllotropeConversionError
56
from allotropy.named_file_contents import NamedFileContents
67
from allotropy.parsers.lines_reader import CsvReader, read_to_lines
78
from allotropy.parsers.utils.pandas import read_csv, SeriesData
@@ -27,6 +28,11 @@ def __init__(self, named_file_contents: NamedFileContents) -> None:
2728
axis="index"
2829
)
2930
new = df["value"].str.split(": ", expand=True, n=1)
31+
32+
# Handle the case where no ": " delimiter is found, resulting in a DataFrame with only one column
33+
if new.shape[1] < 2:
34+
msg = "Unable to parse header data: no key-value pairs found with expected format."
35+
raise AllotropeConversionError(msg)
3036
self.header = SeriesData(pd.Series(new[1].values, index=new[0].str.upper()))
3137

3238
# Read in the rest of the file as a dataframe
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
User = USER,Path = C:\Program Files (x86)\BMG\PHERAstar\User\Data,Test run no. = 1,,,,,,,,,,,,,,,,,,,,,,
2+
Test name = 472 QC 384 FI,Date = 03/03/2016,Time = 13:25:45,,,,,,,,,,,,,,,,,,,,,,
3+
,,,,,,,,,,,,,,,,,,,,,,,,
4+
ID1 = 472-0016,,,,,,,,,,,,,,,,,,,,,,,,
5+
Fluorescence (FI),,,,,,,,,,,,,,,,,,,,,,,,
6+
,,,,,,,,,,,,,,,,,,,,,,,,
7+
Raw Data (485/520),,,,,,,,,,,,,,,,,,,,,,,,
8+
,,,,,,,,,,,,,,,,,,,,,,,,
9+
,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24
10+
A,23864,23796,23934,,,,,,,,,231438,228111,,,,,,,,,24069,24096,24098
11+
B,23774,23887,23810,,,,,,,,,229790,230207,,,,,,,,,24121,24223,24106
12+
C,23879,23687,23567,,,,,,,,,2682,2677,,,,,,,,,24171,24104,23987
13+
D,,,,,,,,,,,,2679,2661,,,,,,,,,,,
14+
E,,,,,,,,,,,,371,374,,,,,,,,,,,
15+
F,,,,,,,,,,,,372,375,,,,,,,,,,,
16+
G,,,,,,,,,,,,187,183,,,,,,,,,,,
17+
H,,,,,,,,,,,,187,188,,,,,,,,,,,
18+
I,,,,,,,,,,,,123,118,,,,,,,,,,,
19+
J,,,,,,,,,,,,120,120,,,,,,,,,,,
20+
K,,,,,,,,,,,,120,124,,,,,,,,,,,
21+
L,,,,,,,,,,,,122,121,,,,,,,,,,,
22+
M,,,,,,,,,,,,119,121,,,,,,,,,,,
23+
N,23478,23579,23326,,,,,,,,,118,120,,,,,,,,,23650,23767,23705
24+
O,23730,23719,23988,,,,,,,,,122,121,,,,,,,,,23904,23959,24076
25+
P,23488,23724,23895,,,,,,,,,120,120,,,,,,,,,23715,23502,23949
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,25 @@
1+
import pytest
2+
3+
from allotropy.constants import CHARDET_ENCODING
4+
from allotropy.exceptions import AllotropeConversionError
15
from allotropy.parser_factory import Vendor
6+
from allotropy.testing.utils import from_file, get_testdata_dir
27
from tests.to_allotrope_test import ParserTest
38

9+
TESTDATA = get_testdata_dir(__file__)
10+
411

512
class TestParser(ParserTest):
613
VENDOR = Vendor.BMG_MARS
14+
15+
16+
def test_to_allotrope_unsupported_kinetic_file() -> None:
17+
with pytest.raises(
18+
AllotropeConversionError,
19+
match="Unable to parse header data: no key-value pairs found with expected format.",
20+
):
21+
from_file(
22+
f"{TESTDATA}/errors/file_with_no_delimiter_malformed.csv",
23+
TestParser.VENDOR,
24+
encoding=CHARDET_ENCODING,
25+
)

0 commit comments

Comments
 (0)