[segyio] 137/376: Python 3.5 support
Jørgen Kvalsvik
jokva-guest at moszumanska.debian.org
Wed Sep 20 08:04:21 UTC 2017
This is an automated email from the git hooks/post-receive script.
jokva-guest pushed a commit to branch debian
in repository segyio.
commit 033f3f7625a69988f5ca4b56d04f44903ec45688
Author: jeanpaul <jpb at prador.net>
Date: Sun Nov 27 12:43:59 2016 +0100
Python 3.5 support
---
.travis.yml | 29 ++++++++++----
appveyor.yml | 8 ++--
cmake/python.cmake | 4 +-
examples/about.py | 20 +++++-----
examples/make-file.py | 10 ++---
examples/make-ps-file.py | 10 ++---
examples/scan_min_max.py | 4 +-
python/segyio/_field.py | 2 +-
python/segyio/_header.py | 11 +++--
python/segyio/_line.py | 16 +++++---
python/segyio/_segyio.c | 44 ++++++++++++++++++--
python/segyio/create.py | 102 +++++++++++++++++++----------------------------
python/segyio/open.py | 2 +-
python/segyio/segy.py | 13 ++++--
tests/test_segy.py | 57 ++++++++++++++------------
tests/test_segyio_c.py | 84 +++++++++++++++++++-------------------
tests/test_utils.c | 2 -
17 files changed, 238 insertions(+), 180 deletions(-)
diff --git a/.travis.yml b/.travis.yml
index 64b69ae..b60a216 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -1,8 +1,10 @@
language: generic
env:
- - BUILD_TYPE=RELEASE
- - BUILD_TYPE=DEBUG
+ - BUILD_TYPE=RELEASE;PYTHON=2.7
+ - BUILD_TYPE=RELEASE;PYTHON=3.5
+ - BUILD_TYPE=DEBUG;PYTHON=2.7
+ - BUILD_TYPE=DEBUG;PYTHON=3.5
os:
- linux
@@ -21,13 +23,8 @@ matrix:
addons:
apt:
- sources:
- - george-edison55-precise-backports
packages:
- - cmake
- - cmake-data
- valgrind
- - python-numpy
before_script:
# Valgrind is experimental(ish) on MacOS with false positives on among others printf
@@ -35,6 +32,24 @@ before_script:
# brew update;
# brew install --HEAD valgrind;
# fi
+ - if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then
+ export CONDA_OS=MacOSX;
+ else
+ export CONDA_OS=Linux;
+ fi
+ - if [[ "$PYTHON" == "2.7" ]]; then
+ wget https://repo.continuum.io/miniconda/Miniconda2-latest-${CONDA_OS}-x86_64.sh -O miniconda.sh;
+ else
+ wget https://repo.continuum.io/miniconda/Miniconda3-latest-${CONDA_OS}-x86_64.sh -O miniconda.sh;
+ fi
+ - bash miniconda.sh -b -p $HOME/miniconda
+ - export PATH="$HOME/miniconda/bin:$PATH"
+ - hash -r
+ - conda config --set always_yes yes --set changeps1 no
+ - conda update -q conda
+ - conda info -a
+
+ - conda install numpy cmake
- cmake --version
- mkdir build
diff --git a/appveyor.yml b/appveyor.yml
index a51c0d7..2e103a7 100644
--- a/appveyor.yml
+++ b/appveyor.yml
@@ -16,11 +16,13 @@ platform:
environment:
matrix:
- PYTHON: C:\Python27
+ - PYTHON: C:\Python35
build_script:
- - python -m pip install --user numpy
+ - "%PYTHON%\\python -m pip install --user numpy"
+ - cmake --version
- mkdir build
- pushd build
- - cmake C:\projects\SegyIO -DBUILD_MEX=OFF -DBUILD_PYTHON=ON -DCMAKE_BUILD_TYPE=%configuration%
+ - cmake C:\projects\SegyIO -DBUILD_MEX=OFF -DBUILD_PYTHON=ON -DCMAKE_BUILD_TYPE=%configuration% -DPYTHON_EXECUTABLE="%PYTHON%\\python.exe"
- cmake --build . --config %configuration%
- - ctest -C %configuration% --output-on-failure
+ - ctest -C %configuration% --output-on-failure -V
diff --git a/cmake/python.cmake b/cmake/python.cmake
index e435a1a..1cd497b 100644
--- a/cmake/python.cmake
+++ b/cmake/python.cmake
@@ -31,7 +31,7 @@ function(add_python_test TESTNAME PYTHON_TEST_FILE)
add_test(NAME ${TESTNAME}
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/tests
- COMMAND python test_runner.py ${PYTHON_TEST_FILE}
+ COMMAND ${PYTHON_EXECUTABLE} test_runner.py ${PYTHON_TEST_FILE}
)
to_path_list(pythonpath "${CMAKE_BINARY_DIR}/python" "$ENV{PYTHONPATH}")
@@ -43,7 +43,7 @@ function(add_python_example TESTNAME PYTHON_TEST_FILE)
add_test(NAME ${TESTNAME}
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/examples
- COMMAND python ${PYTHON_TEST_FILE} ${ARGN}
+ COMMAND ${PYTHON_EXECUTABLE} ${PYTHON_TEST_FILE} ${ARGN}
)
to_path_list(pythonpath "${CMAKE_BINARY_DIR}/python" "$ENV{PYTHONPATH}")
set_tests_properties(${TESTNAME} PROPERTIES ENVIRONMENT "PYTHONPATH=${pythonpath}")
diff --git a/examples/about.py b/examples/about.py
index 91a7649..aea09d4 100644
--- a/examples/about.py
+++ b/examples/about.py
@@ -2,34 +2,36 @@ import sys
from segyio import TraceField
import segyio
+
def list_byte_offset_names():
print("Available offsets and their corresponding byte value:")
for x in TraceField.enums():
print(" %s: %d" % (str(x), x))
+
if __name__ == '__main__':
- if len( sys.argv ) < 4:
+ if len(sys.argv) < 4:
list_byte_offset_names()
- sys.exit( "Usage: about.py [file] [inline] [crossline]" )
+ sys.exit("Usage: about.py [file] [inline] [crossline]")
# we need a way to convert from run-time inline/crossline argument (as
# text) to the internally used TraceField enum. Make a string -> TraceField
# map and look up into that. this dictionary comprehension creates that
- fieldmap = { str( x ).lower(): x for x in TraceField.enums() }
+ fieldmap = {str(x).lower(): x for x in TraceField.enums()}
- filename = sys.argv[ 1 ]
- inline_name, crossline_name = sys.argv[ 2 ].lower(), sys.argv[ 3 ].lower()
+ filename = sys.argv[1]
+ inline_name, crossline_name = sys.argv[2].lower(), sys.argv[3].lower()
# exit if inline or crossline are unknown
if inline_name not in fieldmap:
list_byte_offset_names()
- sys.exit( "Unknown inline field '%s'" % sys.argv[ 2 ] )
+ sys.exit("Unknown inline field '%s'" % sys.argv[2])
if crossline_name not in fieldmap:
list_byte_offset_names()
- sys.exit( "Unknown crossline field '%s'" % sys.argv[ 3 ] )
+ sys.exit("Unknown crossline field '%s'" % sys.argv[3])
- inline, crossline = fieldmap[ inline_name ], fieldmap[ crossline_name ]
+ inline, crossline = fieldmap[inline_name], fieldmap[crossline_name]
with segyio.open(filename, "r", inline, crossline) as f:
print("About '%s':" % filename)
@@ -38,7 +40,7 @@ if __name__ == '__main__':
print("ilines: %s" % ", ".join(map(str, f.ilines)))
print("xlines: %s" % ", ".join(map(str, f.xlines)))
- print "+------+"
+ print("+------+")
with segyio.open(filename, "r", crossline, inline) as f:
# with swapped inline/crossline
diff --git a/examples/make-file.py b/examples/make-file.py
index d76e10b..8fb76cd 100644
--- a/examples/make-file.py
+++ b/examples/make-file.py
@@ -1,7 +1,7 @@
import sys
import numpy as np
import segyio
-from itertools import izip as izip
+
def main():
if len(sys.argv) < 7:
@@ -10,10 +10,10 @@ def main():
spec = segyio.spec()
filename = sys.argv[1]
-# to create a file from nothing, we need to tell segyio about the structure of
-# the file, i.e. its inline numbers, crossline numbers, etc. You can also add
-# more structural information, but offsets etc. have sensible defautls. This is
-# the absolute minimal specification for a N-by-M volume
+ # to create a file from nothing, we need to tell segyio about the structure of
+ # the file, i.e. its inline numbers, crossline numbers, etc. You can also add
+ # more structural information, but offsets etc. have sensible defautls. This is
+ # the absolute minimal specification for a N-by-M volume
spec.sorting = 2
spec.format = 1
spec.samples = int(sys.argv[2])
diff --git a/examples/make-ps-file.py b/examples/make-ps-file.py
index 21ff9d5..cbfab05 100644
--- a/examples/make-ps-file.py
+++ b/examples/make-ps-file.py
@@ -1,7 +1,7 @@
import sys
import numpy as np
import segyio
-from itertools import izip as izip
+
def main():
if len(sys.argv) < 9:
@@ -14,10 +14,10 @@ def main():
spec = segyio.spec()
filename = sys.argv[1]
-# to create a file from nothing, we need to tell segyio about the structure of
-# the file, i.e. its inline numbers, crossline numbers, etc. You can also add
-# more structural information, This is the absolute minimal specification for a
-# N-by-M volume with K offsets volume
+ # to create a file from nothing, we need to tell segyio about the structure of
+ # the file, i.e. its inline numbers, crossline numbers, etc. You can also add
+ # more structural information, This is the absolute minimal specification for a
+ # N-by-M volume with K offsets volume
spec.sorting = 2
spec.format = 1
spec.samples = int(sys.argv[2])
diff --git a/examples/scan_min_max.py b/examples/scan_min_max.py
index f8654a3..7172f0f 100644
--- a/examples/scan_min_max.py
+++ b/examples/scan_min_max.py
@@ -24,8 +24,8 @@ def main():
if np.isfinite(local_max):
max_value = max(local_max, max_value)
- print "min: {}".format(min_value)
- print "max: {}".format(max_value)
+ print("min: {}".format(min_value))
+ print("max: {}".format(max_value))
if __name__ == '__main__':
main()
diff --git a/python/segyio/_field.py b/python/segyio/_field.py
index b6a0f01..d3bca66 100644
--- a/python/segyio/_field.py
+++ b/python/segyio/_field.py
@@ -24,7 +24,7 @@ class Field:
# plain, unstructed output is expected, but header[f1,f2,f3]
# yields a dict
if len(d) == 1:
- return d.values()[0]
+ return d.popitem()[1]
return d
diff --git a/python/segyio/_header.py b/python/segyio/_header.py
index 7b7d467..be996a7 100644
--- a/python/segyio/_header.py
+++ b/python/segyio/_header.py
@@ -4,6 +4,11 @@ import segyio
from segyio._line import Line
from segyio._field import Field
+try:
+ from itertools import izip as zip
+except ImportError: # will be 3.x series
+ pass
+
class Header(object):
def __init__(self, segy):
@@ -65,7 +70,7 @@ class Header(object):
if isinstance(val, Field) or isinstance(val, dict):
val = itertools.repeat(val)
- for i, x in itertools.izip(range(start, stop, stride), val):
+ for i, x in zip(range(start, stop, stride), val):
self[i] = x
@property
@@ -91,7 +96,7 @@ class Header(object):
than that of the file being written to the surplus data will be
ignored. Uses same rules for writing as `f.iline[i] = x`.
"""
- for i, src in itertools.izip(self.segy.ilines, value):
+ for i, src in zip(self.segy.ilines, value):
self.iline[i] = src
@property
@@ -118,5 +123,5 @@ class Header(object):
ignored. Uses same rules for writing as `f.xline[i] = x`.
"""
- for i, src in itertools.izip(self.segy.xlines, value):
+ for i, src in zip(self.segy.xlines, value):
self.xline[i] = src
diff --git a/python/segyio/_line.py b/python/segyio/_line.py
index 71adf96..8223baf 100644
--- a/python/segyio/_line.py
+++ b/python/segyio/_line.py
@@ -1,6 +1,11 @@
import itertools
-import segyio
+import segyio._segyio as _segyio
+
+try:
+ from itertools import izip as zip
+except ImportError: # will be 3.x series
+ pass
class Line:
@@ -54,7 +59,7 @@ class Line:
except TypeError:
raise TypeError("Must be int or slice")
- trace0 = segyio._segyio.fread_trace0(lineno, len(self.other_lines), self.stride, len(offs), self.lines, self.name)
+ trace0 = _segyio.fread_trace0(lineno, len(self.other_lines), self.stride, len(offs), self.lines, self.name)
return offset + trace0
def _indices(self, lineno, offset):
@@ -94,7 +99,7 @@ class Line:
start = source[0] if increasing else source[-1]
if stop is None:
- stop = source[-1]+1 if increasing else source[0]-1
+ stop = source[-1] + 1 if increasing else source[0] - 1
return slice(start, stop, step)
@@ -103,14 +108,13 @@ class Line:
t0 = self._index(lineno, offset)
return self.readfn(t0, self.len, self.stride, buf)
-
def _get_iter(self, lineno, off, buf):
""" :rtype: collections.Iterable[numpy.ndarray]"""
for line, offset in itertools.product(*self._indices(lineno, off)):
yield self._get(line, offset, buf)
- def __getitem__(self, lineno, offset = None):
+ def __getitem__(self, lineno, offset=None):
""" :rtype: numpy.ndarray|collections.Iterable[numpy.ndarray]"""
buf = self.buffn()
@@ -131,7 +135,7 @@ class Line:
lines, offsets = self._indices(lineno, offset)
indices = itertools.product(*self._indices(lineno, offset))
- for (line, offset), x in itertools.izip(indices, val):
+ for (line, offset), x in zip(indices, val):
t0 = self._index(line, offset)
self.writefn(t0, self.len, self.stride, x)
diff --git a/python/segyio/_segyio.c b/python/segyio/_segyio.c
index 10a676a..f7062ea 100644
--- a/python/segyio/_segyio.c
+++ b/python/segyio/_segyio.c
@@ -2,14 +2,37 @@
# define _CRT_NOFORCE_MAINFEST 1
# undef _DEBUG
# include <Python.h>
+# include <bytesobject.h>
# define _DEBUG 1
#else
# include <Python.h>
+# include <bytesobject.h>
#endif
#include "segyio/segy.h"
#include <assert.h>
#include <string.h>
+#if PY_MAJOR_VERSION >= 3
+#define IS_PY3K
+#endif
+
+static bool integer_check(PyObject* integer) {
+#ifdef IS_PY3K
+ return PyLong_Check(integer);
+#else
+ return PyInt_Check(integer);
+#endif
+}
+
+static Py_ssize_t convert_integer(PyObject* integer) {
+#ifdef IS_PY3K
+ return PyLong_AsSsize_t(integer);
+#else
+ return PyInt_AsSsize_t(integer);
+#endif
+}
+
+
// --------------- FILE Handling ------------
static segy_file *get_FILE_pointer_from_capsule(PyObject *capsule) {
if (!PyCapsule_IsValid(capsule, "segy_file*")) {
@@ -230,7 +253,7 @@ static PyObject *py_read_texthdr(PyObject *self, PyObject *args) {
return PyErr_Format(PyExc_Exception, "Could not read text header: %s", strerror(errno));
}
- PyObject *result = Py_BuildValue("s", buffer);
+ PyObject *result = PyBytes_FromStringAndSize(buffer, SEGY_TEXT_HEADER_SIZE);
free(buffer);
return result;
}
@@ -765,7 +788,7 @@ static PyObject *py_read_trace(PyObject *self, PyObject *args) {
return NULL;
}
- if( !PyInt_Check( trace_no ) && !PySlice_Check( trace_no ) ) {
+ if( !integer_check( trace_no ) && !PySlice_Check( trace_no ) ) {
PyErr_SetString(PyExc_TypeError, "Trace number must be int or slice." );
return NULL;
}
@@ -787,7 +810,7 @@ static PyObject *py_read_trace(PyObject *self, PyObject *args) {
}
else {
- start = PyInt_AsSsize_t( trace_no );
+ start = convert_integer( trace_no );
if( start < 0 ) start += trace_count;
step = 1;
stop = start + step;
@@ -952,7 +975,22 @@ static PyMethodDef SegyMethods[] = {
/* module initialization */
+#ifdef IS_PY3K
+static struct PyModuleDef segyio_module = {
+ PyModuleDef_HEAD_INIT,
+ "_segyio", /* name of module */
+ NULL, /* module documentation, may be NULL */
+ -1, /* size of per-interpreter state of the module, or -1 if the module keeps state in global variables. */
+ SegyMethods
+};
+
+PyMODINIT_FUNC
+PyInit__segyio(void) {
+ return PyModule_Create(&segyio_module);
+}
+#else
PyMODINIT_FUNC
init_segyio(void) {
(void) Py_InitModule("_segyio", SegyMethods);
}
+#endif
diff --git a/python/segyio/create.py b/python/segyio/create.py
index 8f07ef8..d3fadfb 100644
--- a/python/segyio/create.py
+++ b/python/segyio/create.py
@@ -1,50 +1,32 @@
import datetime
import numpy
import segyio
+import segyio._segyio as _segyio
+
def default_text_header(iline, xline, offset):
- return ''.join([
- "C 1 DATE: %s ",
- "C 2 AN INCREASE IN AMPLITUDE EQUALS AN INCREASE IN ACOUSTIC IMPEDANCE ",
- "C 3 Written by libsegyio (python) ",
- "C 4 ",
- "C 5 ",
- "C 6 ",
- "C 7 ",
- "C 8 ",
- "C 9 ",
- "C10 ",
- "C11 TRACE HEADER POSITION: ",
- "C12 INLINE BYTES %03d-%03d | OFFSET BYTES %03d-%03d ",
- "C13 CROSSLINE BYTES %03d-%03d | ",
- "C14 ",
- "C15 END EBCDIC HEADER ",
- "C16 ",
- "C17 ",
- "C18 ",
- "C19 ",
- "C20 ",
- "C21 ",
- "C22 ",
- "C23 ",
- "C24 ",
- "C25 ",
- "C26 ",
- "C27 ",
- "C28 ",
- "C29 ",
- "C30 ",
- "C31 ",
- "C32 ",
- "C33 ",
- "C34 ",
- "C35 ",
- "C36 ",
- "C37 ",
- "C38 ",
- "C39 ",
- "C40 \x80"]) \
- % (datetime.date.today(), iline, iline + 4, int(offset), int(offset) + 4, xline, xline + 4)
+ lines = {
+ 1: "DATE %s" % datetime.date.today().isoformat(),
+ 2: "AN INCREASE IN AMPLITUDE EQUALS AN INCREASE IN ACOUSTIC IMPEDANCE",
+ 3: "Written by libsegyio (python)",
+ 11: "TRACE HEADER POSITION:",
+ 12: " INLINE BYTES %03d-%03d | OFFSET BYTES %03d-%03d" % (iline, iline + 4, int(offset), int(offset) + 4),
+ 13: " CROSSLINE BYTES %03d-%03d |" % (xline, xline + 4),
+ 15: "END EBCDIC HEADER",
+ }
+ rows = []
+ for line_no in range(1, 41):
+ line = ""
+ if line_no in lines:
+ line = lines[line_no]
+ row = "C{0:>2} {1:76}".format(line_no, line)
+ rows.append(row)
+
+ rows = ''.join(rows)
+ rows = bytearray(rows, 'ascii') # mutable array of bytes
+ rows[-1] = 128 # \x80 -- Unsure if this is really required...
+ return bytes(rows) # immutable array of bytes that is compatible with strings
+
def create(filename, spec):
"""Create a new segy file.
@@ -97,24 +79,23 @@ def create(filename, spec):
f._samples = spec.samples
f._ext_headers = spec.ext_headers
- f._bsz = segyio._segyio.trace_bsize(f.samples)
+ f._bsz = _segyio.trace_bsize(f.samples)
- txt_hdr_sz = segyio._segyio.textheader_size()
- bin_hdr_sz = segyio._segyio.binheader_size()
- f._tr0 = txt_hdr_sz + bin_hdr_sz + (spec.ext_headers * txt_hdr_sz)
- f._sorting = spec.sorting
- f._fmt = int(spec.format)
- f._offsets = numpy.copy(numpy.asarray(spec.offsets, dtype = numpy.intc))
- f._tracecount = len(spec.ilines) * len(spec.xlines) * len(spec.offsets)
+ txt_hdr_sz = _segyio.textheader_size()
+ bin_hdr_sz = _segyio.binheader_size()
+ f._tr0 = txt_hdr_sz + bin_hdr_sz + (spec.ext_headers * txt_hdr_sz)
+ f._sorting = spec.sorting
+ f._fmt = int(spec.format)
+ f._offsets = numpy.copy(numpy.asarray(spec.offsets, dtype = numpy.intc))
+ f._tracecount = len(spec.ilines) * len(spec.xlines) * len(spec.offsets)
- f._il = int(spec.iline)
+ f._il = int(spec.iline)
f._ilines = numpy.copy(numpy.asarray(spec.ilines, dtype=numpy.uintc))
- f._xl = int(spec.xline)
+ f._xl = int(spec.xline)
f._xlines = numpy.copy(numpy.asarray(spec.xlines, dtype=numpy.uintc))
- line_metrics = segyio._segyio.init_line_metrics(f.sorting, f.tracecount,
- len(f.ilines), len(f.xlines), len(f.offsets))
+ line_metrics = _segyio.init_line_metrics(f.sorting, f.tracecount, len(f.ilines), len(f.xlines), len(f.offsets))
f._iline_length = line_metrics['iline_length']
f._iline_stride = line_metrics['iline_stride']
@@ -123,11 +104,12 @@ def create(filename, spec):
f._xline_stride = line_metrics['xline_stride']
f.text[0] = default_text_header(f._il, f._xl, segyio.TraceField.offset)
- f.bin = { 3213: f.tracecount,
- 3217: 4000,
- 3221: f.samples,
- 3225: f.format,
- 3505: f.ext_headers,
- }
+ f.bin = {
+ 3213: f.tracecount,
+ 3217: 4000,
+ 3221: f.samples,
+ 3225: f.format,
+ 3505: f.ext_headers,
+ }
return f
diff --git a/python/segyio/open.py b/python/segyio/open.py
index 527a36b..1f8d5cf 100644
--- a/python/segyio/open.py
+++ b/python/segyio/open.py
@@ -55,7 +55,7 @@ def open(filename, mode="r", iline=189, xline=193):
f._tr0 = metrics['trace0']
f._fmt = metrics['format']
f._bsz = metrics['trace_bsize']
- f._ext_headers = (f._tr0 - 3600) / 3200 # should probably be from C
+ f._ext_headers = (f._tr0 - 3600) // 3200 # should probably be from C
f._tracecount = metrics['trace_count']
diff --git a/python/segyio/segy.py b/python/segyio/segy.py
index 7aa2871..b8fd98f 100644
--- a/python/segyio/segy.py
+++ b/python/segyio/segy.py
@@ -25,6 +25,11 @@ import segyio._segyio as _segyio
from segyio.tracesortingformat import TraceSortingFormat
+try:
+ from itertools import izip as zip
+except ImportError: # will be 3.x series
+ pass
+
class SegyFile(object):
@@ -282,7 +287,7 @@ class SegyFile(object):
val = itertools.repeat(val)
h, buf = self.header, None
- for i, v in itertools.izip(range(self.tracecount), val):
+ for i, v in zip(range(self.tracecount), val):
h[i, buf] = v
@property
@@ -387,7 +392,7 @@ class SegyFile(object):
@trace.setter
def trace(self, val):
tr = self.trace
- for i, v in itertools.izip(range(len(tr)), val):
+ for i, v in zip(range(len(tr)), val):
tr[i] = v
def _shape_buffer(self, shape, buf):
@@ -530,7 +535,7 @@ class SegyFile(object):
def writefn(t0, length, step, val):
val = buffn(val)
step *= len(self.offsets)
- for i, v in itertools.izip(range(t0, t0 + (step * length), step), val):
+ for i, v in zip(range(t0, t0 + (step * length), step), val):
Trace.write_trace(i, v, self)
return Line(self, il_len, il_stride, lines, other_lines, buffn, readfn, writefn, "Inline")
@@ -659,7 +664,7 @@ class SegyFile(object):
def writefn(t0, length, step, val):
val = buffn(val)
step *= len(self.offsets)
- for i, v in itertools.izip(range(t0, t0 + step * length, step), val):
+ for i, v in zip(range(t0, t0 + step * length, step), val):
Trace.write_trace(i, v, self)
return Line(self, xl_len, xl_stride, lines, other_lines, buffn, readfn, writefn, "Crossline")
diff --git a/tests/test_segy.py b/tests/test_segy.py
index 1b9f53e..17beea4 100644
--- a/tests/test_segy.py
+++ b/tests/test_segy.py
@@ -13,6 +13,11 @@ from segyio._line import Line
from segyio._header import Header
from segyio._trace import Trace
+try:
+ from itertools import izip as zip
+ from itertools import imap as map
+except ImportError: # will be 3.x series
+ pass
def mklines(fname):
spec = segyio.spec()
@@ -59,7 +64,7 @@ class TestSegy(TestCase):
self.assertAlmostEqual(4.2, data[0, 0], places = 6)
# middle sample
- self.assertAlmostEqual(4.20024, data[0, sample_count/2-1], places = 6)
+ self.assertAlmostEqual(4.20024, data[0, sample_count//2-1], places = 6)
# last sample
self.assertAlmostEqual(4.20049, data[0, -1], places = 6)
@@ -68,7 +73,7 @@ class TestSegy(TestCase):
# first sample
self.assertAlmostEqual(4.22, data[middle_line, 0], places = 5)
# middle sample
- self.assertAlmostEqual(4.22024, data[middle_line, sample_count/2-1], places = 6)
+ self.assertAlmostEqual(4.22024, data[middle_line, sample_count//2-1], places = 6)
# last sample
self.assertAlmostEqual(4.22049, data[middle_line, -1], places = 6)
@@ -77,7 +82,7 @@ class TestSegy(TestCase):
# first sample
self.assertAlmostEqual(4.24, data[last_line, 0], places = 5)
# middle sample
- self.assertAlmostEqual(4.24024, data[last_line, sample_count/2-1], places = 6)
+ self.assertAlmostEqual(4.24024, data[last_line, sample_count//2-1], places = 6)
# last sample
self.assertAlmostEqual(4.24049, data[last_line, sample_count-1], places = 6)
@@ -90,7 +95,7 @@ class TestSegy(TestCase):
# first sample
self.assertAlmostEqual(1.22, data[0, 0], places = 5)
# middle sample
- self.assertAlmostEqual(1.22024, data[0, f.samples/2-1], places = 6)
+ self.assertAlmostEqual(1.22024, data[0, f.samples//2-1], places = 6)
# last sample
self.assertAlmostEqual(1.22049, data[0, f.samples-1], places = 6)
@@ -99,7 +104,7 @@ class TestSegy(TestCase):
# first sample
self.assertAlmostEqual(3.22, data[middle_line, 0], places = 5)
# middle sample
- self.assertAlmostEqual(3.22024, data[middle_line, f.samples/2-1], places = 6)
+ self.assertAlmostEqual(3.22024, data[middle_line, f.samples//2-1], places = 6)
# last sample
self.assertAlmostEqual(3.22049, data[middle_line, f.samples-1], places = 6)
@@ -108,7 +113,7 @@ class TestSegy(TestCase):
# first sample
self.assertAlmostEqual(5.22, data[last_line, 0], places = 5)
# middle sample
- self.assertAlmostEqual(5.22024, data[last_line, f.samples/2-1], places = 6)
+ self.assertAlmostEqual(5.22024, data[last_line, f.samples//2-1], places = 6)
# last sample
self.assertAlmostEqual(5.22049, data[last_line, f.samples-1], places = 6)
@@ -117,7 +122,7 @@ class TestSegy(TestCase):
self.assertEqual(len(f.ilines), sum(1 for _ in f.iline))
self.assertEqual(len(f.ilines), sum(1 for _ in f.iline[1:6]))
self.assertEqual(len(f.ilines), sum(1 for _ in f.iline[5:0:-1]))
- self.assertEqual(len(f.ilines) / 2, sum(1 for _ in f.iline[0::2]))
+ self.assertEqual(len(f.ilines) // 2, sum(1 for _ in f.iline[0::2]))
self.assertEqual(len(f.ilines), sum(1 for _ in f.iline[1:]))
self.assertEqual(3, sum(1 for _ in f.iline[::2]))
self.assertEqual(0, sum(1 for _ in f.iline[12:24]))
@@ -153,8 +158,8 @@ class TestSegy(TestCase):
self.assertEqual(1, f.offsets)
self.assertEqual(1, int(f.format))
- xlines = list(xrange(20, 25))
- ilines = list(xrange(1, 6))
+ xlines = list(range(20, 25))
+ ilines = list(range(1, 6))
self.assertEqual(xlines, list(f.xlines))
self.assertEqual(ilines, list(f.ilines))
self.assertEqual(25, f.tracecount)
@@ -164,13 +169,13 @@ class TestSegy(TestCase):
def test_traces_slicing(self):
with segyio.open(self.filename, "r") as f:
- traces = map(np.copy, f.trace[0:6:2])
+ traces = list(map(np.copy, f.trace[0:6:2]))
self.assertEqual(len(traces), 3)
self.assertEqual(traces[0][49], f.trace[0][49])
self.assertEqual(traces[1][49], f.trace[2][49])
self.assertEqual(traces[2][49], f.trace[4][49])
- rev_traces = map(np.copy, f.trace[4::-2])
+ rev_traces = list(map(np.copy, f.trace[4::-2]))
self.assertEqual(rev_traces[0][49], f.trace[4][49])
self.assertEqual(rev_traces[1][49], f.trace[2][49])
self.assertEqual(rev_traces[2][49], f.trace[0][49])
@@ -292,15 +297,15 @@ class TestSegy(TestCase):
self.assertEqual(0, sum(1 for _ in f.iline[:, 10:12]))
self.assertEqual(0, sum(1 for _ in f.iline[10:12, :]))
- self.assertEqual((offs / 2) * ils, sum(1 for _ in f.iline[::2, :]))
- self.assertEqual(offs * (ils / 2), sum(1 for _ in f.iline[:, ::2]))
+ self.assertEqual((offs // 2) * ils, sum(1 for _ in f.iline[::2, :]))
+ self.assertEqual(offs * (ils // 2), sum(1 for _ in f.iline[:, ::2]))
- self.assertEqual((offs / 2) * ils, sum(1 for _ in f.iline[::-2, :]))
- self.assertEqual(offs * (ils / 2), sum(1 for _ in f.iline[:, ::-2]))
+ self.assertEqual((offs // 2) * ils, sum(1 for _ in f.iline[::-2, :]))
+ self.assertEqual(offs * (ils // 2), sum(1 for _ in f.iline[:, ::-2]))
- self.assertEqual((offs / 2) * (ils / 2), sum(1 for _ in f.iline[::2, ::2]))
- self.assertEqual((offs / 2) * (ils / 2), sum(1 for _ in f.iline[::2, ::-2]))
- self.assertEqual((offs / 2) * (ils / 2), sum(1 for _ in f.iline[::-2, ::2]))
+ self.assertEqual((offs // 2) * (ils // 2), sum(1 for _ in f.iline[::2, ::2]))
+ self.assertEqual((offs // 2) * (ils // 2), sum(1 for _ in f.iline[::2, ::-2]))
+ self.assertEqual((offs // 2) * (ils // 2), sum(1 for _ in f.iline[::-2, ::2]))
def test_line_generators(self):
with segyio.open(self.filename, "r") as f:
@@ -312,7 +317,7 @@ class TestSegy(TestCase):
def test_traces_raw(self):
with segyio.open(self.filename, "r") as f:
- gen_traces = np.array(map( np.copy, f.trace ), dtype = np.single)
+ gen_traces = np.array(list(map( np.copy, f.trace )), dtype = np.single)
raw_traces = f.trace.raw[:]
self.assertTrue(np.array_equal(gen_traces, raw_traces))
@@ -326,10 +331,10 @@ class TestSegy(TestCase):
self.assertTrue(np.array_equal(f.trace[10], f.trace.raw[10]))
- for raw, gen in itertools.izip(f.trace.raw[::2], f.trace[::2]):
+ for raw, gen in zip(f.trace.raw[::2], f.trace[::2]):
self.assertTrue(np.array_equal(raw, gen))
- for raw, gen in itertools.izip(f.trace.raw[::-1], f.trace[::-1]):
+ for raw, gen in zip(f.trace.raw[::-1], f.trace[::-1]):
self.assertTrue(np.array_equal(raw, gen))
def test_read_header(self):
@@ -541,7 +546,7 @@ class TestSegy(TestCase):
with segyio.create(fname, spec) as dst:
tr = np.arange( start = 1.000, stop = 1.151, step = 0.001, dtype = np.single)
- for i in xrange( len( dst.trace ) ):
+ for i in range( len( dst.trace ) ):
dst.trace[i] = tr
tr += 1.000
@@ -603,7 +608,7 @@ class TestSegy(TestCase):
self.assertEqual(1, f.header[0][TraceField.offset])
self.assertEqual(2, f.header[1][TraceField.offset])
- for x, y in itertools.izip(f.iline[:,:], cube):
+ for x, y in zip(f.iline[:,:], cube):
self.assertListEqual(list(x.flatten()), list(y.flatten()))
def test_create_write_lines(self):
@@ -698,7 +703,7 @@ class TestSegy(TestCase):
with segyio.open(self.filename, "r") as f:
self.assertEqual(len(f.depth_slice), f.samples)
- for depth_sample in xrange(f.samples):
+ for depth_sample in range(f.samples):
depth_slice = f.depth_slice[depth_sample]
self.assertIsInstance(depth_slice, np.ndarray)
self.assertEqual(depth_slice.shape, (5, 5))
@@ -725,7 +730,7 @@ class TestSegy(TestCase):
buf = np.empty(shape=(5, 5), dtype=np.single)
def value(x, y):
- return x + (1.0 / 5) * y
+ return x + (1.0 // 5) * y
for x, y in itertools.product(range(5), range(5)):
buf[x][y] = value(x, y)
@@ -734,6 +739,6 @@ class TestSegy(TestCase):
f.depth_slice[7] = buf * 3.14 # assign to depth 7
self.assertTrue(np.allclose(f.depth_slice[7], buf * 3.14))
- f.depth_slice = [buf * i for i in xrange(len(f.depth_slice))] # assign to all depths
+ f.depth_slice = [buf * i for i in range(len(f.depth_slice))] # assign to all depths
for index, depth_slice in enumerate(f.depth_slice):
self.assertTrue(np.allclose(depth_slice, buf * index))
diff --git a/tests/test_segyio_c.py b/tests/test_segyio_c.py
index 8f33382..6882218 100644
--- a/tests/test_segyio_c.py
+++ b/tests/test_segyio_c.py
@@ -4,46 +4,46 @@ from unittest import TestCase
import numpy
import segyio._segyio as _segyio
-ACTUAL_TEXT_HEADER = "C 1 DATE: 2016-09-19 " \
- "C 2 AN INCREASE IN AMPLITUDE EQUALS AN INCREASE IN ACOUSTIC IMPEDANCE " \
- "C 3 Written by libsegyio (python) " \
- "C 4 " \
- "C 5 " \
- "C 6 " \
- "C 7 " \
- "C 8 " \
- "C 9 " \
- "C10 " \
- "C11 TRACE HEADER POSITION: " \
- "C12 INLINE BYTES 189-193 | OFFSET BYTES 037-041 " \
- "C13 CROSSLINE BYTES 193-197 | " \
- "C14 " \
- "C15 END EBCDIC HEADER " \
- "C16 " \
- "C17 " \
- "C18 " \
- "C19 " \
- "C20 " \
- "C21 " \
- "C22 " \
- "C23 " \
- "C24 " \
- "C25 " \
- "C26 " \
- "C27 " \
- "C28 " \
- "C29 " \
- "C30 " \
- "C31 " \
- "C32 " \
- "C33 " \
- "C34 " \
- "C35 " \
- "C36 " \
- "C37 " \
- "C38 " \
- "C39 " \
- "C40 \x80"
+ACTUAL_TEXT_HEADER = b"C 1 DATE: 2016-09-19 " \
+ b"C 2 AN INCREASE IN AMPLITUDE EQUALS AN INCREASE IN ACOUSTIC IMPEDANCE " \
+ b"C 3 Written by libsegyio (python) " \
+ b"C 4 " \
+ b"C 5 " \
+ b"C 6 " \
+ b"C 7 " \
+ b"C 8 " \
+ b"C 9 " \
+ b"C10 " \
+ b"C11 TRACE HEADER POSITION: " \
+ b"C12 INLINE BYTES 189-193 | OFFSET BYTES 037-041 " \
+ b"C13 CROSSLINE BYTES 193-197 | " \
+ b"C14 " \
+ b"C15 END EBCDIC HEADER " \
+ b"C16 " \
+ b"C17 " \
+ b"C18 " \
+ b"C19 " \
+ b"C20 " \
+ b"C21 " \
+ b"C22 " \
+ b"C23 " \
+ b"C24 " \
+ b"C25 " \
+ b"C26 " \
+ b"C27 " \
+ b"C28 " \
+ b"C29 " \
+ b"C30 " \
+ b"C31 " \
+ b"C32 " \
+ b"C33 " \
+ b"C34 " \
+ b"C35 " \
+ b"C36 " \
+ b"C37 " \
+ b"C38 " \
+ b"C39 " \
+ b"C40 \x80"
class _segyioTests(TestCase):
@@ -100,7 +100,9 @@ class _segyioTests(TestCase):
_segyio.write_textheader(f, 0, "yolo" * 800)
- self.assertEqual(_segyio.read_textheader(f, 0), "yolo" * 800)
+ textheader = _segyio.read_textheader(f, 0)
+ textheader = textheader.decode('ascii') # Because in Python 3.5 bytes are not comparable to strings
+ self.assertEqual(textheader, "yolo" * 800)
_segyio.close(f)
diff --git a/tests/test_utils.c b/tests/test_utils.c
index f1b942c..9e22125 100644
--- a/tests/test_utils.c
+++ b/tests/test_utils.c
@@ -31,8 +31,6 @@ static void testEbcdicTable() {
ebcdic2ascii(ebcdic, result_ascii);
assertTrue(strcmp(ascii, result_ascii) == 0, "Conversion from EBCDIC to ASCII to EBCDIC failed!");
- for(unsigned char i = 0; i < 255; i++) {
- }
}
static void testConversionAllocation() {
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/debian-science/packages/segyio.git
More information about the debian-science-commits
mailing list