<s> import numpy as np <EOL> def boxcar_filter ( time_series , lb = <NUM_LIT:0> , ub = <NUM_LIT:0.5> , n_iterations = <NUM_LIT:2> ) : <EOL> """<STR_LIT>""" <EOL> n = time_series . shape [ - <NUM_LIT:1> ] <EOL> len_boxcar_ub = np . ceil ( <NUM_LIT:1> / ( <NUM_LIT> * ub ) ) <EOL> boxcar_ub = np . empty ( int ( len_boxcar_ub ) ) <EOL> boxcar_ub . fill ( <NUM_LIT:1.0> / len_boxcar_ub ) <EOL> boxcar_ones_ub = np . ones_like ( boxcar_ub ) <EOL> if lb == <NUM_LIT:0> : <EOL> lb = None <EOL> else : <EOL> len_boxcar_lb = np . ceil ( <NUM_LIT:1> / ( <NUM_LIT> * lb ) ) <EOL> boxcar_lb = np . empty ( int ( len_boxcar_lb ) ) <EOL> boxcar_lb . fill ( <NUM_LIT:1.0> / len_boxcar_lb ) <EOL> boxcar_ones_lb = np . ones_like ( boxcar_lb ) <EOL> if len ( time_series . shape ) == <NUM_LIT:1> : <EOL> time_series = np . array ( [ time_series ] ) <EOL> for i in range ( time_series . shape [ <NUM_LIT:0> ] ) : <EOL> if ub : <EOL> pad_s = np . hstack ( ( boxcar_ones_ub * <EOL> time_series [ i , <NUM_LIT:0> ] , time_series [ i ] ) ) <EOL> pad_s = np . hstack ( ( pad_s , boxcar_ones_ub * time_series [ i , - <NUM_LIT:1> ] ) ) <EOL> for iteration in range ( n_iterations ) : <EOL> conv_s = np . convolve ( pad_s , boxcar_ub ) <EOL> time_series [ i ] = conv_s [ conv_s . shape [ - <NUM_LIT:1> ] // <NUM_LIT:2> - <EOL> int ( np . floor ( n / <NUM_LIT> ) ) : <EOL> conv_s . shape [ - <NUM_LIT:1> ] // <NUM_LIT:2> + <EOL> int ( np . ceil ( n / <NUM_LIT> ) ) ] <EOL> if lb : <EOL> pad_s = np . hstack ( ( boxcar_ones_lb * <EOL> time_series [ i , <NUM_LIT:0> ] , time_series [ i ] ) ) <EOL> pad_s = np . hstack ( ( pad_s , boxcar_ones_lb * time_series [ i , - <NUM_LIT:1> ] ) ) <EOL> for iteration in range ( n_iterations ) : <EOL> conv_s = np . convolve ( pad_s , boxcar_lb ) <EOL> s_lp = conv_s [ conv_s . shape [ - <NUM_LIT:1> ] // <NUM_LIT:2> - int ( np . floor ( n / <NUM_LIT> ) ) : <EOL> conv_s . shape [ - <NUM_LIT:1> ] // <NUM_LIT:2> + int ( np . ceil ( n / <NUM_LIT> ) ) ] <EOL> time_series [ i ] = time_series [ i ] - s_lp + np . mean ( s_lp ) <EOL> return time_series . squeeze ( ) </s>
