From bc743e73d2350fe3417584eb9a3f9c14cac64d89 Mon Sep 17 00:00:00 2001 From: Kent Inverarity Date: Sat, 24 Apr 2021 14:25:38 +0930 Subject: [PATCH 1/2] Reshape data array inside data section reader function This adds two things: (1) the argument `n_columns` to the function signature for read_data_section_iterative (i.e. the normal data section reader) function. (2) code to (attempt) to re-shape the numpy array read by the data section reader into a 2D array. If the file is wrapped and therefore the number of columns is unclear (i.e. inspect_data_section() returns -1), then the data section reader code does not currently attempt to reshape the file. The reason for this is so that the data section reader function can be further modified to return a 2D numpy record array with different dtypes per column. --- lasio/las.py | 7 ++++++- lasio/reader.py | 25 +++++++++++++++++++++++-- 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/lasio/las.py b/lasio/las.py index ab7cab5..caa6963 100644 --- a/lasio/las.py +++ b/lasio/las.py @@ -270,6 +270,7 @@ def read( regexp_subs, value_null_subs, ignore_comments=ignore_data_comments, + n_columns=n_columns, ) except KeyboardInterrupt: raise @@ -278,7 +279,11 @@ def read( traceback.format_exc()[:-1] + " in data section beginning line {}".format(i + 1) ) - logger.debug("Read ndarray {arrshape}".format(arrshape=arr.shape)) + logger.debug( + "Read ndarray {arrshape} from data section".format( + arrshape=arr.shape + ) + ) # This is so we can check data size and use self.set_data(data, truncate=False) # in cases of data.size is zero. diff --git a/lasio/reader.py b/lasio/reader.py index a4b9abc..0973a70 100644 --- a/lasio/reader.py +++ b/lasio/reader.py @@ -367,7 +367,7 @@ def inspect_data_section(file_obj, line_nos, regexp_subs, ignore_comments="#"): def read_data_section_iterative( - file_obj, line_nos, regexp_subs, value_null_subs, ignore_comments + file_obj, line_nos, regexp_subs, value_null_subs, ignore_comments, n_columns ): """Read data section into memory. @@ -380,12 +380,14 @@ def read_data_section_iterative( value_null_subs (list): list of numerical values to be replaced by numpy.nan values. ignore_comments (str): lines beginning with this character will be ignored - + n_columns (int, None): expected number of columns, or None/-1 if unknown Returns: A 1-D numpy ndarray. """ + if n_columns == -1: + n_columns = None title = file_obj.readline() @@ -412,11 +414,30 @@ def items(f, start_line_no, end_line_no): if line_no == end_line_no: break + logger.debug("Reading complete data section...") array = np.array( [i for i in items(file_obj, start_line_no=line_nos[0], end_line_no=line_nos[1])] ) for value in value_null_subs: array[array == value] = np.nan + logger.debug("Successfully read {} items in data section".format(len(array))) + + if not n_columns is None: + logger.debug( + "Attempting to re-shape into 2D array with {} columns".format(n_columns) + ) + try: + array = np.reshape(array, (-1, n_columns)) + except ValueError as exception: + error_message = "Cannot reshape ~A data size {0} into {1} columns".format( + array.shape, n_columns + ) + if sys.version_info.major < 3: + exception.message = error_message + raise exception + else: + raise ValueError(error_message).with_traceback(exception.__traceback__) + return array From aa0d5aa2913ad0fcafb8edd4ecc4779588d9275c Mon Sep 17 00:00:00 2001 From: Kent Inverarity Date: Sat, 24 Apr 2021 14:32:24 +0930 Subject: [PATCH 2/2] Add missing 'sys' import --- lasio/reader.py | 1 + 1 file changed, 1 insertion(+) diff --git a/lasio/reader.py b/lasio/reader.py index 0973a70..14f5287 100644 --- a/lasio/reader.py +++ b/lasio/reader.py @@ -3,6 +3,7 @@ import logging import os import re +import sys import traceback import urllib.request