Until recently, Slicer used a custom Python build that was not binary compatible with third-party Python packages. Now, in latest Slicer Preview release, any Python packages can be installed and used in Slicer (except those that in direct conflict with version of libraries that are bundled with Slicer).
Example: do curve fitting using scipy
- Install scipy by typing in Slicer’s Python console:
pip_install('scipy')
- Try a simple curve fitting example:
import numpy as np
from scipy.optimize import curve_fit
def func(x, a, b, c):
return a * np.exp(-b * x) + c
# Define the data to be fit with some noise:
xdata = np.linspace(0, 4, 50)
y = func(xdata, 2.5, 1.3, 0.5)
np.random.seed(1729)
y_noise = 0.2 * np.random.normal(size=xdata.size)
ydata = y + y_noise
# Fit for the parameters a, b, c of the function func:
popt, pcov = curve_fit(func, xdata, ydata)
# Plot results
plotNodes = {}
plot([xdata, ydata, func(xdata, *popt)], xColumnIndex = 0, nodes = plotNodes)
plotNodes['chart'].SetTitle('Curve fit demo')
plotNodes['series'][0].SetName('noisy')
plotNodes['series'][1].SetName('fit: a=%5.3f, b=%5.3f, c=%5.3f' % tuple(popt))
This feature will be heavily utilized in the upcoming Slicer tutorial “Deep learning and computer vision for real-time procedure annotation” at CARS conference next week, where tensorflow, keras, scikit-learn pacakges are used in Slicer for classification of images streamed in real-time.
Any questions, comments, suggestions are welcome.