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

Feature request: ability to read heterogeneous types in FortranFile #3865

Merged
merged 1 commit into from
Aug 31, 2014

Conversation

astrofrog
Copy link
Contributor

At the moment, the three read_* methods of scipy.io.FortranFile can only read records with homogeneous data types, but it would be nice to allow some more flexibiity, for cases where the data is written e.g. as:

write(10) some_int, some_float

I was thinking that we could add a new method read_mixed that could take a list of dtypes, and returns a Python list with scalars inside.

I could look into it if there was interest in having such a feature in scipy.

@rgommers
Copy link
Member

@astrofrog FortranFile is quite new so I don't think it has many users yet, so they may not express their interest here. Sounds like a reasonable addition though. If it's useful for you and can be implemented in a clean way (meaning: it doesn't start to look like the numpy.loadtxt/genfromtxt mess), then I'd say go for it.

@astrofrog
Copy link
Contributor Author

@rgommers - ok, I'll see what I can do! I won't have time this week, but will look into it at the weekend.

@WarrenWeckesser
Copy link
Member

Definitely use the numpy dtype! dtype has quite a few ways to spell a data format, including the following, which generates field names automatically:

In [30]: dtype("i4,d,(3,3)f")
Out[30]: dtype([('f0', '<i4'), ('f1', '<f8'), ('f2', '<f4', (3, 3))])

@astrofrog
Copy link
Contributor Author

@WarrenWeckesser - thanks! The reason I deleted my comment is because I realized that in fact I could maybe even use the dtype way with the existing read_record and it may work without changes! (then the only thing left to do for this PR would be to improve the docs). I'm testing it out!

@astrofrog
Copy link
Contributor Author

@WarrenWeckesser @rgommers - ok, so read_record does work with mixed type dtypes from Numpy, so no need for a new method! However, I will open a pull request with an improved docstring for read_record :)

@astrofrog
Copy link
Contributor Author

I can also add some tests for that mode to make sure if doesn't break in future.

@astrofrog
Copy link
Contributor Author

@rgommers @WarrenWeckesser - I've now attached code to this issue, but I have one question - as you can see if the tests, if one has an array with shape (2,) then read_record will return a numpy structured array with a length of 1 so that record['f3'] returns a (1,2) array. I could make it so that if the dtype is mixed and matches the size of the record, we slice the structured array with [0] automatically? What do you think?

@astrofrog
Copy link
Contributor Author

Presumably this doesn't need a changelog entry since there is no change in behavior?

@coveralls
Copy link

Coverage Status

Changes Unknown when pulling eb17f81 on astrofrog:fortran-mixed-records into * on scipy:master*.

pv added a commit that referenced this pull request Aug 31, 2014
DOC: io: document and test reading record inputs in FortranFile
@pv pv merged commit 62097b5 into scipy:master Aug 31, 2014
@pv
Copy link
Member

pv commented Aug 31, 2014

LGTM, merged

@pv pv added this to the 0.15.0 milestone Aug 31, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement A new feature or improvement scipy.io
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants