This repository has been archived by the owner on Sep 14, 2018. It is now read-only.
forked from matthiaskramm/mrscake
-
Notifications
You must be signed in to change notification settings - Fork 1
/
mrscake.rb.c
283 lines (256 loc) · 8.86 KB
/
mrscake.rb.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/* predict.rb.c
Ruby wrapper for the prediction library
Part of the prediction package.
Copyright (c) 2011 Matthias Kramm <[email protected]>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
#include <ruby.h>
#include <st.h>
#include "mrscake.h"
#include "stringpool.h"
static VALUE mrscake;
static VALUE DataSet, Model;
static ID id_doc;
typedef struct dataset_internal {
trainingdata_t* trainingdata;
} dataset_internal_t;
typedef struct model_internal {
model_t* model;
} model_internal_t;
#define Get_DataSet(d,cls) dataset_internal_t*d=0;Data_Get_Struct(cls, dataset_internal_t, d);
#define Get_Model(m,cls) model_internal_t*m=0;Data_Get_Struct(cls, model_internal_t, m);
static VALUE rb_model_allocate(VALUE cls);
static VALUE rb_dataset_allocate(VALUE cls);
// ------------------------ dataset ------------------------------------
variable_t value_to_variable(VALUE v)
{
if(TYPE(v) == T_SYMBOL) {
return variable_new_text(rb_id2name(SYM2ID(v)));
} else if(TYPE(v) == T_FLOAT) {
return variable_new_continuous(NUM2DBL(v));
} else if(TYPE(v) == T_FIXNUM) {
return variable_new_continuous(FIX2INT(v));
} else {
return variable_new_missing();
}
}
static int hash_count(VALUE key, VALUE value, VALUE arg)
{
int*count = (int*)arg;
(*count)++;
return ST_CONTINUE;
}
static int hash_fill(VALUE key, VALUE value, VALUE arg)
{
Check_Type(key, T_SYMBOL);
const char*name;
if(TYPE(key) == T_SYMBOL) {
name = rb_id2name(SYM2ID(key));
} else if(TYPE(key) == T_STRING) {
name = StringValuePtr(key);
}
example_t*e = (example_t*)arg;
e->inputs[e->num_inputs] = value_to_variable(value);
e->input_names[e->num_inputs] = register_string(name);
e->num_inputs++;
return ST_CONTINUE;
}
static example_t*value_to_example(VALUE input)
{
example_t*e = 0;
if(TYPE(input) == T_ARRAY) {
int len = RARRAY(input)->len;
int t;
e = example_new(len);
for(t=0;t<len;t++) {
VALUE item = RARRAY(input)->ptr[t];
e->inputs[t] = value_to_variable(item);
if(e->inputs[t].type == MISSING) {
rb_raise(rb_eArgError, "bad element in array at pos %d", t+1);
}
}
} else if(TYPE(input) == T_HASH) {
int len = 0;
rb_hash_foreach(input, hash_count, (VALUE)&len);
e = example_new(len);
e->input_names = (const char**)malloc(sizeof(char*)*len);
e->num_inputs = 0;
rb_hash_foreach(input, hash_fill, (VALUE)e);
if(e->num_inputs != len)
rb_raise(rb_eArgError, "Couldn't process hash");
} else {
rb_raise(rb_eArgError, "expected array or a hash");
}
e->desired_response = variable_new_missing();
return e;
}
static VALUE rb_dataset_add(VALUE cls, VALUE input, VALUE response)
{
Get_DataSet(dataset,cls);
example_t*e = value_to_example(input);
e->desired_response = value_to_variable(response);
if(e->desired_response.type == MISSING) {
rb_raise(rb_eArgError, "bad argument to add(): second parameter must be an int or a symbol");
}
trainingdata_add_example(dataset->trainingdata, e);
return cls;
}
static VALUE rb_dataset_get_model(VALUE cls)
{
Get_DataSet(dataset,cls);
VALUE model_value = rb_model_allocate(Model);
Get_Model(model, model_value);
model->model = model_select(dataset->trainingdata);
if(!model->model)
rb_raise(rb_eArgError, "bad (empty?) data");
return model_value;
}
static VALUE rb_dataset_print(VALUE cls)
{
Get_DataSet(dataset,cls);
trainingdata_print(dataset->trainingdata);
return cls;
}
static VALUE rb_dataset_save(VALUE cls, VALUE _filename)
{
Check_Type(_filename, T_STRING);
const char*filename = StringValuePtr(_filename);
Get_DataSet(dataset,cls);
trainingdata_save(dataset->trainingdata, filename);
return cls;
}
static void rb_dataset_mark(dataset_internal_t*dataset)
{
}
static void rb_dataset_free(dataset_internal_t*dataset)
{
if(dataset->trainingdata) {
trainingdata_destroy(dataset->trainingdata);
dataset->trainingdata = 0;
}
free(dataset);
}
static VALUE rb_dataset_allocate(VALUE cls)
{
dataset_internal_t*dataset = 0;
VALUE v = Data_Make_Struct(cls, dataset_internal_t, rb_dataset_mark, rb_dataset_free, dataset);
memset(dataset, 0, sizeof(dataset_internal_t));
dataset->trainingdata = trainingdata_new();
return v;
}
// ------------------------ model ---------------------------------------
static void rb_model_free(model_internal_t*model)
{
if(!model) return;
if(model->model) {
model_destroy(model->model);
model->model = 0;
}
free(model);
}
static VALUE rb_model_print(VALUE cls)
{
Get_Model(model,cls);
model_print(model->model);
return cls;
}
static VALUE rb_model_save(VALUE cls, VALUE _filename)
{
Check_Type(_filename, T_STRING);
const char*filename = StringValuePtr(_filename);
Get_Model(model,cls);
model_save(model->model, filename);
return cls;
}
static VALUE rb_model_generate_code(VALUE cls, VALUE _language)
{
Check_Type(_language, T_STRING);
const char*language = StringValuePtr(_language);
Get_Model(model,cls);
char*code = model_generate_code(model->model, language);
return rb_str_new2(code);
}
static VALUE rb_load_model(VALUE module, VALUE _filename)
{
Check_Type(_filename, T_STRING);
const char*filename = StringValuePtr(_filename);
VALUE cls = rb_model_allocate(Model);
Get_Model(model,cls);
model->model = model_load(filename);
if(!model->model) {
rb_raise(rb_eIOError, "couldn't open %s", filename);
}
return cls;
}
static VALUE rb_load_dataset(VALUE module, VALUE _filename)
{
Check_Type(_filename, T_STRING);
const char*filename = StringValuePtr(_filename);
VALUE cls = rb_dataset_allocate(DataSet);
Get_DataSet(dataset,cls);
dataset->trainingdata = trainingdata_load(filename);
if(!dataset->trainingdata) {
rb_raise(rb_eIOError, "couldn't open %s", filename);
}
return cls;
}
static VALUE rb_model_predict(VALUE cls, VALUE input)
{
Get_Model(model,cls);
if(TYPE(input) != T_ARRAY && TYPE(input) != T_HASH) {
rb_raise(rb_eArgError, "First argument to predict() must be an array or a hash");
}
example_t*e = value_to_example(input);
if(e->num_inputs != model->model->sig->num_inputs)
rb_raise(rb_eArgError, "You supplied %d inputs for a model with %d inputs", e->num_inputs, model->model->sig->num_inputs);
row_t*row = example_to_row(e, model->model->sig->column_names);
variable_t prediction = model_predict(model->model, row);
row_destroy(row);
if(prediction.type == CONTINUOUS)
return rb_float_new(prediction.value);
else if(prediction.type == CATEGORICAL)
return INT2FIX(prediction.category);
else if(prediction.type == TEXT)
return rb_str_new2(prediction.text);
else
return T_NIL;
}
static void rb_model_mark(model_internal_t*model)
{
}
static VALUE rb_model_allocate(VALUE cls)
{
model_internal_t*model = 0;
VALUE v = Data_Make_Struct(cls, model_internal_t, rb_model_mark, rb_model_free, model);
memset(model, 0, sizeof(model_internal_t));
return v;
}
// --------------------------------------------------------------------------
void Init_mrscake()
{
mrscake = rb_define_module("MrsCake");
rb_define_module_function(mrscake, "load_model", rb_load_model, 1);
rb_define_module_function(mrscake, "load_data", rb_load_dataset, 1);
DataSet = rb_define_class_under(mrscake, "DataSet", rb_cObject);
rb_define_alloc_func(DataSet, rb_dataset_allocate);
rb_define_method(DataSet, "add", rb_dataset_add, 2);
rb_define_method(DataSet, "get_model", rb_dataset_get_model, 0);
rb_define_method(DataSet, "train", rb_dataset_get_model, 0);
rb_define_method(DataSet, "print", rb_dataset_print, 0);
rb_define_method(DataSet, "save", rb_dataset_save, 1);
Model = rb_define_class_under(mrscake, "Model", rb_cObject);
rb_define_alloc_func(Model, rb_model_allocate);
rb_define_method(Model, "print", rb_model_print, 0);
rb_define_method(Model, "save", rb_model_save, 1);
rb_define_method(Model, "predict", rb_model_predict, 1);
rb_define_method(Model, "generate_code", rb_model_generate_code, 1);
}