Coverage for src/bob/fusion/base/algorithm/AlgorithmBob.py: 100%

30 statements  

« prev     ^ index     » next       coverage.py v7.6.0, created at 2024-07-13 01:00 +0200

1#!/usr/bin/env python 

2 

3from __future__ import absolute_import, division 

4 

5import logging 

6import pickle 

7 

8from h5py import File as HDF5File 

9 

10from .Algorithm import Algorithm 

11 

12logger = logging.getLogger(__name__) 

13 

14 

15class AlgorithmBob(Algorithm): 

16 """A class to be used in score fusion using bob machines.""" 

17 

18 def _get_hdf5_file(self, model_file): 

19 return model_file[:-3] + "hdf5" 

20 

21 def custom_save(self, model_file): 

22 # dump preprocessors in a pickle file because 

23 # we don't know how they look like 

24 # saves the class to create it later. 

25 with open(model_file, "wb") as f: 

26 pickle.dump(type(self), f) 

27 pickle.dump(self.preprocessors, f) 

28 # just for consistent string representation 

29 pickle.dump(self.str, f) 

30 

31 d5 = HDF5File(self._get_hdf5_file(model_file), "w") 

32 try: 

33 self.machine.save(d5) 

34 finally: 

35 d5.close() 

36 

37 def load(self, model_file): 

38 # load preprocessors and the class 

39 with open(model_file, "rb") as f: 

40 myclass = pickle.load(f) 

41 preprocessors = pickle.load(f) 

42 strings = pickle.load(f) 

43 

44 myinstance = myclass(preprocessors=preprocessors) 

45 # just for consistent string representation 

46 myinstance.str.update(strings) 

47 

48 d5 = HDF5File(self._get_hdf5_file(model_file)) 

49 try: 

50 myinstance.machine.load(d5) 

51 finally: 

52 d5.close() 

53 

54 return myinstance