import numpy as np
import vtk.numpy_interface.dataset_adapter as dsa

def get_scalars(dataobject):
    do = dsa.WrapDataObject(dataobject)
    # get the first
    rawarray = do.PointData.GetScalars()
    vtkarray = dsa.vtkDataArrayToVTKArray(rawarray, do)
    vtkarray.Association = dsa.ArrayAssociation.POINT
    return vtkarray


def set_scalars(dataobject, newscalars):
    do = dsa.WrapDataObject(dataobject)
    oldscalars = do.PointData.GetScalars()
    name = oldscalars.GetName()
    del oldscalars
    do.PointData.append(newscalars, name)
    do.PointData.SetActiveScalars(name)

import numpy as np
import vtk.numpy_interface.dataset_adapter as dsa
import vtk.util.numpy_support as np_s

def get_scalars(dataobject):
    do = dsa.WrapDataObject(dataobject)
    # get the first
    rawarray = do.PointData.GetScalars()
    vtkarray = dsa.vtkDataArrayToVTKArray(rawarray, do)
    vtkarray.Association = dsa.ArrayAssociation.POINT
    return vtkarray


def set_scalars(dataobject, newscalars):
    do = dsa.WrapDataObject(dataobject)
    oldscalars = do.PointData.GetScalars()
    name = oldscalars.GetName()
    del oldscalars

    # handle the case if the newscalars array has a type that
    # cannot be passed on to VTK. In which case, we convert to
    # convert to float64
    vtk_typecode = np_s.get_vtk_array_type(newscalars.dtype)
    if vtk_typecode is None:
        newscalars = newscalars.astype(np.float64)
    do.PointData.append(newscalars, name)
    do.PointData.SetActiveScalars(name)

Fix numpy related errors on Mavericks.
Fix numpy related errors on Mavericks.

The problem was due to the fact that operations (like sqrt) can return a
float16 arrays which cannot be passed back to VTK directly. Added a
temporary conversion to float64. We should potentially handle this in
VTK.

  import numpy as np
  import vtk.numpy_interface.dataset_adapter as dsa
+ import vtk.util.numpy_support as np_s
  
  def get_scalars(dataobject):
      do = dsa.WrapDataObject(dataobject)
      # get the first
      rawarray = do.PointData.GetScalars()
      vtkarray = dsa.vtkDataArrayToVTKArray(rawarray, do)
      vtkarray.Association = dsa.ArrayAssociation.POINT
      return vtkarray
  
  
  def set_scalars(dataobject, newscalars):
      do = dsa.WrapDataObject(dataobject)
      oldscalars = do.PointData.GetScalars()
      name = oldscalars.GetName()
      del oldscalars
+ 
+     # handle the case if the newscalars array has a type that
+     # cannot be passed on to VTK. In which case, we convert to
+     # convert to float64
+     vtk_typecode = np_s.get_vtk_array_type(newscalars.dtype)
+     if vtk_typecode is None:
+         newscalars = newscalars.astype(np.float64)
      do.PointData.append(newscalars, name)
      do.PointData.SetActiveScalars(name)
  
Fix numpy related errors on Mavericks.
## Code Before:
import numpy as np
import vtk.numpy_interface.dataset_adapter as dsa

def get_scalars(dataobject):
    do = dsa.WrapDataObject(dataobject)
    # get the first
    rawarray = do.PointData.GetScalars()
    vtkarray = dsa.vtkDataArrayToVTKArray(rawarray, do)
    vtkarray.Association = dsa.ArrayAssociation.POINT
    return vtkarray


def set_scalars(dataobject, newscalars):
    do = dsa.WrapDataObject(dataobject)
    oldscalars = do.PointData.GetScalars()
    name = oldscalars.GetName()
    del oldscalars
    do.PointData.append(newscalars, name)
    do.PointData.SetActiveScalars(name)

## Instruction:
Fix numpy related errors on Mavericks.
## Code After:
import numpy as np
import vtk.numpy_interface.dataset_adapter as dsa
import vtk.util.numpy_support as np_s

def get_scalars(dataobject):
    do = dsa.WrapDataObject(dataobject)
    # get the first
    rawarray = do.PointData.GetScalars()
    vtkarray = dsa.vtkDataArrayToVTKArray(rawarray, do)
    vtkarray.Association = dsa.ArrayAssociation.POINT
    return vtkarray


def set_scalars(dataobject, newscalars):
    do = dsa.WrapDataObject(dataobject)
    oldscalars = do.PointData.GetScalars()
    name = oldscalars.GetName()
    del oldscalars

    # handle the case if the newscalars array has a type that
    # cannot be passed on to VTK. In which case, we convert to
    # convert to float64
    vtk_typecode = np_s.get_vtk_array_type(newscalars.dtype)
    if vtk_typecode is None:
        newscalars = newscalars.astype(np.float64)
    do.PointData.append(newscalars, name)
    do.PointData.SetActiveScalars(name)
