re-implement stack_curves method
@@ -705,42 +705,43 @@ class LASFile(object):
]
self.set_data(df_values, **kwargs)
- def stack_curves(self, curve_list=None, stub=None, sort_curves=True):
+ def stack_curves(self, mnemonic, sort_curves=True):
"""Stack multi-channel curve data to a numpy 2D ndarray. Provide a
- list of curves or a stub name (prefix shared by all curves that will
- be stacked). If `stub` and `curve_list` are both supplied, `stub`
- is used.
+ stub name (prefix shared by all curves that will be stacked) or a
+ list of curve mnemonic strings.
Keyword Arguments:
- stub (str): Supply the first several characters of the channel
- set to be stacked.
- curve_list (list): Or a list of the curves to be stacked
+ mnemonic (str or list): Supply the first several characters of
+ the channel set to be stacked. Alternatively, supply a list
+ of the curve names (mnemonics strings) to be stacked.
sort_curves (bool): Natural sort curves based on mnemonic prior
to stacking.
Returns:
2-D numpy array
"""
- curves = self.curves.keys()
- if stub:
- channels = [i for i in curves if i.startswith(stub)]
- elif curve_list:
- channels = curve_list
- if sort_curves:
- channels.sort(key=self._sort_channels)
- if channels:
- indices = [curves.index(i) for i in channels]
- if indices:
- return self.data[:, indices]
-
- def _str_to_int(self, mnemonic):
- if mnemonic.isdigit():
- return int(mnemonic)
+ if (not mnemonic) or (not all([i for i in mnemonic])):
+ raise ValueError('`mnemonic` must not contain empty element')
+
+ keys = self.curves.keys()
+ if isinstance(mnemonic, str):
+ channels = [i for i in keys if i.startswith(mnemonic)] or [mnemonic]
+ elif isinstance(mnemonic, list):
+ channels = mnemonic
else:
- return mnemonic
+ raise TypeError('`mnemonic` argument must be str or list')
+
+ if not set(keys).issuperset(set(channels)):
+ missing = ', '.join(set(channels).difference(set(keys)))
+ raise KeyError('{} not found in LAS curves.'.format(missing))
- def _sort_channels(self, mnemonic):
- return [self._str_to_int(i) for i in re.split(r'(\d+)', mnemonic)]
+ if sort_curves:
+ nat_sort = lambda x: \
+ [int(i) if i.isdigit() else i for i in re.split(r'(\d+)', x)]
+ channels.sort(key=nat_sort)
+
+ indices = [keys.index(i) for i in channels]
+ return self.data[:, indices]
@property
def index(self):
