Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def time_to_x(self, time):
""" Return x-coordinate in spectrogram that corresponds to the
passed datetime value.
Parameters
----------
time : parse_time compatible
Datetime to find the x coordinate for.
"""
# This is impossible for frequencies because that mapping
# is not injective.
time = parse_time(time)
diff = time - self.start
diff_s = SECONDS_PER_DAY * diff.days + diff.seconds
result = diff_s // self.t_delt
if 0 <= result <= self.shape[1]:
return result
raise ValueError("Out of range.")
try:
start = parse_time(start)
except ValueError:
# XXX: We could do better than that.
if get_day(self.start) != get_day(self.end):
raise TypeError(
"Time ambiguous because data spans over more than one day"
)
start = datetime.datetime(
self.start.year, self.start.month, self.start.day,
*map(int, start.split(":"))
)
start = self.time_to_x(start)
if end is not None:
try:
end = parse_time(end)
except ValueError:
if get_day(self.start) != get_day(self.end):
raise TypeError(
"Time ambiguous because data spans over more than one day"
)
end = datetime.datetime(
self.start.year, self.start.month, self.start.day,
*map(int, end.split(":"))
)
end = self.time_to_x(end)
return self[:, start:end]
def in_interval(self, start=None, end=None):
""" Return part of spectrogram that lies in [start, end).
Parameters
----------
start : None or datetime or parse_time compatible string or time string
Start time of the part of the spectrogram that is returned. If the
measurement only spans over one day, a colon separated string
representing the time can be passed.
end : None or datetime or parse_time compatible string or time string
See start.
"""
if start is not None:
try:
start = parse_time(start)
except ValueError:
# XXX: We could do better than that.
if get_day(self.start) != get_day(self.end):
raise TypeError(
"Time ambiguous because data spans over more than one day"
)
start = datetime.datetime(
self.start.year, self.start.month, self.start.day,
*map(int, start.split(":"))
)
start = self.time_to_x(start)
if end is not None:
try:
end = parse_time(end)
except ValueError:
if get_day(self.start) != get_day(self.end):