Module: NexosisApi::Client::Sessions

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

Overview

Session-based API operations

Instance Method Summary collapse

Instance Method Details

#create_forecast_session(dataset_name, start_date, end_date, target_column = nil, result_interval = NexosisApi::TimeInterval::DAY, column_metadata = nil) ⇒ NexosisApi::SessionResponse

Note:

The time interval selected must be greater than or equal to the finest granularity of the data provided. For instance if your data includes many recoreds per hour, then you could request hour, day, or any other result interval. However, if your data includes only a few records per day or fewer, then a request for an hourly result interval will produce poor results.

Initiate a new forecast session based on a named dataset.

Parameters:

  • dataset_name (String)

    The name of the saved data set that has the data to forecast on.

  • start_date (DateTime)

    The starting date of the forecast period. Can be ISO 8601 string.

  • end_date (DateTime)

    The ending date of the forecast period. Can be ISO 8601 string.

  • target_column (String) (defaults to: nil)

    The name of the column for which you want predictions. Nil if defined in dataset.

  • result_interval (NexosisApi::TimeInterval) (defaults to: NexosisApi::TimeInterval::DAY)

    (optional) - The date/time interval (e.g. Day, Hour) at which predictions should be generated. So, if Hour is specified for this parameter you will get a Result record for each hour between startDate and endDate. If unspecified, we’ll generate predictions at a Day interval.

  • column_metadata (Array of NexosisApi::Column) (defaults to: nil)

    (optional) - specification for how to handle columns if different from existing metadata on dataset

Returns:

[View source]

80
81
82
# File 'lib/nexosis_api/client/sessions.rb', line 80

def create_forecast_session(dataset_name, start_date, end_date, target_column = nil, result_interval = NexosisApi::TimeInterval::DAY,  = nil)
  create_session(dataset_name, start_date, end_date, target_column, nil, 'forecast', result_interval, )
end

#create_impact_session(dataset_name, start_date, end_date, event_name, target_column = nil, result_interval = NexosisApi::TimeInterval::DAY, column_metadata = nil) ⇒ NexosisApi::SessionResponse

Analyze impact for an event with data already saved to the API.

Parameters:

  • dataset_name (String)

    The name of the saved data set that has the data to forecast on.

  • start_date (DateTime)

    The starting date of the impactful event. Can be ISO 8601 string.

  • end_date (DateTime)

    The ending date of the impactful event. Can be ISO 8601 string.

  • event_name (String)

    The name of the event.

  • target_column (String) (defaults to: nil)

    The name of the column for which you want predictions. Nil if defined in datatset.

  • result_interval (NexosisApi::TimeInterval) (defaults to: NexosisApi::TimeInterval::DAY)

    (optional) - The date/time interval (e.g. Day, Hour) at which predictions should be generated. So, if Hour is specified for this parameter you will get a Result record for each hour between startDate and endDate. If unspecified, we’ll generate predictions at a Day interval.

  • column_metadata (Array of NexosisApi::Column) (defaults to: nil)

    (optional) - specification for how to handle columns if different from existing metadata on dataset

Returns:

[View source]

94
95
96
# File 'lib/nexosis_api/client/sessions.rb', line 94

def create_impact_session(dataset_name, start_date, end_date, event_name, target_column = nil, result_interval = NexosisApi::TimeInterval::DAY,  = nil)
  create_session(dataset_name, start_date, end_date, target_column, event_name, 'impact', result_interval, )
end

#create_model(datasource_name, target_column, columns = {}, options = { prediction_domain: 'regression' }) ⇒ Object

Note:
  • classifcation assumes balanced classes. The use of a ‘balanced=false’ option

Create a new model based on a data source

indicates that no attempt should be made to sample the classes in balanced fashion.

Parameters:

  • datasource_name (String)

    The datasource from which to build the model

  • target_column (String)

    The column which will be predicted when using the model

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

    column metadata to modify roles, imputation, or target.

  • options (Hash) (defaults to: { prediction_domain: 'regression' })

    prediction_domain and or balance (true or false) indicator for classification

Since:

  • 1.3.0

[View source]

139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/nexosis_api/client/sessions.rb', line 139

def create_model(datasource_name, target_column, columns = {}, options = { prediction_domain: 'regression' })
  model_url = '/sessions/model'
  body = {
    dataSourceName: datasource_name,
    targetColumn: target_column,
    predictionDomain: options[:prediction_domain].downcase
  }
  body.store(:extraParameters, { balance: options[:balance] }) if options.include?(:balance) && body[:predictionDomain] == 'classification'
  body.store(columns: columns) unless columns.empty?
  response = self.class.post(model_url, headers: @headers, body: body.to_json)
  if response.success?
    session_hash = { 'session' => response.parsed_response }.merge(response.headers)
    NexosisApi::SessionResponse.new(session_hash)
  else
    raise HttpException.new("There was a problem creating the model session: #{response.code}.", 'creating model session' ,response)
  end
end

#get_confusion_matrix(session_id) ⇒ NexosisApi::ClassifierResult

Note:
  • This endpoint returns a 404 for requests of non-classification sessions

Get the confusion matrix for a completed classification session

Parameters:

  • session_id (String)

    The unique id of the completed classification session

Returns:

Raises:

Since:

  • 1.4.1

[View source]

162
163
164
165
166
167
# File 'lib/nexosis_api/client/sessions.rb', line 162

