def operator_overload(self, another_stream, func): from merge import zip_map output_stream = Stream() zip_map(func=func, in_streams=[self, another_stream], out_stream=output_stream) return output_stream
def echo_func(self, in_streams, out_streams): # IDENTIFY INPUT AND OUTPUT STREAMS. # We give names for the input and output streams # so that the code is easier to read; this is # merely a convenience. # The input and output streams are: original_sound = in_streams[0] echo = out_streams[0] echo.extend([0]*self.delay) # CREATE INTERNAL STREAMS. original_sound_plus_echo = Stream( name='original sound plus echo') # CREATE AGENTS # Make the agent that sums the input # streams --- which are echo and original # sound --- to get the original sound plus the echo. zip_map( func=sum, in_streams=[original_sound, echo], out_stream=original_sound_plus_echo) # Make the agent that creates the echo by # echoing the original sound plus the echo. window_dot_product( in_stream=original_sound_plus_echo, out_stream=echo, multiplicand_vector=self.attenuation_vector) # Agents that store sounds in files stream_to_file(in_stream=echo, filename=self.echo_name + '.txt') stream_to_file(in_stream=original_sound_plus_echo, filename='original_sound_plus_' + self.echo_name + '.txt')
def compute(in_streams): def subtract_mean(window): return window[-1] - sum(window) / float(len(window)) def magnitude_of_vector(coordinates): return math.sqrt(sum([v * v for v in coordinates])) def simple_anomaly(value, threshold): if value > threshold: return 1.0 else: return 0.0 zero_mean_streams = [ Stream('zero mean e'), Stream('zero mean n'), Stream('zero mean z') ] magnitude_stream = Stream('magnitude') anomaly_stream = Stream('anomalies') filenames = ['zero_mean_e.txt', 'zero_mean_n.txt', 'zero_mean_z.txt'] for i in range(3): map_window(func=subtract_mean, in_stream=in_streams[i], out_stream=zero_mean_streams[i], window_size=8000, step_size=1) zip_map(func=magnitude_of_vector, in_streams=zero_mean_streams, out_stream=magnitude_stream) map_element(func=simple_anomaly, in_stream=magnitude_stream, out_stream=anomaly_stream, threshold=0.1) stream_to_file(in_stream=magnitude_stream, filename='magnitude.txt') stream_to_file(in_stream=anomaly_stream, filename='anomaly.txt')
def aggregate(self, in_streams, out_streams): # IDENTIFY INPUT AND OUTPUT STREAMS. # original_sound = in_streams[0] # echoes = in_streams[1:] # original_stream_copy = out_streams[0] # heard_sound = out_streams[1] # CREATE INTERNAL STREAMS. # This agent has no internal streams. # CREATE AGENTS # Create agent that creates heard sound by summing # original sound and and all echoes. zip_map(sum, in_streams, out_streams[1]) # Copy original stream to an output stream. copy_stream(in_streams[0], out_streams[0]) # Create agents that store sounds in files. stream_to_file(out_streams[1], self.output_file_name) stream_to_file(in_streams[0], 'original_sound.txt')
def compute_func(in_streams, out_streams): ## F1 - Amplitude Adjustment def delay_and_attenuate(in_streams): tmp = deepcopy(in_streams) for i in range(len(tmp)): tmp[i] *= alpha #print("Length of tmp==tmp1 --",len(tmp)==len(tmp1)) return tmp t = Stream(initial_value=[0] * delay) w = Stream() map_list(func=delay_and_attenuate, in_stream=in_streams[0], out_stream=t) zip_map(sum, [t, in_streams[0]], w) stream_to_file(in_stream=w, filename='Reverb.dat')
def compute_func(in_streams, out_streams): # Name external streams for convenience original_sound = in_streams[0] heard_sound = out_streams[0] # Define internal streams echo = Stream(name='echo', initial_value=[0]*delay) # Create agents # Agent that creates heard sound from original sound and echo zip_map(func=sum, in_streams=[original_sound, echo], out_stream=heard_sound) # Agent that creates the echo from the heard sound. window_dot_product( in_stream=heard_sound, out_stream=echo, multiplicand_vector=attenuation_vector) # Agents that store sounds in files stream_to_file(in_stream=heard_sound, filename='heard.txt') stream_to_file(in_stream=echo, filename='echo.txt') stream_to_file(in_stream=original_sound, filename='original_sound.txt')
def g(in_streams, out_stream, **kwargs): return zip_map(func, in_streams, out_stream, **kwargs)
y_stream = Stream('y') # magnitude_stream is the stream of magnitudes. magnitude_stream = Stream('magnitudes') # input_stream is the same as raw_data_stream # except that it starts after window_size. # This allows for an appropriate comparison # with zero_mean_stream input_stream = Stream('input data') # CREATE AGENTS # Create x_stream stream_to_file(in_stream=x_stream, filename='x_data_file.txt') # Create y_stream stream_to_file(in_stream=y_stream, filename='y_data_file.txt') # Create magnitude_stream zip_map(func=magnitude_of_vector, in_streams=[x_stream, y_stream], out_stream=magnitude_stream) # Save magnitude_stream to file stream_to_file(in_stream=magnitude_stream, filename='magnitude_file.txt') # Put sin values into x_stream and # cosin values into y_stream t = np.arange(0.0, 8.0, 0.05) x_data = np.sin(2 * np.pi * t) y_data = np.cos(2 * np.pi * t) x_stream.extend(list(x_data)) y_stream.extend(list(y_data)) Stream.scheduler.step() # Use matplotlib to plot the data
def f(in_streams, out_streams): """ Parameters ---------- in_streams: list of Stream in_streams is usually a list of 3 streams indicating measurements in x, y and z directions or equivalently in e, n, and z (for east, north, vertical) directions. Each triaxial sensor generates x, y and z streams. out_streams: list of Stream out_streams has only one element, which is a Stream of int. An element of this stream is either 1.0 or 0.0. An element is 1.0 to indicate that an anomaly was detected and is 0.0 otherwise. """ # DECLARE STREAMS # 1. zero_means # Array of stream with one stream for each stream in in_streams # zero_means[0, 1, 2] usually represent streams in the x, y, z direction generated by a single triaxial sensor. zero_means = [ Stream('zero_means_' + str(i)) for i in range(len(in_streams)) ] # magnitudes is a stream of magnitudes of a vector from its x, y, z values magnitudes = Stream('magnitudes') # CREATE AGENTS # 1. subtract_mean agent # Define the terminating function def subtract_mean(window): return window[-1] - sum(window) / float(len(window)) # Wrap the terminating function to create an agent for i in range(len(in_streams)): map_window(func=subtract_mean, in_stream=in_streams[i], out_stream=zero_means[i], window_size=500, step_size=1, initial_value=0.0) # 2. magnitude agent # Define the terminating function def magnitude_of_vector(coordinates): return math.sqrt(sum([v * v for v in coordinates])) # Wrap the terminating function to create an agent zip_map(func=magnitude_of_vector, in_streams=zero_means, out_stream=magnitudes) # 3. local anomaly agent # Define the terminating function def simple_anomaly(value): if value > ANOMALY_THRESHOLD: return 1.0 else: return 0.0 # Wrap the terminating function to create an agent map_element(func=simple_anomaly, in_stream=magnitudes, out_stream=out_streams[0])