//---------------------------------------------------------------------------- static PyObject* f_load(PyObject* self, PyObject* args, PyObject* kwargs) { static char *kwlist1[] = {"filename", NULL}; static char *kwlist2[] = {"data", NULL}; char* filename = 0; char* data = 0; int len = 0; SWFObject* swf; int fi; if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s", kwlist1, &filename)) { PyErr_Clear(); if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s#", kwlist2, &data, &len)) { PyErr_Clear(); PyArg_ParseTupleAndKeywords(args, kwargs, "s:load", kwlist1, &filename); return 0; } } swf = PyObject_New(SWFObject, &SWFClass); mylog("+%08x(%d) f_load\n", (int)swf, swf->ob_refcnt); memset(&swf->swf, 0, sizeof(SWF)); if(filename) { if(!filename) { PyErr_SetString(PyExc_Exception, setError("Couldn't open file %s", filename)); return 0; } swf->filename = strdup(filename); fi = open(filename,O_RDONLY|O_BINARY); if (fi<0) { return PY_ERROR("Couldn't open file %s", filename); } if(swf_ReadSWF(fi,&swf->swf)<0) { close(fi); return PY_ERROR("%s is not a valid SWF file or contains errors",filename); } close(fi); } else { reader_t r; reader_init_memreader(&r, data, len); swf->filename = 0; if(swf_ReadSWF2(&r, &swf->swf)<0) { return PY_ERROR("<data> is not a valid SWF file or contains errors"); } r.dealloc(&r); } swf_FoldAll(&swf->swf); swf->taglist = taglist_new2(swf->swf.firstTag); if(swf->taglist == NULL) { return NULL; } swf_FreeTags(&swf->swf); swf->swf.firstTag = 0; return (PyObject*)swf; }
static PyObject* py_dataset_train(PyObject*_self, PyObject* args, PyObject* kwargs) { DataSetObject*self = (DataSetObject*)_self; static char *kwlist[] = {"name", NULL}; const char*name = 0; if (args && !PyArg_ParseTupleAndKeywords(args, kwargs, "|s", kwlist, &name)) return NULL; int num_examples = self->data->num_examples; if(!num_examples) { return PY_ERROR("No training data given. Can't build a model from no data."); } if(!trainingdata_check_format(self->data)) { return PY_ERROR("bad training data"); } model_t*model = NULL; if(name == NULL) { model = trainingdata_train(self->data); } else { model = trainingdata_train_specific_model(self->data, name); if(!model) return PY_ERROR("Couldn't train model %s", name); } if(!model) return PY_NONE; ModelObject*ret = PyObject_New(ModelObject, &ModelClass); ret->model = model; return (PyObject*)ret; }
example_t* pylist_to_example(PyObject*input) { example_t*e = 0; if(PyList_Check(input)) { int size = PyList_Size(input); e = example_new(size); int t; for(t=0; t<size; t++) { PyObject*item = PyList_GetItem(input, t); if(!add_item(e, t, item)) return NULL; } } else if(PyDict_Check(input)) { int size = PyDict_Size(input); PyObject*pkey = 0; PyObject*pvalue = 0; size_t pos = 0; int t = 0; e = example_new(size); e->input_names = (const char**)malloc(sizeof(e->input_names[0])*size); while(PyDict_Next(input, &pos, &pkey, &pvalue)) { if(!pystring_check(pkey)) return PY_ERROR("dict object must use strings as keys"); const char*s = pystring_asstring(pkey); if(!add_item(e, t, pvalue)) return NULL; e->input_names[t] = register_string(s); t++; } } else { return PY_ERROR("first argument must be a list or a dict"); } return e; }
static PyObject* py_dataset_add(PyObject * _self, PyObject* args, PyObject* kwargs) { DataSetObject*self = (DataSetObject*)_self; static char *kwlist[] = {"input","output",NULL}; PyObject*input=0,*output=0; if (args && !PyArg_ParseTupleAndKeywords(args, kwargs, "OO", kwlist, &input, &output)) return NULL; if(!PyList_Check(input) && !PyDict_Check(input)) // && !PyTuple_Check(input)) return PY_ERROR("first argument to train() must be a list or a dict"); example_t*e = pylist_to_example(input); if(!e) return NULL; if(pyint_check(output)) { e->desired_response = variable_new_categorical(pyint_as_long(output)); } else if(pystring_check(output)) { e->desired_response = variable_new_text(pystring_asstring(output)); } else { return PY_ERROR("output parameter must be an integer or a string"); } trainingdata_add_example(self->data, e); return PY_NONE; }
static PyObject* py_model_predict(PyObject* _self, PyObject* args, PyObject* kwargs) { ModelObject* self = (ModelObject*)_self; PyObject*data = 0; static char *kwlist[] = {"data", NULL}; if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O", kwlist, &data)) return NULL; example_t*e = pylist_to_example(data); if(!e) return NULL; if(e->num_inputs != self->model->sig->num_inputs) { PY_ERROR("You supplied %d inputs for a model with %d inputs", e->num_inputs, self->model->sig->num_inputs); example_destroy(e); return NULL; } row_t*row = example_to_row(e, self->model->sig->column_names); if(!row) return PY_ERROR("Can't create row from data"); variable_t i = model_predict(self->model, row); row_destroy(row); example_destroy(e); if(i.type == TEXT) return pystring_fromstring(i.text); else if(i.type == CATEGORICAL) return pyint_fromlong(i.category); else if(i.type == CONTINUOUS) return PyFloat_FromDouble(i.value); else if(i.type == MISSING) return PY_NONE; else return PY_ERROR("internal error: bad variable type %d", i.type); }
static PyObject* output_fillbitmap(PyObject* _self, PyObject* args, PyObject* kwargs) { OutputObject* self = (OutputObject*)_self; PyObject*_line=0; PyObject*_bitmap=0; static char *kwlist[] = {"line", "bitmap", NULL}; if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O!O", kwlist, &PyList_Type, &_line, &_bitmap)) return NULL; gfximage_t*image = toImage(_bitmap); if(!image) return PY_ERROR("invalid image"); gfxline_t*line = toLine(_line); if(!line) return 0; /* TODO */ gfxmatrix_t m; memset(&m, 0, sizeof(gfxmatrix_t)); m.m00 = m.m11 = 1.0; self->output_device->fillbitmap(self->output_device, line, image, &m, 0); gfxline_free(line); return PY_NONE; }
static gfximage_t*toImage(PyObject*_bitmap) { if(!_bitmap || !_bitmap->ob_type->tp_name || strcmp(_bitmap->ob_type->tp_name, "Image")) { PY_ERROR("Second argument to fillbitmap must be an image"); return 0; } ImageObject*bitmap = (ImageObject*)_bitmap; return bitmap->image; }
static PyObject* py_dataset_load(PyObject* module, PyObject* args, PyObject* kwargs) { char*filename = 0; static char *kwlist[] = {"filename", NULL}; if (args && !PyArg_ParseTupleAndKeywords(args, kwargs, "s", kwlist, &filename)) return NULL; DataSetObject*self = PyObject_New(DataSetObject, &DataSetClass); self->data = trainingdata_load(filename); if(!self->data) return PY_ERROR("Couldn't load model from %s", filename); return (PyObject*)self; }
static PyObject* py_model_load(PyObject* module, PyObject* args, PyObject* kwargs) { char*filename = 0; static char *kwlist[] = {"filename", NULL}; if (args && !PyArg_ParseTupleAndKeywords(args, kwargs, "s", kwlist, &filename)) return NULL; ModelObject*self = PyObject_New(ModelObject, &ModelClass); self->model = model_load(filename); if(!self->model) return PY_ERROR("Couldn't load model from %s", filename); return (PyObject*)self; }
static PyObject* mrscake_set_parameter(PyObject* module, PyObject* args, PyObject* kwargs) { static char *kwlist[] = {"key", "value", NULL}; char*key=0,*value=0; if (!PyArg_ParseTupleAndKeywords(args, kwargs, "ss", kwlist, &key, &value)) return NULL; state_t*state = STATE(module); bool ok = config_setparameter(key,value); if(!ok) { return PY_ERROR("Couldn't set parameter %s", key); } return PY_NONE; }
static gfxline_t*toLine(PyObject*_line) { int t; int num = PyList_Size(_line); gfxline_t first; first.next = 0; gfxline_t*last=&first; for(t=0;t<num;t++) { PyObject*p= PySequence_GetItem(_line, t); if(!PyTuple_Check(p)) { return PY_ERROR("each point must be a tuple"); } PyObject*_type = PyTuple_GetItem(p, 0); if(!PyString_Check(_type)) return PY_ERROR("point tuples must start with a string"); char*type = PyString_AsString(_type); int s; int size = PyTuple_Size(p); for(s=1;s<size;s++) { if(!PyFloat_Check(PyTuple_GetItem(p,s))) { return PY_ERROR("coordinates must be floats"); } } gfxline_t*l = (gfxline_t*)malloc(sizeof(gfxline_t)); memset(l, 0, sizeof(gfxline_t)); last->next = l; last = l; if(type[0]=='m') { l->type = gfx_moveTo; if(size!=3) return PY_ERROR("need 2 values for move"); l->x = PyFloat_AsDouble(PyTuple_GetItem(p, 1)); l->y = PyFloat_AsDouble(PyTuple_GetItem(p, 2)); } else if(type[0]=='l') { l->type = gfx_lineTo; if(size!=3) return PY_ERROR("need 2 values for line"); l->x = PyFloat_AsDouble(PyTuple_GetItem(p, 1)); l->y = PyFloat_AsDouble(PyTuple_GetItem(p, 2)); } else if(type[0]=='s') { l->type = gfx_splineTo; if(size!=5) return PY_ERROR("need 4 values for spline"); l->x = PyFloat_AsDouble(PyTuple_GetItem(p, 1)); l->y = PyFloat_AsDouble(PyTuple_GetItem(p, 2)); l->sx = PyFloat_AsDouble(PyTuple_GetItem(p, 3)); l->sy = PyFloat_AsDouble(PyTuple_GetItem(p, 4)); } else { return PY_ERROR("Unknown line code '%s'", type); } } return first.next; }
//--------------------helper functions -------------------------------- int add_item(example_t*e, int pos, PyObject*item) { if(pyint_check(item)) { e->inputs[pos] = variable_new_continuous(pyint_as_long(item)); } else if(PyFloat_Check(item)) { e->inputs[pos] = variable_new_continuous(PyFloat_AS_DOUBLE(item)); } else if(pystring_check(item)) { e->inputs[pos] = variable_new_text(pystring_asstring(item)); } else { PY_ERROR("bad object %s in list", item->ob_type->tp_name); return 0; } return 1; }
static PyObject* output_save(PyObject* _self, PyObject* args, PyObject* kwargs) { OutputObject* self = (OutputObject*)_self; char*filename = 0; static char *kwlist[] = {"filename", NULL}; if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s", kwlist, &filename)) return NULL; gfxresult_t*result = self->output_device->finish(self->output_device); self->output_device = 0; if(result->save(result, filename) < 0) { return PY_ERROR("Couldn't write to %s", filename); } result->destroy(result); return PY_NONE; }
static PyObject* color_getattr(PyObject * self, char* a) { ColorObject*color = (ColorObject*)self; if(!strcmp(a, "r")) { return Py_BuildValue("i", color->rgba.r); } else if(!strcmp(a, "g")) { return Py_BuildValue("i", color->rgba.g); } else if(!strcmp(a, "b")) { return Py_BuildValue("i", color->rgba.b); } else if(!strcmp(a, "a")) { return Py_BuildValue("i", color->rgba.a); } else if(!strcmp(a, "alpha")) { return Py_BuildValue("i", color->rgba.a); } else if(!strcmp(a, "rgb")) { char text[80]; sprintf(text, "%02x%02x%02x", color->rgba.r, color->rgba.g, color->rgba.b); return PyString_FromString(text); } else if(!strcmp(a, "rgba")) { char text[80]; sprintf(text, "%02x%02x%02x%02x", color->rgba.r, color->rgba.g, color->rgba.b, color->rgba.a); return PyString_FromString(text); } return PY_ERROR("bad attribute"); }