✊🏿 Black Lives Matter. Please consider donating to Black Girls Code today.
⚾️ It's finally Baseball season! Root for the home team... & Register for our Sports Analytics Webinar!

FFT Filters in Python

I’ve been using a Plot.ly example for FFT and Filters.

https://plot.ly/python/fft-filters/

Two errors consistently pop up in the sample code:

IndexError                                Traceback (most recent call last)
<ipython-input-46-bd63162e75b1> in <module>()
     16 hhpf = hhpf / np.sum(hhpf)
     17 hhpf = -hhpf
---> 18 hhpf[(N - 1) / 2] += 1
     19 
     20 h = np.convolve(hlpf, hhpf)

IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are valid indices

This is fixed if I modify the code with the error to:

---> 18 hhpf[int((N - 1) / 2)] += 1

The second error is:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-50-adc0392cb73a> in <module>()
39 trace_data = [trace1]
40 fig = go.Figure(data=trace_data, layout=layout)
---> 41 py.iplot(fig, filename='fft-band-pass-filter')

~/anaconda3/lib/python3.6/site-packages/plotly/plotly/plotly.py in iplot(figure_or_data, **plot_options)
138     if 'auto_open' not in plot_options:
139         plot_options['auto_open'] = False
--> 140     url = plot(figure_or_data, **plot_options)
141 
142     if isinstance(figure_or_data, dict):

~/anaconda3/lib/python3.6/site-packages/plotly/plotly/plotly.py in plot(figure_or_data, validate, 
**plot_options)
231     data = fig.get('data', [])
232     plot_options['layout'] = fig.get('layout', {})
--> 233     response = v1.clientresp(data, **plot_options)
234 
235     # Check if the url needs a secret key

~/anaconda3/lib/python3.6/site-packages/plotly/api/v1/clientresp.py in clientresp(data, **kwargs)
27     payload = {
28         'platform': 'python', 'version': version.__version__,
---> 29         'args': _json.dumps(data, **dumps_kwargs),
30         'un': creds['username'], 'key': creds['api_key'], 'origin': 'plot',
31         'kwargs': _json.dumps(kwargs, **dumps_kwargs)

~/anaconda3/lib/python3.6/json/__init__.py in dumps(obj, skipkeys, ensure_ascii, check_circular, 
allow_nan, cls, indent, separators, default, sort_keys, **kw)
236         check_circular=check_circular, allow_nan=allow_nan, indent=indent,
237         separators=separators, default=default, sort_keys=sort_keys,
--> 238         **kw).encode(obj)
239 
240 

~/anaconda3/lib/python3.6/site-packages/plotly/utils.py in encode(self, o)
134 
135         # this will raise errors in a normal-expected way
--> 136         encoded_o = super(PlotlyJSONEncoder, self).encode(o)
137 
138         # now:

~/anaconda3/lib/python3.6/json/encoder.py in encode(self, o)
197         # exceptions aren't as detailed.  The list call should be roughly
198         # equivalent to the PySequence_Fast that ''.join() would do.
--> 199         chunks = self.iterencode(o, _one_shot=True)
200         if not isinstance(chunks, (list, tuple)):
201             chunks = list(chunks)

~/anaconda3/lib/python3.6/json/encoder.py in iterencode(self, o, _one_shot)
255                 self.key_separator, self.item_separator, self.sort_keys,
256                 self.skipkeys, _one_shot)
--> 257         return _iterencode(o, 0)
258 
259 def _make_iterencode(markers, _default, _encoder, _indent, _floatstr,

~/anaconda3/lib/python3.6/site-packages/plotly/utils.py in default(self, obj)
202             except NotEncodable:
203                 pass
--> 204         return _json.JSONEncoder.default(self, obj)
205 
206     @staticmethod

~/anaconda3/lib/python3.6/json/encoder.py in default(self, o)
178         """
179         raise TypeError("Object of type '%s' is not JSON serializable" %
--> 180                         o.__class__.__name__)
181 
182     def encode(self, o):

TypeError: Object of type 'range' is not JSON serializable

This error is fixed if I add a list() around the x data:
x=list(range(len(new_signal))),

My question is has something changed in Plot.ly that this code no longer works. What am I doing wrong when I can’t get it to run as it is published.