Skip to content

RF: Prefer math.gcd to hand-rolled Euclid's algorithm #3177

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

Merged
merged 2 commits into from
Feb 25, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 4 additions & 23 deletions nipype/algorithms/modelgen.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,25 +30,6 @@
iflogger = logging.getLogger("nipype.interface")


def gcd(a, b):
"""
Return the greatest common divisor of two integers (uses Euclid's algorithm).

Examples
--------
>>> gcd(4, 5)
1
>>> gcd(4, 8)
4
>>> gcd(22, 55)
11

"""
while b > 0:
a, b = b, a % b
return a


def spm_hrf(RT, P=None, fMRI_T=16):
"""
python implementation of spm_hrf
Expand Down Expand Up @@ -799,9 +780,9 @@ def _gen_regress(self, i_onsets, i_durations, i_amplitudes, nscans):
matplotlib.use(config.get("execution", "matplotlib_backend"))
import matplotlib.pyplot as plt

TR = np.round(self.inputs.time_repetition * 1000) # in ms
TR = int(np.round(self.inputs.time_repetition * 1000)) # in ms
if self.inputs.time_acquisition:
TA = np.round(self.inputs.time_acquisition * 1000) # in ms
TA = int(np.round(self.inputs.time_acquisition * 1000)) # in ms
else:
TA = TR # in ms
nvol = self.inputs.volumes_in_cluster
Expand All @@ -813,10 +794,10 @@ def _gen_regress(self, i_onsets, i_durations, i_amplitudes, nscans):
if len(durations) == 1:
durations = durations * np.ones((len(i_onsets)))
onsets = np.round(np.array(i_onsets) * 1000)
dttemp = gcd(TA, gcd(SILENCE, TR))
dttemp = math.gcd(TA, math.gcd(SILENCE, TR))
if dt < dttemp:
if dttemp % dt != 0:
dt = float(gcd(dttemp, dt))
dt = float(math.gcd(dttemp, int(dt)))

if dt < 1:
raise Exception("Time multiple less than 1 ms")
Expand Down