Repository: bokeh/bokeh

Issue:
automatically fill masked arrays when encoding
Could not resist to try cartographic mapping with bokeh...

I have tried to plot a spatial map (lon,lat) with use of plt.image and plt.line to plot continent borders.
Despite 2 problems, it is really promissing and having all this interactivity from the plot itself makes me really enthousiastic.

Attached 2 problems related to this early try:
1) plt.line does not handled missing points 
2) plt.image does not have a modulo feature (I put another issue on that point).

![map_bokeh](https://cloud.githubusercontent.com/assets/5402758/5961731/fe131d64-a7de-11e4-8076-2b69b022517b.png)



Discussion:
Hi @PBrockmann thanks for the report! During serialization, bokeh encodes missing data as `NaN` values. I can't say I am thrilled about that but I am also not sure what an alternative would be. So, it's probably possible to make the Bokeh JSON encoder smart enough to do this automatically when there are NumPy masked arrays. In the mean time, you could try converting to a regular array with `NaN`s for missing data, and missing points should be handled correctly (but let us know if there is any problem).

Indeed, I can handle this with 2 lines of numpy

```
np.ma.set_fill_value(myVar, np.nan)    # Change fill_value to Nan
myVar = myVar.filled()      # Masked values filled
```

![bokeh_map](https://cloud.githubusercontent.com/assets/5402758/5967523/20489e20-a80e-11e4-8dfe-e298bae4aedc.png)

And finally it looks nice. This try can be tested from
http://nbviewer.ipython.org/gist/PBrockmann/a6302573276ddd49a4c3

I'm glad that works! I am going to re-open this as the issue to add that automatically in our encode. 

Related: the current behavior in the presence of inf and -inf values is to ignore these, but other than with nans, there is no gap. Is this intentional?

Edit: I added `y[y>0.8] = np.inf` to the line.py example.

![index](https://cloud.githubusercontent.com/assets/3015475/6288061/ae84414e-b916-11e4-8e6f-5a3b081a069d.png)


Resolving patch:
diff --git a/bokeh/protocol.py b/bokeh/protocol.py
--- a/bokeh/protocol.py
+++ b/bokeh/protocol.py
@@ -56,6 +56,8 @@ def transform_array(self, obj):
     def transform_numerical_array(self, obj):
         """handles nans/inf conversion
         """
+        if isinstance(obj, np.ma.MaskedArray):
+            obj = obj.filled(np.nan)  # Set masked values to nan
         if not np.isnan(obj).any() and not np.isinf(obj).any():
             return obj.tolist()
         else:
diff --git a/examples/glyphs/line_missing_data.py b/examples/glyphs/line_missing_data.py
new file mode 100644
--- /dev/null
+++ b/examples/glyphs/line_missing_data.py
@@ -0,0 +1,27 @@
+# Example similar to line.py, but demoing special data
+# like masked arrays, nans, and inf
+
+import numpy as np
+
+from bokeh.plotting import *
+
+x = np.linspace(0, 4*np.pi, 200)
+y1 = np.sin(x)
+y2 = np.cos(x)
+
+# Set high/low values to inf
+y1[y1>+0.9] = +np.inf
+y1[y1<-0.9] = -np.inf
+
+# Set high values to nan and mask the low
+y2[y2>0.9] = np.nan
+y2 = np.ma.masked_array(y2, y2<-0.9)
+
+
+output_file("line_missing_data.html", title="line_missing_data.py example")
+
+p = figure(title="lines with some missing/inf values")
+p.line(x,y1, color="#2222aa", line_width=2)
+p.line(x,y2, color="#22aa22", line_width=2)
+
+show(p)
diff --git a/examples/plotting/file/line.py b/examples/plotting/file/line.py
--- a/examples/plotting/file/line.py
+++ b/examples/plotting/file/line.py
@@ -8,6 +8,6 @@
 output_file("line.html", title="line.py example")
 
 p = figure(title="simple line example")
-p.line(x,y, color="#2222aa", line_with=2)
+p.line(x,y, color="#2222aa", line_width=2)
 
 show(p)


Test patch: