import numpy as np
from effectlayer import EffectLayer
from model import SomaModel
import sys

CYCLE_TIME = 2
OFFSET = 0
TRIGGER_THRESHOLD = 0.05
COLOR = (0,0,1)

class AxonChaseLayer(EffectLayer):

    def __init__(self, cycle_time=CYCLE_TIME, offset=OFFSET, color=COLOR, trigger_threshold=TRIGGER_THRESHOLD):
        self.color = np.array(color)
        self.cycle_time = cycle_time
        self.offset = offset
        self.trigger_threshold = trigger_threshold

        # Extract and normalize axon point positions in one dimension
        model = SomaModel()
        x_coords = [model.nodes[i][1] for i in model.axonIndices]
        min_ = min(x_coords)
        max_ = max(x_coords)
        self.normalized_x_coords = [(coord - min_) / (max_ - min_) for coord in x_coords]

    def render(self, model, params, frame):
        phi = self.phi(params)

        for i, x in zip(model.axonIndices, self.normalized_x_coords):
            # node = model.nodes[i]
            dist = abs(x - phi)
            if dist < self.trigger_threshold:
                # print "%d %f %f" % (i, x, dist)
                frame[i] = self.color * (1-dist)

        return frame

    def phi(self, params):
        """ 
        Converts current time + time offset into phi (oscillatory phase parameter in range [0,1]) 
        """
        phi = ((params.time + self.offset) % self.cycle_time)/self.cycle_time + 0.01
        return phi

@@ -114,23 +114,24 @@
 SET = 0%0A
-TRIGGER
+DISTANCE
 _THRESHO
@@ -153,11 +153,11 @@
  = (
-0,0
+1,1
 ,1)%0A
@@ -267,23 +267,24 @@
 =COLOR, 
-trigger
+distance
 _thresho
@@ -290,15 +290,16 @@
 old=
-TRIGGER
+DISTANCE
 _THR
@@ -304,16 +304,78 @@
 HRESHOLD
+, segments=%5B'axon'%5D, direction='forward', blend_mode='replace'
 ):%0A     
@@ -481,31 +481,32 @@
        self.
-trigger
+distance
 _threshold =
@@ -506,23 +506,24 @@
 shold = 
-trigger
+distance
 _thresho
@@ -525,16 +525,550 @@
 reshold%0A
+        if direction not in ('forward','reverse'):%0A            direction = 'forward'%0A        self.direction = direction%0A%0A        model = SomaModel()%0A        #select indices%0A        indices = %5B%5D%0A        if 'axon' in segments or 'all' in segments:%0A            indices += model.axonIndices %0A        if 'upper' in segments or 'all' in segments:%0A            indices += model.upperIndices %0A        if 'lower' in segments or 'all' in segments:%0A            indices += model.lowerIndices %0A        self.indices = indices%0A        print indices%0A%0A
 %0A       
@@ -1133,36 +1133,8 @@
 sion
-%0A        model = SomaModel()
 %0A   
@@ -1177,27 +1177,22 @@
 or i in 
-model.axonI
+self.i
 ndices%5D%0A
@@ -1414,16 +1414,82 @@
 (params)
+%0A        if self.direction == 'forward':%0A            phi = 1 - phi
 %0A%0A      
@@ -1506,27 +1506,22 @@
  in zip(
-model.axonI
+self.i
 ndices, 
@@ -1646,15 +1646,16 @@
 elf.
-trigger
+distance
 _thr

Make the chase effect more general.