| 343 | } |
| 344 | |
| 345 | py::UniqueObj LDAModelObject::infer(PyObject* docObj, size_t iteration, float tolerance, size_t workers, tomoto::ParallelScheme ps, bool together, PyObject* transform) const |
| 346 | { |
| 347 | auto* inst = getInst<tomoto::ILDAModel>(); |
| 348 | if (!isPrepared) throw py::RuntimeError{ "cannot infer with untrained model" }; |
| 349 | py::UniqueObj iter; |
| 350 | if (PyObject_TypeCheck(docObj, py::Type<CorpusObject>)) |
| 351 | { |
| 352 | auto cps = makeCorpus(docObj, transform); |
| 353 | std::vector<tomoto::DocumentBase*> docs; |
| 354 | for (auto& d : cps->docsMade) docs.emplace_back(d.get()); |
| 355 | auto ll = inst->infer(docs, iteration, tolerance, workers, ps, together); |
| 356 | return py::buildPyTuple(cps, ll); |
| 357 | } |
| 358 | else if (auto* doc = py::checkType<DocumentObject>(docObj)) |
| 359 | { |
| 360 | if (doc->corpus->tm.get() != py::getPObjectAddress(this)) throw py::ValueError{ "`doc` was from another model, not fit to this model" }; |
| 361 | if (doc->owner) |
| 362 | { |
| 363 | std::vector<tomoto::DocumentBase*> docs; |
| 364 | docs.emplace_back((tomoto::DocumentBase*)doc->getBoundDoc()); |
| 365 | float ll = inst->infer(docs, iteration, tolerance, workers, ps, together)[0]; |
| 366 | doc->initialized = true; |
| 367 | return py::buildPyTuple(py::buildPyValue(inst->getTopicsByDoc(doc->getBoundDoc())), ll); |
| 368 | } |
| 369 | else |
| 370 | { |
| 371 | return py::buildPyTuple(py::buildPyValue(inst->getTopicsByDoc(doc->getBoundDoc())), nullptr); |
| 372 | } |
| 373 | } |
| 374 | else if (py::clearError(), (iter = py::UniqueObj{ PyObject_GetIter(docObj) })) |
| 375 | { |
| 376 | std::vector<tomoto::DocumentBase*> docs; |
| 377 | std::vector<DocumentObject*> docObjs; |
| 378 | py::UniqueObj item; |
| 379 | while ((item = py::UniqueObj{ PyIter_Next(iter.get()) })) |
| 380 | { |
| 381 | auto* doc = py::checkType<DocumentObject>(item.get()); |
| 382 | if (!doc) throw py::ValueError{ "`doc` must be tomotopy.Document type or list of tomotopy.Document" }; |
| 383 | if (doc->corpus->tm.get() != py::getPObjectAddress(this)) throw py::ValueError{ "`doc` was from another model, not fit to this model" }; |
| 384 | docs.emplace_back((tomoto::DocumentBase*)doc->doc); |
| 385 | docObjs.emplace_back(doc); |
| 386 | } |
| 387 | if (PyErr_Occurred()) throw py::ExcPropagation{}; |
| 388 | auto ll = inst->infer(docs, iteration, tolerance, workers, ps, together); |
| 389 | |
| 390 | for (auto doc : docObjs) doc->initialized = true; |
| 391 | |
| 392 | auto ret = py::UniqueObj{ PyList_New(docs.size()) }; |
| 393 | size_t i = 0; |
| 394 | for (auto d : docs) |
| 395 | { |
| 396 | PyList_SetItem(ret.get(), i++, py::buildPyValue(inst->getTopicsByDoc(d)).release()); |
| 397 | } |
| 398 | if (together) |
| 399 | { |
| 400 | return py::buildPyTuple(ret, ll[0]); |
| 401 | } |
| 402 | else |
nothing calls this directly
no test coverage detected