Commit 6d0aeddb authored by Klaus Zimmermann's avatar Klaus Zimmermann
Browse files

Change fusing behavior to consider spells in all affected periods (closes #189)

Up to this point, if a spell is touches more than one period,
it is considered *only* for the last period.
This commit changes that to consider spells up to the current period
as well as the continued spell in subsequent periods.
parent 68497c0a
...@@ -53,30 +53,21 @@ class SpellLength(ThresholdMixin, ReducerMixin, IndexFunction): ...@@ -53,30 +53,21 @@ class SpellLength(ThresholdMixin, ReducerMixin, IndexFunction):
def post_process(self, cube, data, coords, period, **kwargs): def post_process(self, cube, data, coords, period, **kwargs):
def fuse(this, next_chunk, previous_tail): def fuse(this, previous_tail):
own_mask = da.ma.getmaskarray(this[..., 0]) own_mask = da.ma.getmaskarray(this[..., 0])
own_length = this[..., 0]
own_head = this[..., 1] own_head = this[..., 1]
own_tail = da.where(this[..., 0] == this[..., 1],
previous_tail + this[..., 3],
this[..., 3])
next_head = next_chunk[..., 1]
head = da.where(own_head, previous_tail + own_head, 0.)
internal = this[..., 2] internal = this[..., 2]
tail = da.where(next_head, 0., own_tail) own_tail = this[..., 3]
head = da.where(own_head, previous_tail + own_head, 0.)
tail = da.where(own_length == own_head,
previous_tail + own_tail,
own_tail)
stack = da.stack([head, internal, tail], axis=-1) stack = da.stack([head, internal, tail], axis=-1)
spell_length = da.ma.masked_array( spell_length = da.ma.masked_array(
self.lazy_reducer(stack, axis=-1), self.lazy_reducer(stack, axis=-1),
own_mask) own_mask)
return spell_length, own_tail return spell_length, tail
def fuse_last(this, previous_tail):
own_head = this[..., 1]
tail = this[..., 3]
head = da.where(own_head, previous_tail + own_head, 0.)
internal = this[..., 2]
stack = da.stack([head, internal, tail], axis=-1)
spell_length = self.lazy_reducer(stack, axis=-1)
return spell_length
if self.fuse_periods and len(data) > 1: if self.fuse_periods and len(data) > 1:
stack = [] stack = []
...@@ -88,12 +79,11 @@ class SpellLength(ThresholdMixin, ReducerMixin, IndexFunction): ...@@ -88,12 +79,11 @@ class SpellLength(ThresholdMixin, ReducerMixin, IndexFunction):
for next_chunk in data[1:]: for next_chunk in data[1:]:
spell_length, previous_tail = fuse(this, spell_length, previous_tail = fuse(this,
next_chunk,
previous_tail) previous_tail)
stack.append(spell_length) stack.append(spell_length)
this = next_chunk this = next_chunk
stack.append(fuse_last(next_chunk, previous_tail)) stack.append(fuse(next_chunk, previous_tail)[0])
res_data = da.stack(stack, axis=0) res_data = da.stack(stack, axis=0)
else: else:
res_data = self.lazy_reducer(data[..., 1:], axis=-1) res_data = self.lazy_reducer(data[..., 1:], axis=-1)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment