From 25d645baab18eb69402c6e1c8ada44e4733b85fe Mon Sep 17 00:00:00 2001 From: "Bernhard F.W. Gschaider" Date: Thu, 11 Oct 2018 19:40:48 +0200 Subject: [PATCH 1/3] Improve error message for non-2D data arrays --- lasio/las.py | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/lasio/las.py b/lasio/las.py index 22286bd1..d1572a0c 100644 --- a/lasio/las.py +++ b/lasio/las.py @@ -7,6 +7,7 @@ import logging import os import re +import sys import textwrap import traceback @@ -263,8 +264,21 @@ def add_section(pattern, name, **sect_kws): if wrap == "NO": if s["ncols"] > n_curves: n_arr_cols = s["ncols"] - data = np.reshape(arr, (-1, n_arr_cols)) - + try: + data = np.reshape(arr, (-1, n_arr_cols)) + except ValueError as e: + err_msg = ( + "cannot reshape ~A array of " + "size {arr_shape} into " + "{n_arr_cols} columns".format( + arr_shape=arr.shape, n_arr_cols=n_arr_cols + ) + ) + if sys.version_info.major < 3: + e.message = err_msg + raise e + else: + raise ValueError(err_msg) from e self.set_data(data, truncate=False) drop.append(s["title"]) for key in drop: @@ -597,12 +611,12 @@ def header(self): return self.sections def df(self): - '''Return data as a :class:`pandas.DataFrame` structure. + """Return data as a :class:`pandas.DataFrame` structure. The first Curve of the LASFile object is used as the pandas DataFrame's index. - ''' + """ import pandas as pd from pandas.api.types import is_object_dtype From d90036c5a2aad826080590575316507f7b9bbe26 Mon Sep 17 00:00:00 2001 From: Kent Inverarity Date: Sun, 20 Oct 2019 16:42:45 +1030 Subject: [PATCH 2/3] Add test of misformatted ~A arrays that should raise a reshape exception --- tests/examples/sample_lastcolblanked.las | 46 ++++++++++++++++++++++++ tests/test_read.py | 5 +++ 2 files changed, 51 insertions(+) create mode 100644 tests/examples/sample_lastcolblanked.las diff --git a/tests/examples/sample_lastcolblanked.las b/tests/examples/sample_lastcolblanked.las new file mode 100644 index 00000000..d4b53004 --- /dev/null +++ b/tests/examples/sample_lastcolblanked.las @@ -0,0 +1,46 @@ +~VERSION INFORMATION + VERS. 1.2: CWLS LOG ASCII STANDARD -VERSION 1.2 + WRAP. NO: ONE LINE PER DEPTH STEP +~WELL INFORMATION BLOCK +#MNEM.UNIT DATA TYPE INFORMATION +#--------- ------------- ------------------------------ + STRT.M 1670.000000: + STOP.M 1660.000000: + STEP.M -0.1250: + NULL. -999.2500: + COMP. COMPANY: # ANY OIL COMPANY LTD. + WELL. WELL: ANY ET AL OIL WELL #12 + FLD . FIELD: EDAM + LOC . LOCATION: A9-16-49-20W3M + PROV. PROVINCE: SASKATCHEWAN + SRVC. SERVICE COMPANY: ANY LOGGING COMPANY LTD. + DATE. LOG DATE: 25-DEC-1988 + UWI . UNIQUE WELL ID: 100091604920W300 +~CURVE INFORMATION +#MNEM.UNIT API CODE CURVE DESCRIPTION +#--------- ------------- ------------------------------ + DEPT.M : 1 DEPTH + DT .US/M : 2 SONIC TRANSIT TIME + RHOB.K/M3 : 3 BULK DENSITY + NPHI.V/V : 4 NEUTRON POROSITY + SFLU.OHMM : 5 RXO RESISTIVITY + SFLA.OHMM : 6 SHALLOW RESISTIVITY + ILM .OHMM : 7 MEDIUM RESISTIVITY + ILD .OHMM : 8 DEEP RESISTIVITY +~PARAMETER INFORMATION +#MNEM.UNIT VALUE DESCRIPTION +#--------- ------------- ------------------------------ + BHT .DEGC 35.5000: BOTTOM HOLE TEMPERATURE + BS .MM 200.0000: BIT SIZE + FD .K/M3 1000.0000: FLUID DENSITY + MATR. 0.0000: NEUTRON MATRIX(0=LIME,1=SAND,2=DOLO) + MDEN. 2710.0000: LOGGING MATRIX DENSITY + RMF .OHMM 0.2160: MUD FILTRATE RESISTIVITY + DFD .K/M3 1525.0000: DRILL FLUID DENSITY +~Other + Note: The logging tools became stuck at 625 meters causing the data + between 625 meters and 615 meters to be invalid. +~A DEPTH DT RHOB NPHI SFLU SFLA ILM ILD +1670.000 123.450 2550.000 0.450 123.450 123.450 110.200 105.600 +1669.875 123.450 2550.000 0.450 123.450 123.450 **************** +1669.750 123.450 2550.000 0.450 123.450 123.450 110.200 105.600 diff --git a/tests/test_read.py b/tests/test_read.py index 7af49600..24f03e3f 100644 --- a/tests/test_read.py +++ b/tests/test_read.py @@ -372,3 +372,8 @@ def test_index_unit_equals_f(): def test_index_unit_equals_m(): las = lasio.read(egfn("autodepthindex_M.las"), index_unit="m") assert (las.depth_ft[1:] != las.index[1:]).all() + + +def test_read_incorrect_shape(): + with pytest.raises(ValueError): + lasio.read(egfn("sample_lastcolblanked.las")) From 1c38c25ffae13c66c404c05e155a63fbc1dcd435 Mon Sep 17 00:00:00 2001 From: Kent Inverarity Date: Sun, 20 Oct 2019 16:47:45 +1030 Subject: [PATCH 3/3] Add Py3 raising code to avoid Py2 syntax errors --- lasio/las.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lasio/las.py b/lasio/las.py index d1572a0c..99e9f9e3 100644 --- a/lasio/las.py +++ b/lasio/las.py @@ -278,7 +278,7 @@ def add_section(pattern, name, **sect_kws): e.message = err_msg raise e else: - raise ValueError(err_msg) from e + raise ValueError(err_msg).with_traceback(e.__traceback__) self.set_data(data, truncate=False) drop.append(s["title"]) for key in drop: