Announcing BigML’s SDK for Swift

Posted by

The last month of 2015 has seen two significant announcements concerning the Swift programming language. On the one hand, Apple has finally fulfilled their promise to make its source code available under an open source license; on the other, Swift has surpassed Objective-C on the TIOBE index. Thus we are especially proud to announce our SDK for Swift, bigml-swift, which evolves and supersedes the old BigMLKitConnector library.

Apple Swift

Our BigML SDK for Swift will provide all iOS, OS X, watchOS, and tvOS developers with the possibility of integrating the BigML platform into their apps while also benefitting from Swift’s type safety, modern syntax, and performance.

The main features that BigML SDK for Swift provides can be divided into two areas:

  • Remote resource processing: BigML SDK exposes BigML’s REST API through a higher-level Swift API that will make is easier for you to create, retrieve, update, and delete remote resources. Supported resources are:

    • data sources
    • datasets
    • models
    • clusters
    • anomalies
    • ensembles
    • predictions,
    • centroids,
    • anomaly scores.
  • Local resource processing: BigML SDK allows you to mix local and remote distributed processing in a seamless and transparent way. You will be able to download your remote resources (e.g. a cluster) and then apply supported algorithms to them (e.g. calculate its nearest centroid based on your input data). This is one definite advantages that BigML offers in comparison to competing services, which mostly bind you into either using their remote services or do everything locally. BigML’s SDK for Swift combines the benefits of both approaches by making it possible to use the power of a cloud solution and to enjoy the flexibility/transparency of local processing right when you need it. The following is a list of currently supported algorithms that BigML’s SDK for Swift provides:

    • Model predictions
    • Ensemble predictions
    • Clustering
    • Anomaly detections.

A dive into BigML’s Swift API

The BMLConnector class is the workhorse of all remote processing: it allows you to create, delete, and get remote resource of any supported type. When instantiating it, you should provide your BigML’s account credentials and specify whether you want to work in development or production mode:

let connector = BMLConnector(username:"your-username-here",
                               apiKey:"your-api-key-here",
                                 mode:BMLMode.Development)

Once you connector is instantiated, you can use it to create a data source from a local CSV file:

let myDatasource : BMLMinimalResource
let filePath : String = ...
let source = BMLMinimalResource(name:"My Data source",
    type:BMLResourceType.File,
    uuid:filePath!)
    
self.connector!.createResource(BMLResourceType.Source,
     name: "testCreateDatasource",
     options: [:],
     from: source) { (resource, error) -> Void in
          if let resource = resource {
              myDatasource = resource!
          }
}

As you can see, BMLConnector’s createResource provides a strongly typed interface, which allows you to specify the type of resource you want to create, its name, a set of options, and the resource that should be used to create it.

BigML’s SDK for Swift API is entirely asynchronous and relies on completion blocks, where you will get the resource that has been created, if any, or the error that aborted the operation as applicable. The resource you will receive in the completion block is an instance of the BMLMinimalResource type, which conforms to the BMLResource protocol.

The BMLResource protocol encodes the most basic information that all resources share: a name, a type, a UUID, the resource’s current status, and a JSON object that describes all of its properties. You are supposed to create your own custom class that conforms to the BMLResource protocol and that best suits your needs e.g., it might be a Core Data class that allows you to persist your resource to a local cache. Of course you are welcome to reuse our BMLMinimalResource implementation as you wish.

In a pretty similar way you can create a dataset from the data source just created:

self.connector!.createResource(BMLResourceType.Dataset,
     name: "My first dataset",
     options: [:],
     from: myDatasource) { (resource, error) -> Void in
                   
         //-- your processing of the dataset here
}

If you know the UUID of an existing resource of a given type and want to retrieve it from BigML, you can use BMLConnector’s getResource method:

self.connector!.getResource(BMLResourceType.Model, uuid: modelId) {
     (resource, error) -> Void in

     if let model = resource {
     ...
     }
}

Local algorithms

The most exciting part of BigML’s SDK for Swift is surely its support for a collection of the most widely used ML algorithms such as model prediction, clustering, anomaly detection etc. What is even more exciting is that the family of algorithms that BigML’s SDK for Swift supports is constantly growing!

As an example, say that you have a model in your BigML account and that you want to use it to make a prediction based on some set of data that you have got. This is a two step process:

  • retrieve the model from your account, as shown above, with getResource;
  • use BigML’s SDK for Swift to calculate a prediction locally.

The second step can be executed inside of the completion block that you pass to getResource. This could look like the following:

self.connector!.getResource(BMLResourceType.Model, uuid: modelId) {
(resource, error) -> Void in
if let model = resource {

    let pModel = Model(jsonModel: model.jsonDefinition)
    let prediction = pModel.predict([
            "sepal width": 3.15,
            "petal length": 4.07,
            "petal width": 1.51],
        options: ["byName" : true])
    }
}

The prediction object returned is a dictionary containing the value of the prediction and its confidence. In similar ways, you can calculate the nearest centroid, or do anomaly scoring.

Practical Info

The BigML SDK for Swift is compatible with Swift 2.1.1. You can fork BigML’s SDK for Swift from BigML’s GitHub account and send us your PRs. As always, let us know what you think about it and how we can improve it to better suit your requirements!

2 comments

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s