Changeset 86420e in git


Ignore:
Timestamp:
Aug 7, 2019, 10:18:55 AM (4 years ago)
Author:
Murray Heymann <heymann.murray@…>
Branches:
(u'jengelh-datetime', 'ceac47cbc86fe4a15902392bdbb9bd2ae0ea02c6')(u'spielwiese', '0604212ebb110535022efecad887940825b97c3f')
Children:
0696a360fd5622a1c1f3cf64fc8282f0ecf1b8bd
Parents:
bb8cdbbda3604b0b59579d1fa969c8d5ca30f07b
Message:
Update python testing
Location:
machine_learning
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • machine_learning/ml_python/common/lookuptable.py

    rbb8cdbb r86420e  
    116116        vectors, file_list = create_table(dictionary=dictionary,
    117117                                          attempt_cached=False)
     118
     119def is_lookup_initialised():
     120    """
     121    Check whether the various files exist, return True if so, False
     122    otherwise.
     123    """
     124    retvalue = True
     125    tbz2_path = os.path.join(HELP_FILE_PATH, "helpfiles.tbz2")
     126    if not os.path.isdir(HELP_FILE_PATH) or not os.path.isfile(tbz2_path):
     127        retvalue = False
     128    if not os.path.isfile(KEYWORDS_FILE):
     129        retvalue = False
     130    if not os.path.isfile(VECTORS_NPY) or not os.path.isfile(HELPFILE_NPY):
     131        retvalue = False
     132
     133    return retvalue
  • machine_learning/ml_python/tests/common/test_lookuptable.py

    rbb8cdbb r86420e  
    4747        init_table_on_system()
    4848
     49    def test_is_lookup_initialised(self):
     50        tbz2_path = os.path.join(HELP_FILE_PATH, "helpfiles.tbz2")
     51        os.remove(tbz2_path)
     52        self.assertFalse(is_lookup_initialised())
     53        init_table_on_system()
     54        self.assertTrue(is_lookup_initialised())
     55
     56        os.remove(KEYWORDS_FILE)
     57        self.assertFalse(is_lookup_initialised())
     58        init_table_on_system()
     59        self.assertTrue(is_lookup_initialised())
     60
     61        os.remove(VECTORS_NPY)
     62        self.assertFalse(is_lookup_initialised())
     63        init_table_on_system()
     64        self.assertTrue(is_lookup_initialised())
     65       
    4966
    5067if __name__ == '__main__':
  • machine_learning/ml_python/tests/model/test_predictor.py

    rbb8cdbb r86420e  
    3333        files = np.array(["file1", "file2", "file3"])
    3434
    35         testvec = normalise_vector(np.array([1, 1, 1]))
     35        testvec1 = normalise_vector(np.array([1, 1, 1]))
     36        testvec2 = normalise_vector(np.array([1, 3, 10]))
    3637
    3738        print("distance to 1")
    38         print(vector_distance(testvec, vector1))
     39        print("vec1:", vector_distance(testvec1, vector1))
     40        print("vec2:", vector_distance(testvec2, vector1))
    3941        print()
    4042        print("distance to 2")
    41         print(vector_distance(testvec, vector2))
     43        print("vec1:", vector_distance(testvec1, vector2))
     44        print("vec2:", vector_distance(testvec2, vector2))
    4245        print()
    4346        print("distance to 3")
    44         print(vector_distance(testvec, vector3))
     47        print("vec1:", vector_distance(testvec1, vector3))
     48        print("vec2:", vector_distance(testvec2, vector2))
    4549        print()
    4650
    4751        predictor.fit(vectors, files)
    48         prediction = predictor.predict(np.array([testvec]))
     52        prediction = predictor.predict(np.array([]))
     53        self.assertEqual(len(prediction), 0)
     54        prediction = predictor.predict(np.array([testvec1]))
     55        self.assertEqual(prediction[0], "file2")
     56        self.assertEqual(len(prediction), 1)
     57        prediction = predictor.predict(np.array([testvec1, testvec2]))
    4958        print("Prediction:")
    5059        print(prediction)
    5160        self.assertEqual(prediction[0], "file2")
     61        self.assertEqual(prediction[1], "file1")
     62        self.assertEqual(len(prediction), 2)
    5263
    5364
  • machine_learning/mlpredict.c

    rbb8cdbb r86420e  
    2121int ml_is_initialised()
    2222{
    23         /*
    24          * TODO
    25          */
    26         return 1;
     23        int retvalue = 1;
     24        int t_value = 0;
     25        PyObject *pValue = NULL;
     26
     27        if (!Py_IsInitialized()) {
     28                retvalue = 0;
     29        } else {
     30                /* python system is initialised */
     31                pValue = call_python_function(LOOKUPTABLE, IS_LOOKUP_INITIALISED);
     32                /* is this a boolean? */
     33                if (pValue != NULL && PyBool_Check(pValue)) {
     34                        t_value = PyObject_IsTrue(pValue);
     35                        /* errors? */
     36                        if (t_value == -1) {
     37                                PyErr_Print();
     38                                retvalue = 0;
     39                        } else {
     40                                /* no errors */
     41                                retvalue = t_value;
     42                        }
     43                        Py_DECREF(pValue);
     44                } else {
     45                        retvalue = 0;
     46                }
     47        }
     48        return retvalue;
    2749}
    2850
     
    3860int ml_initialise()
    3961{
    40         char lookuptable[] = "common.lookuptable";
    41         char init_table_on_system[] = "init_table_on_system";
    4262        PyObject *pValue = NULL;
    4363
     
    4565                Py_Initialize();
    4666        }
    47         pValue = call_python_function(lookuptable, init_table_on_system);
     67        pValue = call_python_function(LOOKUPTABLE, INIT_TABLE_ON_SYSTEM);
    4868        if (pValue != NULL) {
    4969                Py_DECREF(pValue);
     
    5676
    5777/**
    58  * Finalize the python interpreter
     78 * A wrapper for Py_Finalize, checking whether it is necessary in the first
     79 * place.
    5980 *
    6081 * @return An integer: 1 if successful, 0 if not.
     
    6283int ml_finalise()
    6384{
     85        int retvalue = 1;
    6486        if (Py_IsInitialized()) {
    6587                Py_Finalize();
     88                retvalue = 1;
     89        } else {
     90                retvalue = 0;
    6691        }
    67         return 1;
     92        return retvalue;
    6893}
    6994
  • machine_learning/mlpredict.h

    rbb8cdbb r86420e  
    1717#define HELP_FILE_URL   "ftp://jim.mathematik.uni-kl.de/pub/Math/Singular/src/4-1-2/doc.tbz2"
    1818#define HELP_FILE_PATH  "helpfiles/singular"
     19
     20#define LOOKUPTABLE "common.lookuptable"
     21#define IS_LOOKUP_INITIALISED "is_lookup_initialised"
     22#define INIT_TABLE_ON_SYSTEM "init_table_on_system"
    1923
    2024/**
  • machine_learning/testml.c

    rbb8cdbb r86420e  
    11#include <stdio.h>
     2#include <Python.h>
    23#include "mlpredict.h"
    34
     
    910                return 1;
    1011        }
     12
     13        i = ml_is_initialised();
     14        printf("Returnvalue for ml_is_initialised: %d\n", i);
     15        Py_Initialize();
     16
     17        i = ml_is_initialised();
     18        printf("Returnvalue for ml_is_initialised: %d\n", i);
     19
    1120        i = ml_initialise();
    12         printf("Returnvalue for initialise: %d\n", i);
     21        printf("Returnvalue for ml_initialise: %d\n", i);
     22
     23        i = ml_is_initialised();
     24        printf("Returnvalue for ml_is_initialised: %d\n", i);
    1325
    1426        i = ml_finalise();
    15         printf("Returnvalue for finalise: %d\n", i);
     27        printf("Returnvalue for ml_finalise: %d\n", i);
    1628
    1729        return 0;
Note: See TracChangeset for help on using the changeset viewer.