-
Notifications
You must be signed in to change notification settings - Fork 144
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
Realign4d TR from header #410
Conversation
@matthew-brett ok, so the logic has an issue: If the TR values are non-consistent, Checking for None twice seems really clunky. I'd prefer to just raise an exception in |
It will be more obvious to you, doing the implementation, what the cleanest way is - please feel free to chose what you prefer. |
@matthew-brett It seems the checks are indeed failing because of something with the functions I've been working on. Can you help me figure out why? I don't understand what this not raising a
|
What happens when you try running the same checks in IPython, like:
Do they generate a ValueError? Should they? |
It generates a NameError, as I believe it should, since |
Sure, but what if you replicate the environment in the test. I guess |
Hmmm, that gives me the output I would expect, but I am surprised that it's the last line of the exception that gets highlighted:
Also, I don't like how that error message prints, so I just updated the PR, now it gives me this, which I guess is just as wrong/right, but looks a deal better:
|
raise ValueError('Repetition time cannot be None') | ||
tr = tr_from_header(images) | ||
if tr == 1: | ||
raise ValueError('A TR of 1 was found in the header. '\ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You don't need the line continuation here, Python will combine the strings without them.
Sorry - I see - you are seeing the errors for the tests that were expecting None to generate an error. You need to replace those tests with tests of the new TR-finding machinery. |
if tr != images_tr: | ||
raise ValueError('TR inconsistent between images.') | ||
else: | ||
return images_tr |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The else
is a bit confusing here, you usually use this to run something when there was no break
in the for loop. Of course there isn't here, but an experienced programmer will look for one. How about something like:
def tr_from_header(images):
""" A good docstring """
if not isinstance(images, list):
images = [images]
images_tr = None
for image in images:
tr = image.header.get_zooms()[3]
if images_tr is None:
images_tr = tr
if tr != images_tr:
raise ValueError('TR inconsistent between images.')
return images_tr
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I went for no_init_value
instead of the more elegant
if images_tr is None:
images_tr = tr
BEcause I was worried that the logic would be unable to pick up different-TR images if the first n of the list had image.header.get_zooms()[3] == None
but it seems .get_zooms()
can never produce None
. Can you confirm this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, that's right - it only produces a vector of numbers, one per dimension of the image.
Currently we only get a ValueError if the header TR is 0 or 1 - which is not the case for |
Why not write a few tests for the states that do raise errors, such as bad TRs or TR not equal between images? You could make some example
etc. |
I can't really make sense of the coveralls output - how exactly did I reduce the coverage? @matthew-brett Also, can you help me out with travis? I get this mesage:
But afaict |
if tr == 1: | ||
raise ValueError('A TR of 1 was found in the header. ' | ||
'This value often stands in for an unknown TR. ' | ||
'Please specify TR explicitly.') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe add: or consider "header-allow-1.0"
.
Sorry, the coveralls output is hard to read. I now prefer |
@matthew-brett ok, it seems there is yet another problem. TR is needed in What would you say is better - moving the |
I'd say move that logic into the |
Current coverage is 83.54% (diff: 100%)@@ master #410 diff @@
==========================================
Files 291 291
Lines 27757 27790 +33
Methods 0 0
Messages 0 0
Branches 3252 3262 +10
==========================================
+ Hits 23185 23218 +33
Misses 3570 3570
Partials 1002 1002
|
I added the logic to I still don't understand what's up with codecov. The lines it highlights are tested by line 202 and 204, respectively. |
Not sure what's going on with the coverage. Have you tested that 'header-allow-1.0' works on an image with TR=0? EDIT - sorry - on an image with TR=1. |
@matthew-brett anything else you'd like to recommend before I squash this? |
from nibabel.affines import apply_affine | ||
|
||
from ...externals.six import string_types |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This import should go at the top, because it's a sort-of special import, like a system import.
@@ -3,14 +3,16 @@ | |||
# vi: set ft=python sts=4 ts=4 sw=4 et: | |||
|
|||
import warnings | |||
import nibabel as nib | |||
import numpy as np |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Import numpy before nibabel (system imports first, then common imports, then package-specific imports).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great - nearly there.
and extended TR logic, and assed tests
Great - thanks a lot for your patience. |
with "fail-safe" caveats. addressing #409