confusionMatrixDemo.py
1 ## @package python.demo.confusionMatrixDemo
2 #
3 # example script showing how to use opals.workflows.clfConfusionMatrix within python to retrieve a confusion matrix
4 #
5 
6 import opals.Types
7 from opals.workflows import clfConfusionMatrix
8 from opals import pyDM
9 
10 
11 def confusion_matrix(odm, refAttr, estimAttr):
12  conf = clfConfusionMatrix.clfConfusionMatrix(infile=odm, refAttr=refAttr, estimAttr=estimAttr,
13  screenLogLevel=opals.Types.LogLevel.error)
14  conf.run()
15  obj = conf.matrix # get the confusion matrix of type pyDM.ClassificationMatrix
16  print(f"id list: {obj.idList()}")
17  npConMat = pyDM.NumpyConverter.get(obj) # convert confusion matrix object into a numpy array (maybe None)
18  if npConMat is not None:
19  print("Confusion Matrix (absolute frequencies):")
20  print(npConMat)
21 
22 
23 # example file
24 odm = "niederrhein.odm"
25 
26 # get the confusion matrix of an odm
27 confusion_matrix(odm, refAttr='Classification', estimAttr='_classEstim')