Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reshape data array inside data section reader function #460

Merged
merged 2 commits into from Apr 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 6 additions & 1 deletion lasio/las.py
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
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