Skip to content

Commit

Permalink
Merge pull request #460 from kinverarity1/reshape-in-data-reader
Browse files Browse the repository at this point in the history
Reshape data array inside data section reader function
  • Loading branch information
kinverarity1 authored Apr 24, 2021
2 parents 8e1c29c + aa0d5aa commit 6c93cb5
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
7 changes: 6 additions & 1 deletion lasio/las.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,7 @@ def read(
regexp_subs,
value_null_subs,
ignore_comments=ignore_data_comments,
n_columns=n_columns,
)
except KeyboardInterrupt:
raise
Expand All @@ -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.
Expand Down
26 changes: 24 additions & 2 deletions lasio/reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import logging
import os
import re
import sys
import traceback
import urllib.request

Expand Down Expand Up @@ -367,7 +368,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.
Expand All @@ -380,12 +381,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()

Expand All @@ -412,11 +415,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


Expand Down

0 comments on commit 6c93cb5

Please sign in to comment.