sixty-north / segpy

A Python package for reading and writing SEG Y files.
Other
102 stars 54 forks source link

Make numpy an optional dependency #22

Closed rth closed 9 years ago

rth commented 9 years ago

While I see the point of producing a module that only depends on the standard library, in practice I'm sure that a vast majority of use cases consist of something like: read SEG-Y and return some numpy arrays, or given some numpy arrays write the data in SEG-Y. There is just no way of doing anything remotely related to science or data analysis in python without numpy. In terms of interface, one then would want something like,

from segpy.reader import create_reader

with open(filename, 'wb') as fh:
    segy_reader = create_reader(filename)

for trace_index in segy_reader_in.trace_indexes():
    segy_reader.trace_samples_numpy(trace_index)  # return numpy ndarray

this could be easily achieved adding a couple of methods to SegYReader, however this is made more difficult by numpy related things being exported to segpy_ext. Of course, one could do,

 try:
      import segpy.ext
 exept ImportError:
      print('Warning: numpy not avalable')
 except:
      raise

in the corresponding method, but then one could do the same thing with numpy and just add it as an optional dependency of the segpy module.

My point is that the fact that we can't use any of the numpy functions, in the main segpy code (even if the function in question is never run when numpy is not present), makes things much more difficult. What's your opinion about this ?

rob-smallshire commented 9 years ago

There are plenty of use cases which don't need to involve numpy, and which don't even use the trace data, such as indexing and searching of large numbers of SEG Y files which only uses the headers.

Furthermore, I'd like to retain the option of running on PyPy (or indeed Jython or IronPython when they eventually attain Python 3 compatibility).

So, no, I won't be maying core Segpy depend on numpy. That said, you can expect segpy_numpy to contain many more useful tools along the lines of what you suggest, which will be layered on top of segpy. I have some of my own bumpy wrappers, but I haven't incorporated them yet. I want to get core segpy stable and released first.

rth commented 9 years ago

Fair enough. Thanks for your response. It is true that Jython, and IronPython don't play so well with numpy. PyPy should get there though. I'll be looking into wrappers with numpy on top of segpy then, thanks.

rob-smallshire commented 9 years ago

I've added into segpy_numpy some experimental wrappers for extracting as numpy arrays trace header fields, inlines, cross lines and time slices from 3-d surveys. They're essentially untested at this point, but you may want to give them a spin.

    from segpy.reader import create_reader
    from segpy_numpy.extract import extract_inline_3d    

    with open(segy_filename, 'rb') as segy_file:
        segy_reader = create_reader(segy_file)
        inline_array = extract_inline_3d(segy_reader, inline_number=1, null=0.0)
rth commented 9 years ago

Great, thanks. I'll have a look.