def get_confusion_matrix(session_id)
  result_url = "/sessions/#{session_id}/results/confusionmatrix"
  response = self.class.get(result_url, headers: @headers)
  raise HttpException.new("There was a problem getting a confusion matrix for session #{session_id}", 'getting confusion matrix', response) unless response.success?
  NexosisApi::ClassifierResult.new(response.parsed_response)
end

#get_session(session_id) ⇒ NexosisApi::Session

Get a specific session by id.

Parameters:

  • session_id (String)

    the Guid string returned in a previous session request

Returns:

Raises:

[View source]

123
124
125
126
127
128
# File 'lib/nexosis_api/client/sessions.rb', line 123

def get_session(session_id)
  session_url = "/sessions/#{session_id}"
  response = self.class.get(session_url, @options)
  return NexosisApi::Session.new(response.parsed_response) if response.success?
  raise HttpException.new("There was a problem getting the session: #{response.code}.", "getting session #{session_id}" ,response)
end

#get_session_results(session_id, as_csv = false, prediction_interval = nil) ⇒ NexosisApi::SessionResult

Get the results of the session.

Parameters:

  • session_id (String)

    the Guid string returned in a previous session request

  • as_csv (Boolean) (defaults to: false)

    indicate whether results should be returned in csv format

  • prediction_interval (Float) (defaults to: nil)

    one of the available prediction intervals for the session.

Returns:

[View source]

104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/nexosis_api/client/sessions.rb', line 104

def get_session_results(session_id, as_csv = false, prediction_interval = nil)
  session_result_url = "/sessions/#{session_id}/results"
  @headers['Accept'] = 'text/csv' if as_csv
  query = { predictionInterval: prediction_interval } unless prediction_interval.nil?
  response = self.class.get(session_result_url, headers: @headers, query: query)
  @headers.delete('Accept')

  if (response.success?)
    return response.body if as_csv
    NexosisApi::SessionResult.new(response.parsed_response)
  else
    raise HttpException.new("There was a problem getting the session: #{response.code}.", "get results for session #{session_id}" ,response)
  end
end

#list_sessions(query_options = {}, page = 0, pageSize = 50) ⇒ NexosisApi::PagedArray of NexosisApi::SessionResponse

Note:

query parameters hash members are dataset_name, event_name, requested_before_date, and requested_after_date. After and before dates refer to the session requested date.

List sessions previously submitted

Examples:

query for just one dataset

sessions = NexosisApi.client.list_sessions :dataset_name => 'MyDataset'

Parameters:

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

    optionally provide query parameters to limit the search of sessions.

  • page (Integer) (defaults to: 0)

    optionally provide a page number for paging. Defaults to 0 (first page).

  • pageSize (Integer) (defaults to: 50)

    optionally provide a page size to limit the total number of results. Defaults to 50, max 1000

Returns:

Raises:

[View source]

21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/nexosis_api/client/sessions.rb', line 21

def list_sessions(query_options = {}, page = 0, pageSize = 50)
  sessions_url = '/sessions'
  query = {
    'dataSetName' => query_options[:dataset_name],
    'eventName' => query_options[:event_name],
    'requestedAfterDate' => query_options[:requested_after_date],
    'requestedBeforeDate' => query_options[:requested_before_date],
    'type' => query_options[:type],
    'page' => page,
    'pageSize' => pageSize
  }
  response = self.class.get(sessions_url, headers: @headers, query: query)
  raise HttpException.new('Could not retrieve sessions',
                          "Get all sessions with query #{query_options}",
                          response) unless response.success?
  NexosisApi::PagedArray.new(response.parsed_response, response.parsed_response['items'].map do |session_hash|
    response_hash = { 'session' => session_hash }.merge(response.headers)
    NexosisApi::SessionResponse.new(response_hash)
  end)
end

#remove_session(session_id) ⇒ Object

Remove a session

Parameters:

  • session_id (String)

    required session identifier

Raises:

[View source]

44
45
46
47
48
49
50
51
52
# File 'lib/nexosis_api/client/sessions.rb', line 44

def remove_session(session_id)
  if (session_id.to_s.empty?)
    raise ArgumentError 'session_id cannot be empty or nil'
  end
  session_url = "/sessions/#{session_id}"
  response = self.class.delete(session_url, headers: @headers)
  return if response.success?
  raise HttpException.new('Could not delete session with given id', "remove session with id #{session_id}", response)
end

#remove_sessions(query_options = {}) ⇒ Object

Note:

query parameters hash members are type, dataset_name, event_name, start_date, and end_date. Start and end dates refer to the session requested date. Results are not removed but then can only be accessed by dataset name

Remove sessions that have been run. All query options are optional and will be used to limit the sessions removed.

Examples:

Remove all sessions based on a dataset by name

NexosisApi.client.remove_sessions :dataset_name => 'existing_dataset'

Parameters:

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

    optionally provide query parametes to limit the set of sessions removed.

Raises:

[View source]

61
62
63
64
65
66
# File 'lib/nexosis_api/client/sessions.rb', line 61

def remove_sessions(query_options = {})
  sessions_url = '/sessions'
  response = self.class.delete(sessions_url, :headers => @headers, :query => get_query_from_options(query_options))
  return if response.success?
  raise HttpException.new('Could not remove sessions', "Remove sessions with query #{query_options.to_s}",response)
end