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:

  • (ArgumentError)

Since:

  • 1.3.0

[View source]

37
38
39
40
41
42
43
44
45
46
# File 'lib/nexosis_api/client/models.rb', line 37

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(datasource_name = nil, page = 0, page_size = 50, query_options = {}) ⇒ 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

models created for this data source name.

Parameters:

  • datasource_name (String) (defaults to: nil)

    optionally limit to those

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

    limit by dates: begin_date and/or end_date

Returns:

Raises:

Since:

  • 1.3.0

[View source]

13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/nexosis_api/client/models.rb', line 13

def list_models(datasource_name = nil, page = 0, page_size = 50, query_options = {})
  model_url = '/models'
  query = {
    page: page,
    pageSize: page_size
  }
  unless query_options.empty?
    query.store('createdBeforeDate', query_options['end_date']) unless query_options['end_date'].nil?
    query.store('createdAfterDate', query_options['begin_date']) unless query_options['begin_date'].nil?
  end
  query.store(dataSourceName: datasource_name) unless datasource_name.nil?
  response = self.class.get(model_url, headers: @headers, query: query)
  raise HttpException.new("There was a problem listing models: #{response.code}.",
                          "listing models with data source name #{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) ⇒ NexosisApi::PredictResponse

Note:

The feature data shape should match that of the dataset used to create the model.

Run a feature set through the model to get predictions

Any missing features in this request will reduce the quality of the predictions.

Parameters:

  • model_id (String)

    unique identifier of model to use

  • feature_data (Array of Hash)

    feature columns with values to predict from

Returns:

Raises:

  • (ArgumentError)

Since:

  • 1.3.0

[View source]

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

def predict(model_id, feature_data)
  raise ArgumentError, 'Running predictions requires that model_id be specified and it is currently empty.' if model_id.empty?
  raise ArgumentError, 'Running predictions requires that feature_data be specified and it is currently empty.' if feature_data.empty?
  predict_url = "/models/#{model_id}/predict"
  response = self.class.post(predict_url, headers: @headers, body: { "data": feature_data }.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:

  • (ArgumentError)

Since:

  • 1.3.0

[View source]

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

def remove_model(model_id)
  raise ArgumentError, 'Deleting a model requires that model_id be specified and it is currently empty.' if 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.

Deletes multiple models based on the provided filter criteria. All parameters are indepdently optional, but one must be sent.

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:

  • (ArgumentError)

Since:

  • 1.3.0

[View source]

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

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