Python — Swift Inter-op for Data Science

Nikhil Ravindra Akki
dotStar
Published in
5 min readOct 19, 2019
Python & Swift to Chandler : Can we be any more readable? ;-)

On my quest to optimize Python for speed I stumbled open Swift Lang, for those new to Swift, it is a general-purpose, multi-paradigm, object-oriented, functional, imperative and block structured language. It is built using a modern approach to safety, software design patterns by Apple Inc. (for more — link)

After having explored a few languages, I have found Swift Lang to be by far the closet to Python in terms of the syntax, the learning curve should be shallow for an intermediate Pyhon dev.

Why should we be interested in Swift ?

  • Performance : Static typed — Compiled language like Swift is in most cases faster than a Dynamic typed & interpreted language. Speed is something which means a lot when putting ML products and models to productions. Python has a few native ways to achieve performance (one of which was mentioned here)
  • Packaging : If you are building an application commercially which will be distributed to be used on private cloud or local machine, compiled language would be a natural choice.
  • Swift API for Tensorflow : I recommend that you read this article by Tensorflow team.

Well everything mentioned above can be done in Python as well and probably at a faster development time. Nevertheless, let’s look at the Python-Swift inter-op code

I’m using a Colab notebook with Swift kernel and environment (can be found here, feel free to fork it)

This notebook comes with Python as a package which can be imported in Swift.

Swift basics tip# 1 - variable or constants needs to be defined with var or a let keyword once at the start.

Import python packages using Python.import(“<package_name>”)

Swift basics tip# 2—

  • functions are defined in func () {} block with type annotations just like in Python 3.6 and above, but they are mandatory.
  • Types need to be defined for function parameters and return types (and not limited to just these, you would also need to type annotate classes, structs etc. when needed).
  • The compiler or our this case the REPL in Colab will give a nice debug message in case you missed Swift’s convention/syntax
Helper functions in Swift to integrate with Python’s Pandas and Sklearn Library

I really like coding like this as it helps remove most bugs from the start. I use Type annotations extensively in Python, it has helped me understand Swift quite effortlessly.

Last function to wrap other functions into one entry function

Swift basics tip# 3 —

Passing parameters in a function is slightly different in Swift compared to Python.

// Function definition
func foo(a: Int = 1, b: Int = 2) -> Int {
return a + b
}// Calling the function
let c = foo(a: 10, b: 20)
print(c) // will print 30
print(foo()) // will print 3
Linear & Logistic regression using Scikit-Learn from Python in Swift

I have used two demo data-sets from sci kit-learn library (Sklearn.datasets: IRIS and Diabetes data-sets).

Swift’s Python package calls the Python libraries and carry out tasks in Python and gives the result back to Swift as Python object which can be converted to Swift and used as you would in a native Swift code and vise-versa.

Benefits —

  • Use Swift for Loops, conditionals (the low hanging fruit)
  • Swift’s Python wrapper quite easily does the conversion between Python and Swift object types.
  • For ML and Data Manipulation send to Python which sends to complied layers (Pandas, Numpy, Scikit Learn and many other Compute intensive frameworks are written in C/C++/Fortran)
  • So far I have tested Basic Python Object types, Numpy, Pandas, Scikit-learn on Swift Python wrapper and works pretty well.

Challenges —

  • Swift for Data Science tasks (anything other than iOS / Apple Eco-system) is still at it’s infancy. As of this day, you may end up sending most of the Data Science tasks to Python layer while using Swift which may not be worth all the effort.
  • Moving existing code base to Swift-Python would have breaking changes, bugs which would need extra dev effort to debug and fix.
  • More time is needed for this Eco-system to mature.

Conclusion : Both languages have there stand —

  • Python is faster to code, get started with and has one of the best eco-systems in Data Science, Web development, General computing, Scripting to managing servers etc. (here’s a list of things Python is used for, this is a long list but we have used library/framework not mentioned here). Python is great, clean language with virtually no barrier for entry.
  • Swift on the other hand builds on shoulder’s of giants, is a new language and has picked best from all worlds. It is built with the open source LLVM compiler framework which helps it to be readable and fast at the same time.

Resources -

  1. Swift Tour
  2. Python Inter-op tutorial (Tensorflow)
  3. Colab Notebook from this article link,
  4. Blank Swift Colab Notebook

Articles of Cython -

  1. https://medium.com/dotstar/speed-up-python-using-cython-652dd5a27f39
  2. https://medium.com/@tzuni_eh/optimize-python-with-cython-9c36d3be83f9
  3. https://medium.com/@stefanobosisio1/cython-fast-as-gpus-without-gpus-2c2e52cc4c42
  4. https://medium.com/@xpl/protecting-python-sources-using-cython-dcd940bb188e

--

--