Skip to content

Commit

Permalink
in-place multiply and divide for UniformTime
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanov committed Nov 5, 2011
1 parent 922d68c commit 4fd9528
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
8 changes: 7 additions & 1 deletion nitime/tests/test_timeseries.py
Original file line number Diff line number Diff line change
Expand Up @@ -813,8 +813,14 @@ def assign_to_one_element_of(t): t[0]=42

# same as utime, but with a lower sampling rate
utime_2 = ts.UniformTime(t0=10, length=10, sampling_interval=2)
utime += np.arange(10)
utime += np.arange(10) # make utime match utime_2
npt.assert_equal(utime,utime_2)
npt.assert_equal(utime.sampling_interval,utime_2.sampling_interval)

utime = ts.UniformTime(t0=5, length=10, sampling_rate=1)
utime *= 2 # alternative way to make utime match utime_2
npt.assert_equal(utime.sampling_interval,utime_2.sampling_interval)
npt.assert_equal(utime.sampling_rate,utime_2.sampling_rate)

nonuniform = np.concatenate((range(2),range(3), range(5)))
def iadd_nonuniform(t): t+=nonuniform
Expand Down
15 changes: 14 additions & 1 deletion nitime/timeseries.py
Original file line number Diff line number Diff line change
Expand Up @@ -617,7 +617,7 @@ def __setitem__(self, key, val):
You can either use += on the full array, OR
create a new TimeArray from this UniformTime""")

def _convert_and_check_uniformity(self,val):
def _convert_and_check_uniformity(self, val):
# look at the units - convert the values to what they need to be (in
# the base_unit) and then delegate to the ndarray.__iadd__
if not hasattr(val, '_conversion_factor'):
Expand Down Expand Up @@ -649,6 +649,19 @@ def __isub__(self, val):
val = self._convert_and_check_uniformity(val)
return np.ndarray.__isub__(self, val)

def __imul__(self, val):
np.ndarray.__imul__(self, val)
self.sampling_interval *= val
self.sampling_rate = Frequency(self.sampling_rate / val)
return self

def __idiv__(self, val):
np.ndarray.__idiv__(self, val)
self.sampling_interval /= val
self.sampling_rate = Frequency(self.sampling_rate * val)
return self


def index_at(self, t, boolean=False):
"""Find the index that corresponds to the time bin containing t
Expand Down

0 comments on commit 4fd9528

Please sign in to comment.