ibm-ml 0.2.0 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8043c7c62f75f733b6e6b714c9dd3cd9a231c418
4
- data.tar.gz: 2e9cc28ef1ac51cb993785890ae4ebbf95e7e3c7
3
+ metadata.gz: 9181a59f405e92671980ea2483c050acece5a3db
4
+ data.tar.gz: c260419693c752f3e050031e1005de5da5a62177
5
5
  SHA512:
6
- metadata.gz: 42bfc1730006f18327265ec024d5427edc895938d90a07c73c1c33353ea07fb2d88b31a937959df2b1bf1672c0f8bda867cb08b400f9317321a75cc423fdfa76
7
- data.tar.gz: e13d5dfd1365ecae5acffdd8e2223abb07e876c7412206ffe8a63cab713d96bf5d93db0a72a0ffb84dd8d945111aa8ddc62c612150d6adcb1c60ee31581d293e
6
+ metadata.gz: 96b92a8a2e4af5bd444a689975676e6fc4c7d849863eaae472806a9fd03e69dc630420ed0fb19acad635dca5a3a8b5c2e4e337837b30e85dececf48098c90987
7
+ data.tar.gz: 55a34c8a5899d2986ea0c637cc5a2e9e33bf14096c76e0e9e17dd5b069a805245f77b9ac3bec6fa04990a0bf0082272e05f04db0bb3c91f934fb17fff2ecdbdb
@@ -12,3 +12,5 @@ deploy:
12
12
  gem: ibm-ml
13
13
  api_key:
14
14
  secure: KrW9QsWC2deO9ubwZxQSRcbL8Azj4xkE0KpY30B564/o3qJOFZN98cevVJ7EujyxA+HVXWsdvEsr+PHk17fCSbSlbnmcRS0C7BSy7RCNWgHhDd2hiUG32QXszpr5ZFvXkzlZ9N5gNRzlIpOVckQS2swYCt20GNTn6XQ69a9j7wMER68bDT4gWCkq+xssioS5PaE1hFqOvPDXRltigIz3cZz8+8UDnPzprTyyJ235KNktEFs4OUWGrDwfbgoJs3K744+PvYyTxw+xenlZ6huZqEYmYAws/okIPKenQ/qiTBOx4RRRkVOXWzZ4kUexJiAdIoxKunZ1bPjTtip3AKa1nu3FdgJqq3DZxxX5wftKyeeZYyOoZs+e24Vi79JGXSRcHpMDi578nd5Fqu1UU6weTjS11DHhxX2rPNYRBcvCwXxKCnrw3IY8bzhQ2cF3K1PTV6g6RCXqEY/IKqex6RUw+dcPxqFNaH5VIgyDxs3TUUkEUmsGfHZmyz1MXNjnrLJRQki+H25LpHhOP/lcwJZuLjwNca5uBp01M0R89gsumLVvI5fxpteWjFMUTMZ397sC6Ve0/PmggYxuwLre1nT1N8MMVX79oHXwxeOHltw9lcR4wOc7oIPX8BzgtLvnCvsha9AHXP8F0n0u2F8UrJr25sBQ0CXLpTeTG+zG2t63L5s=
15
+ on:
16
+ rvm: 2.4.0
data/README.md CHANGED
@@ -10,14 +10,14 @@ Currently supports:
10
10
 
11
11
  ## Installation
12
12
 
