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
-
#create_anomalies_model(datasource_name, columns = {}, data_contains_anomalies = true) ⇒ NexosisApi::SessionResponse
Create a new model based on a data source.
-
#create_classification_model(datasource_name, target_column, columns = {}, balance = true) ⇒ NexosisApi::SessionResponse
Create a new model based on a data source.
-
#create_forecast_session(dataset_name, start_date, end_date, target_column = nil, result_interval = NexosisApi::TimeInterval::DAY, column_metadata = nil) ⇒ NexosisApi::SessionResponse
Initiate a new forecast session based on a named dataset.
-
#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.
-
#create_model(datasource_name, target_column, columns = {}, options = { prediction_domain: 'regression' }) ⇒ NexosisApi::SessionResponse
Create a new model based on a data source.
-
#get_anomaly_scores(session_id, page = 0, page_size = 50) ⇒ NexosisApi::ScoreResult
Get the observation anomaly score for a completed anomalies session.
-
#get_class_scores(session_id, page_number = 0, page_size = 50) ⇒ NexosisApi::ScoreResult
Get the observation class score for a completed classification session.
-
#get_confusion_matrix(session_id) ⇒ NexosisApi::ClassifierResult
Get the confusion matrix for a completed classification session.
-
#get_distance_metrics(session_id, page_number = 0, page_size = 50) ⇒ NexosisApi::AnomalyDistances
Get per result distance metric for anomaly detection sessions.
-
#get_feature_importance(session_id, page_number = 0, page_size = 50) ⇒ NexosisApi::FeatureImportance
Get a feature importance score for each column in the data source used in a session that can return internal scores you need to use the champion endpoints.
-
#get_session(session_id) ⇒ NexosisApi::Session
Get a specific session by id.
-
#get_session_result_data(session_id, page = 0, page_size = 50, options = {}) ⇒ NexosisApi::SessionResult
Get the results of the session.
- #get_session_results(session_id, as_csv = false, prediction_interval = nil) ⇒ NexosisApi::SessionResult deprecated Deprecated.
- #get_timeseries_outliers(session_id, page_number = 0, page_size = 50) ⇒ Object
-
#list_sessions(session_list_query = NexosisApi::SessionListQuery.new) ⇒ NexosisApi::PagedArray of NexosisApi::SessionResponse
List sessions previously submitted.
-
#remove_session(session_id) ⇒ Object
Remove a session.
-
#remove_sessions(query_options = {}) ⇒ Object
Remove sessions that have been run.
Instance Method Details
#create_anomalies_model(datasource_name, columns = {}, data_contains_anomalies = true) ⇒ NexosisApi::SessionResponse
The anomalies model session results will contain the anomalous observations when the session is complete.
Create a new model based on a data source
179 180 181 182 183 184 185 186 187 188 189 190 191 192 |
# File 'lib/nexosis_api/client/sessions.rb', line 179 def create_anomalies_model(datasource_name, columns = {}, data_contains_anomalies = true) model_url = '/sessions/model' body = { dataSourceName: datasource_name, predictionDomain: 'anomalies', extraParameters: { 'containsAnomalies': data_contains_anomalies }, columns: columns } response = self.class.post(model_url, headers: @headers, body: body.to_json) raise HttpException.new("There was a problem creating the model session: #{response.code}.", 'creating anomaly model session' ,response) unless response.success? NexosisApi::SessionResponse.new(response.parsed_response.merge(response.headers)) end |
#create_classification_model(datasource_name, target_column, columns = {}, balance = true) ⇒ NexosisApi::SessionResponse
The anomalies model session results will contain the anomalous observations when the session is complete.
Create a new model based on a data source
204 205 206 |
# File 'lib/nexosis_api/client/sessions.rb', line 204 def create_classification_model(datasource_name, target_column, columns = {}, balance = true) return create_model(datasource_name, target_column, columns, prediction_domain: 'classification', balance: balance) end |
#create_forecast_session(dataset_name, start_date, end_date, target_column = nil, result_interval = NexosisApi::TimeInterval::DAY, column_metadata = nil) ⇒ NexosisApi::SessionResponse
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.
75 76 77 |
# File 'lib/nexosis_api/client/sessions.rb', line 75 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.
89 90 91 |
# File 'lib/nexosis_api/client/sessions.rb', line 89 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' }) ⇒ NexosisApi::SessionResponse
-
classifcation assumes balanced classes. The use of a ‘balance=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.
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 |
# File 'lib/nexosis_api/client/sessions.rb', line 152 def create_model(datasource_name, target_column, columns = {}, = { prediction_domain: 'regression' }) model_url = '/sessions/model' body = { dataSourceName: datasource_name, targetColumn: target_column, predictionDomain: [:prediction_domain].downcase } body.store(:extraParameters, { balance: [:balance] }) if .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_anomaly_scores(session_id, page = 0, page_size = 50) ⇒ NexosisApi::ScoreResult
-
This endpoint returns a 404 for requests of non-anomalies sessions
Get the observation anomaly score for a completed anomalies session
227 228 229 230 231 232 233 234 235 236 |
# File 'lib/nexosis_api/client/sessions.rb', line 227 def get_anomaly_scores(session_id, page = 0, page_size = 50) score_url = "/sessions/#{session_id}/results/anomalyScores" query = { page: page, pageSize: page_size } response = self.class.get(score_url, headers: @headers, query: query) raise HttpException.new("There was a problem getting the anomaly scores for session #{session_id}", 'getting anomaly scores', response) unless response.success? NexosisApi::AnomalyScores.new(response.parsed_response.merge(response.headers)) end |
#get_class_scores(session_id, page_number = 0, page_size = 50) ⇒ NexosisApi::ScoreResult
-
This endpoint returns a 404 for requests of non-classification sessions
Get the observation class score for a completed classification session
246 247 248 249 250 251 252 253 254 255 |
# File 'lib/nexosis_api/client/sessions.rb', line 246 def get_class_scores(session_id, page_number = 0, page_size = 50) score_url = "/sessions/#{session_id}/results/classScores" query = { page: page_number, pageSize: page_size } response = self.class.get(score_url, headers: @headers, query: query) raise HttpException.new("There was a problem getting the class scores for session #{session_id}", 'getting class scores', response) unless response.success? NexosisApi::ClassifierScores.new(response.parsed_response.merge(response.headers)) end |
#get_confusion_matrix(session_id) ⇒ NexosisApi::ClassifierResult
-
This endpoint returns a 404 for requests of non-classification sessions
Get the confusion matrix for a completed classification session
214 215 216 217 218 219 |
# File 'lib/nexosis_api/client/sessions.rb', line 214 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.merge(response.headers)) end |
#get_distance_metrics(session_id, page_number = 0, page_size = 50) ⇒ NexosisApi::AnomalyDistances
This method will return 404 when the session for the given session id does not have a prediction domain of ‘anomalies’
Get per result distance metric for anomaly detection sessions.
285 286 287 288 289 290 291 292 293 294 |
# File 'lib/nexosis_api/client/sessions.rb', line 285 def get_distance_metrics(session_id, page_number = 0, page_size = 50) distance_url = "/sessions/#{session_id}/results/mahalanobisdistances" query = { page: page_number, pageSize: page_size } response = self.class.get(distance_url, headers: @headers, query: query) raise HttpException.new("There was a problem getting the distance metrics for session #{session_id}", 'getting distance metrics', response) unless response.success? NexosisApi::AnomalyDistances.new(response.parsed_response.merge(response.headers)) end |
#get_feature_importance(session_id, page_number = 0, page_size = 50) ⇒ NexosisApi::FeatureImportance
The score returned here is calculated before algorithm selection and so is not algorithm specific. For algorithms
Those sessions which will provide scores have the supports_feature_importance attribute set True. Others will 404 at this endpoint.
Get a feature importance score for each column in the data source used in a session that can return internal scores you need to use the champion endpoints.
266 267 268 269 270 271 272 273 274 275 |
# File 'lib/nexosis_api/client/sessions.rb', line 266 def get_feature_importance(session_id, page_number = 0, page_size = 50) importance_url = "/sessions/#{session_id}/results/featureimportance" query = { page: page_number, pageSize: page_size } response = self.class.get(importance_url, headers: @headers, query: query) raise HttpException.new("There was a problem getting the feature importance scores for session #{session_id}", 'getting feature importance', response) unless response.success? NexosisApi::FeatureImportance.new(response.parsed_response.merge(response.headers)) end |
#get_session(session_id) ⇒ NexosisApi::Session
Get a specific session by id.
134 135 136 137 138 139 |
# File 'lib/nexosis_api/client/sessions.rb', line 134 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_result_data(session_id, page = 0, page_size = 50, options = {}) ⇒ NexosisApi::SessionResult
Get the results of the session.
114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/nexosis_api/client/sessions.rb', line 114 def get_session_result_data(session_id, page = 0, page_size = 50, = {}) session_result_url = "/sessions/#{session_id}/results" @headers['Accept'] = 'text/csv' if [:as_csv] query = { 'page' => page, 'pageSize' => page_size } query.store('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 |
#get_session_results(session_id, as_csv = false, prediction_interval = nil) ⇒ NexosisApi::SessionResult
Get the results of the session.
101 102 103 |
# File 'lib/nexosis_api/client/sessions.rb', line 101 def get_session_results(session_id, as_csv = false, prediction_interval = nil) get_session_result_data(session_id, 0, 50, {as_csv: as_csv, prediction_interval: prediction_interval}) end |
#get_timeseries_outliers(session_id, page_number = 0, page_size = 50) ⇒ Object
296 297 298 299 300 301 302 303 304 305 |
# File 'lib/nexosis_api/client/sessions.rb', line 296 def get_timeseries_outliers(session_id, page_number = 0, page_size = 50) outlier_url = "/sessions/#{session_id}/results/outliers" query = { page: page_number, pageSize: page_size } response = self.class.get(outlier_url, headers: @headers, query: query) raise HttpException.new("There was a problem getting the outliers for session #{session_id}", 'getting outliers', response) unless response.success? NexosisApi::TimeseriesOutliers.new(response.parsed_response.merge(response.headers)) end |
#list_sessions(session_list_query = NexosisApi::SessionListQuery.new) ⇒ NexosisApi::PagedArray of NexosisApi::SessionResponse
query parameters hash members are dataset_name, event_name, model_id, sort_by, sort_order, requested_before_date, and requested_after_date. After and before dates refer to the session requested date.
List sessions previously submitted
22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/nexosis_api/client/sessions.rb', line 22 def list_sessions(session_list_query = NexosisApi::SessionListQuery.new) sessions_url = '/sessions' response = self.class.get(sessions_url, headers: @headers, query: session_list_query.query_parameters) raise HttpException.new('Could not retrieve sessions', "Get all sessions with query #{session_list_query.query_parameters}", 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
38 39 40 41 42 43 44 45 46 |
# File 'lib/nexosis_api/client/sessions.rb', line 38 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
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.
56 57 58 59 60 61 |
# File 'lib/nexosis_api/client/sessions.rb', line 56 def remove_sessions( = {}) sessions_url = '/sessions' response = self.class.delete(sessions_url, :headers => @headers, :query => ()) return if response.success? raise HttpException.new('Could not remove sessions', "Remove sessions with query #{.to_s}",response) end |