Module: NexosisApi::Client::Models

Included in:
NexosisApi::Client
Defined in:
lib/nexosis_api/client/models.rb

Overview

class to operate on model endpoint in Nexosis API

Since:

  • 1.3.0

Instance Method Summary collapse

Instance Method Details

#get_model(model_id) ⇒ NexosisApi::ModelSummary

Get the details of the particular model requested by id

Parameters:

  • model_id (String)

    The unique identifier for the model returned by a create-model session

Returns:

Raises:

Since:

  • 1.3.0



30
31
32
33
34
35
36
37
38
39
# File 'lib/nexosis_api/client/models.rb', line 30

def get_model(model_id)
  raise ArgumentError, 'Retrieving a model requires that model_id be specified and it is currently null.' if model_id.nil?
  model_url = "/models/#{model_id}"
  response = self.class.get(model_url, @options)
  if (response.success?)
    NexosisApi::ModelSummary.new(response.parsed_response)
  else
    raise HttpException.new("There was a problem getting your model: #{response.code}.", "Could not get model #{model_id}", response)
  end
end

#list_models(model_list_query = NexosisApi::ModelListQuery.new) ⇒ NexosisApi::PagedArray of NexosisApi::ModelSummary

Note:
  • query options dates can either be ISO 8601 compliant strings or Date objects.

List all models created in your company, optionally filtered by query parameters

Parameters:

  • model_list_query (NexosisApi::ModelListQuery) (defaults to: NexosisApi::ModelListQuery.new)

    optionally limit query results by the given parameters

Returns:

Raises:

Since:

  • 3.0.0 - all query parameters use list query class



13
14
15
16
17
18
19
20
21
22
# File 'lib/nexosis_api/client/models.rb', line 13

def list_models(model_list_query = NexosisApi::ModelListQuery.new)
  model_url = '/models'
  response = self.class.get(model_url, headers: @headers, query: model_list_query.query_parameters)
  raise HttpException.new("There was a problem listing models: #{response.code}.",
                          "listing models with data source name #{model_list_query.datasource_name}",
                          response) unless response.success?
  NexosisApi::PagedArray.new(response.parsed_response,
                             response.parsed_response['items']
                             .map { |item| NexosisApi::ModelSummary.new(item) })
end

#predict(model_id, feature_data, extra_parameters = {}) ⇒ NexosisApi::PredictResponse

Note:

The feature data shape should match that of the dataset used to create the model. Any missing features in this request will reduce the quality of the predictions.

Note:

For a classification model you may include the extra parameter ‘includeClassScores’ to get scores back for each class, not just the chosen class.

Run a feature set through the model to get predictions

Parameters:

  • model_id (String)

    unique identifier of model to use

  • feature_data (Array of Hash)

    feature columns with values to predict from

  • extra_parameters (Hash) (defaults to: {})

    name value pairs which should be included in the extraParameters property of the json body of the request.

Returns:

Raises:

Since:

  • 1.3.0



53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/nexosis_api/client/models.rb', line 53

def predict(model_id, feature_data, extra_parameters = {})
  raise ArgumentError, 'Running predictions requires that model_id be specified and it is currently empty.' if model_id.nil? || model_id.empty?
  raise ArgumentError, 'Running predictions requires that feature_data be specified and it is currently empty.' if feature_data.nil? || feature_data.empty?
  predict_url = "/models/#{model_id}/predict"
  feature_data = [feature_data] unless feature_data.kind_of?(Array)
  response = self.class.post(predict_url, headers: @headers, body: { 'data': feature_data, 'extraParameters': extra_parameters }.to_json)
  if (response.success?)
    NexosisApi::PredictResponse.new(model_id, response.parsed_response)
  else
    raise HttpException.new("There was a problem predicting from your model: #{response.code}.",
                            "Could not start predict for #{model_id}",
                            response)
  end
end

#remove_model(model_id) ⇒ Object

Remove an existing model

Parameters:

  • model_id (String)

    the unique id of the model to remove.

Raises:

Since:

  • 1.3.0



73
74
75
76
77
78
79
80
81
82
# File 'lib/nexosis_api/client/models.rb', line 73

def remove_model(model_id)
  raise ArgumentError, 'Deleting a model requires that model_id be specified and it is currently empty.' if model_id.nil? || model_id.empty?
  delete_url = "/models/#{model_id}"
  response = self.class.delete(delete_url, @options)
  unless (response.success?)
    raise HttpException.new("There was a problem deleting your model: #{response.code}.",
                            "Could not delete #{model_id}",
                            response)
  end
end

#remove_models(datasource_name = nil, begin_date = nil, end_date = nil) ⇒ Object

Note:
  • Use with great care. This permanently removes trained models.

All parameters are indepdently optional, but one must be sent.

Deletes multiple models based on the provided filter criteria.

Parameters:

  • datasource_name (String) (defaults to: nil)

    remove all models created by this datasource

  • begin_date (DateTime) (defaults to: nil)

    remove all models created after this date/time - inclusive. May be a ISO 8601 compliant string.

  • end_date (DateTime) (defaults to: nil)

    remove all models created before this date/time - inclusive. May be a ISO 8601 compliant string.

Raises:

Since:

  • 1.3.0



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/nexosis_api/client/models.rb', line 92

def remove_models(datasource_name = nil, begin_date = nil, end_date = nil)
  params_unset = datasource_name.nil?
  params_unset &= begin_date.nil?
  params_unset &= end_date.nil?
  raise ArgumentError, 'Must set one of the method parameters.' if params_unset
  delete_url = '/models'
  query = {}
  query.store('dataSourceName', datasource_name) unless datasource_name.nil?
  query.store('createdAfterDate', begin_date) unless begin_date.nil?
  query.store('createdBeforeDate', end_date) unless end_date.nil?
  response = self.class.delete(delete_url, headers: @headers, query: query)
  unless (response.success?)
    raise HttpException.new("There was a problem deleting your models: #{response.code}.",
                            'Could not delete models',
                            response)
  end
end