13
- #### With Gem
13
+ ### With Gem
14
14
  After [installing Ruby](https://www.ruby-lang.org/en/documentation/installation/) >= 2.0:
15
15
 
16
16
  ```bash
17
17
  $ gem install ibm-ml
18
18
  ```
19
19
 
20
- #### With Bundler
20
+ ### With Bundler
21
21
  Add this line to your application's Gemfile:
22
22
 
23
23
  ```ruby
@@ -32,12 +32,12 @@ $ bundle install
32
32
 
33
33
  ## Usage
34
34
 
35
- #### Setup
35
+ ### Setup
36
36
  ```ruby
37
37
  require 'ibm/ml'
38
38
  require 'pp'
39
39
 
40
- # example input record to score
40
+ # input record to score
41
41
  record = {
42
42
  GENDER: 'M',
43
43
  AGEGROUP: '45-54',
@@ -50,30 +50,53 @@ record = {
50
50
  }
51
51
  ```
52
52
 
53
- #### Cloud
53
+ ### Cloud
54
54
  ```ruby
55
- USERNAME = # ML service username
56
- PASSWORD = # ML service password
57
- MODEL_ID = # model ID
58
- DEPLOYMENT_ID = # deployment ID
59
-
60
- service = IBM::ML::Cloud.new(USERNAME, PASSWORD)
61
- pp service.fetch_token
62
- pp service.published_models
63
- pp service.deployments
64
- pp service.get_score(MODEL_ID, DEPLOYMENT_ID, record)
55
+ CLOUD_USERNAME = # WML service username
56
+ CLOUD_PASSWORD = # WML service password
57
+ DEPLOYMENT_ID = # deployment ID
58
+
59
+ # Create the service object
60
+ ml_service = IBM::ML::Cloud.new(CLOUD_USERNAME, CLOUD_PASSWORD)
61
+
62
+ # Fetch an authentication token
63
+ pp ml_service.fetch_token
64
+
65
+ # Query models
66
+ pp ml_service.models
67
+ pp ml_service.model_by_name('ML Model')
68
+
69
+ # Query deployments
70
+ pp ml_service.deployments
71
+ pp ml_service.deployment(DEPLOYMENT_ID)
72
+ pp ml_service.deployment_by_name('Deployed ML Model')
73
+
74
+ # Get a score for the given deployment and record
75
+ score = ml_service.score(DEPLOYMENT_ID, record)
76
+ score = ml_service.score_by_name('Deployed ML Model', record)
77
+ pp score
78
+ prediction = ml_service.query_score(score, 'prediction')
79
+ probability = ml_service.query_score(score, 'probability')[prediction]
80
+ puts
81
+ puts "Prediction = #{prediction == 1}"
82
+ puts "Probability = #{(probability * 100).round(1)}%"
65
83
  ```
66
84
 
67
- #### Local
85
+ ### Local
68
86
  ```ruby
69
- HOST = # DSX Local hostname / IP address
70
- USERNAME = # DSX Local username
71
- PASSWORD = # DSX Local password
72
- DEPLOYMENT_ID = # deployment ID
73
-
74
- service = IBM::ML::Local.new(HOST, USERNAME, PASSWORD)
75
- pp service.fetch_token
76
- pp service.get_score(DEPLOYMENT_ID, record)
87
+ LOCAL_HOST = # DSX Local hostname / IP address
88
+ LOCAL_USERNAME = # DSX Local username
89
+ LOCAL_PASSWORD = # DSX Local password
90
+ DEPLOYMENT_ID = # deployment ID
91
+
92
+ # Create the service object
93
+ ml_service = IBM::ML::Local.new(LOCAL_HOST, LOCAL_USERNAME, LOCAL_PASSWORD)
94
+
95
+ # Fetch an authentication token
96
+ pp ml_service.fetch_token
97
+
98
+ # Get a score for the given deployment and record
99
+ pp ml_service.score(DEPLOYMENT_ID, record)
77
100
  ```
78
101
 
79
102
  ## Development
@@ -32,7 +32,7 @@ module IBM
32
32
 
33
33
  def get_request(addr, top_key)
34
34
  url = URI(addr)
35
- header = { 'authorization' => "Bearer #{fetch_token}" }
35
+ header = auth_header
36
36
  request = Net::HTTP::Get.new url, header
37
37
 
38
38
  response = @http.request(request)
@@ -40,5 +40,22 @@ module IBM
40
40
  body = JSON.parse(response.read_body)
41
41
  body.key?(top_key) ? body : raise(body['message'])
42
42
  end
43
+
44
+ def auth_header
45
+ { 'authorization' => "Bearer #{fetch_token}" }
46
+ end
47
+
48
+ def post_request(url, body)
49
+ request = Net::HTTP::Post.new(url, post_header)
50
+ request.body = body
51
+ response = @http.request(request)
52
+ JSON.parse(response.read_body)
53
+ end
54
+
55
+ def post_header
56
+ header = auth_header
57
+ header['content-type'] = 'application/json'
58
+ header
59
+ end
43
60
  end
44
61
  end
@@ -10,7 +10,7 @@ module IBM
10
10
  @http.use_ssl = true
11
11
  end
12
12
 
13
- def published_models
13
+ def models
14
14
  get_request "https://#{@host}/v2/published_models", 'resources'
15
15
  end
16
16
 
@@ -18,31 +18,44 @@ module IBM
18
18
  get_request "https://#{@host}/v2/deployments", 'resources'
19
19
  end
20
20
 
21
- def get_model(model_id)
21
+ def deployment(deployment_id)
22
+ find_by_id(deployments, deployment_id)
23
+ end
24
+
25
+ def deployment_by_name(name)
26
+ find_by_name(deployments, name)
27
+ end
28
+
29
+ def model(model_id)
22
30
  get_request "https://#{@host}/v2/published_models/#{model_id}", 'entity'
23
31
  end
24
32
 
25
- def get_score(model_id, deployment_id, record)
26
- url = URI("https://#{@host}/v2/published_models/#{model_id}/deployments/#{deployment_id}/online")
33
+ def model_by_name(name)
34
+ find_by_name(models, name)
35
+ end
36
+
37
+ def score_by_name(name, record)
38
+ deployment = find_by_name(deployments, name)
39
+ score(deployment['metadata']['guid'], record)
40
+ end
27
41
 
28
- # noinspection RubyStringKeysInHashInspection
29
- header = {
30
- 'authorization' => "Bearer #{fetch_token}",
31
- 'content-type' => 'application/json'
32
- }
42
+ def score(deployment_id, record)
43
+ deployment = deployment(deployment_id)['entity']
44
+ model_fields = model(deployment['published_model']['guid'])['entity']['input_data_schema']['fields']
33
45
 
34
- model_fields = get_model(model_id)['entity']['input_data_schema']['fields']
35
- request = Net::HTTP::Post.new(url, header)
36
- request.body = {
46
+ response = post_request deployment['scoring_href'], {
37
47
  fields: model_fields.map { |field| field['name'] },
38
48
  values: [record.values]
39
49
  }.to_json
40
50
 
41
- response = @http.request(request)
51
+ raise(response['message'] + ' : ' + response['description']) if response.key?('message')
52
+ response
53
+ end
42
54
 
43
- body = JSON.parse(response.read_body)
44
- return body if body.key?('fields') && body.key?('values')
45
- raise(body['message'] + ' : ' + body['description'])
55
+ def query_score(score, field)
56
+ fields = score['fields'].map(&:upcase)
57
+ index = fields.index(field.upcase)
58
+ score['values'].map { |record| record[index] }[0]
46
59
  end
47
60
 
48
61
  private
@@ -61,6 +74,22 @@ module IBM
61
74
  def process_ldap_response(response)
62
75
  JSON.parse(response.read_body)['token']
63
76
  end
77
+
78
+ def find_by_id(response, guid)
79
+ response['resources'].each do |resource|
80
+ return resource if resource['metadata']['guid'] == guid
81
+ end
82
+ end
83
+
84
+ def find_by_name(response, name)
85
+ response['resources'].each do |resource|
86
+ return resource if resource['entity']['name'] == name
87
+ end
88
+ raise(QueryError, "Could not find resource with name \"#{name}\"")
89
+ end
90
+ end
91
+
92
+ class QueryError < StandardError
64
93
  end
65
94
  end
66
95
  end
@@ -12,7 +12,7 @@ module IBM
12
12
  @http.verify_mode = OpenSSL::SSL::VERIFY_NONE
13
13
  end
14
14
 
15
- def get_score(deployment_id, hash)
15
+ def score(deployment_id, hash)
16
16
  url = URI("https://#{@host}/v2/scoring/online/#{deployment_id}")
17
17
 
18
18
  # noinspection RubyStringKeysInHashInspection
@@ -1,5 +1,5 @@
1
1
  module IBM
2
2
  module ML
3
- VERSION = '0.2.0'.freeze
3
+ VERSION = '0.2.1'.freeze
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ibm-ml
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - David Thomason
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-10-02 00:00:00.000000000 Z
11
+ date: 2017-10-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler