def write_gmm_mgr(gmm_mgr, file): """ >>> f = cStringIO.StringIO() >>> dim = 2 >>> num_components = 3 >>> weights = numpy.array((0.25, 0.5, 0.25), dtype=float) >>> mu = numpy.array(((1, 1), (2, 2), (3, 3)), dtype=float) >>> v = numpy.array(((1, 1), (1, 1), (1, 1)), dtype=float) >>> gmm0 = GaussianMixtureModel(dim, GaussianMixtureModel.DIAGONAL_COVARIANCE, num_components) >>> gmm0.set_weights(weights) >>> gmm0.set_means(mu) >>> gmm0.set_vars(v) >>> gmm0.set_relevances((10.0, 10.0, 10.0)) >>> gmm1 = GaussianMixtureModel(dim, GaussianMixtureModel.DIAGONAL_COVARIANCE, num_components) >>> gmm1.set_weights(weights) >>> gmm1.set_means(mu) >>> gmm1.set_vars(v) >>> gmm_mgr0 = GmmMgr((gmm0, gmm1)) >>> write_gmm_mgr(gmm_mgr0, f) >>> print f.getvalue() --- - __onyx_yaml__stream_version: "0" __onyx_yaml__meta_version: "1" __onyx_yaml__stream_type: "OnyxText" - - stream_type OnyxText stream_version 0 data_type GmmManager data_version 0 - GmmMgr IndexedCollection Gmm 2 - Gmm 0 - covariance_type Singleton onyx.am.gaussian.GaussianModelBase.DIAGONAL_COVARIANCE - dimension 2 - num_components 3 - relevances List 3 - 10.0 10.0 10.0 - weights Array 1 3 - +(-0002)0x0000000000000 +(-0001)0x0000000000000 +(-0002)0x0000000000000 - Gaussians IndexedCollection SimpleGaussian 3 - SimpleGaussian 0 - means Array 1 2 - +(+0000)0x0000000000000 +(+0000)0x0000000000000 - vars Array 1 2 - +(+0000)0x0000000000000 +(+0000)0x0000000000000 - SimpleGaussian 1 - means Array 1 2 - +(+0001)0x0000000000000 +(+0001)0x0000000000000 - vars Array 1 2 - +(+0000)0x0000000000000 +(+0000)0x0000000000000 - SimpleGaussian 2 - means Array 1 2 - +(+0001)0x8000000000000 +(+0001)0x8000000000000 - vars Array 1 2 - +(+0000)0x0000000000000 +(+0000)0x0000000000000 - Gmm 1 - covariance_type Singleton onyx.am.gaussian.GaussianModelBase.DIAGONAL_COVARIANCE - dimension 2 - num_components 3 - relevances List 3 - 0.0 0.0 0.0 - weights Array 1 3 - +(-0002)0x0000000000000 +(-0001)0x0000000000000 +(-0002)0x0000000000000 - Gaussians IndexedCollection SimpleGaussian 3 - SimpleGaussian 0 - means Array 1 2 - +(+0000)0x0000000000000 +(+0000)0x0000000000000 - vars Array 1 2 - +(+0000)0x0000000000000 +(+0000)0x0000000000000 - SimpleGaussian 1 - means Array 1 2 - +(+0001)0x0000000000000 +(+0001)0x0000000000000 - vars Array 1 2 - +(+0000)0x0000000000000 +(+0000)0x0000000000000 - SimpleGaussian 2 - means Array 1 2 - +(+0001)0x8000000000000 +(+0001)0x8000000000000 - vars Array 1 2 - +(+0000)0x0000000000000 +(+0000)0x0000000000000 <BLANKLINE> # Round-trip test >>> f.seek(0) # rewind >>> gmm_mgr1 = read_gmm_mgr(f) >>> gmm_mgr0 == gmm_mgr1 True """ # create YamldataWriter on file yw = YamldataWriter(file, stream_type=OnyxTextWriter.STREAM_TYPE, stream_version=OnyxTextWriter.STREAM_VERSION) stream = OnyxTextWriter() hdr_gen = stream.gen_header("GmmManager", "0") # The first arg is the collection name, the second arg is the object name, the third arg gives # the objects to be written and the fourth arg is called with each object and returns a # generator of tuples to be written for that object. The generator must yield at least one # tuple, which will be written as part of the name/index line; subsequent tuples go on their own # line. all_gmms = [gmm for gmm in gmm_mgr] mgr_gen = stream.gen_indexed_collection("GmmMgr", "Gmm", all_gmms, gen_gmm) yw.write_document(chain(hdr_gen, mgr_gen))
def write_hmm_mgr(hmm_mgr, file): """ >>> f = cStringIO.StringIO() >>> dummies = ( DummyModel(2, 0.1), DummyModel(2, 0.2), DummyModel(2, 0.4), DummyModel(2, 0.4) ) >>> mm = GmmMgr(dummies) >>> models = range(3) >>> hmm0 = Hmm(3) >>> hmm0.build_forward_model_compact(mm, models, 2, ((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))) >>> hmm1 = Hmm(3) >>> hmm1.build_forward_model_compact(mm, models, 2, ((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))) >>> hmm_mgr0 = HmmMgr((hmm0, hmm1)) >>> write_hmm_mgr(hmm_mgr0, f) >>> print f.getvalue() --- - __onyx_yaml__stream_version: "0" __onyx_yaml__meta_version: "1" __onyx_yaml__stream_type: "OnyxText" - - stream_type OnyxText stream_version 0 data_type HmmManager data_version 0 - HmmMgr IndexedCollection Hmm 2 - Hmm 0 - num_inputs 1 - num_states 3 - num_outputs 1 - transition_matrix Array 2 5 5 - +(-1023)0x0000000000000 +(+0000)0x0000000000000 +(-1023)0x0000000000000 +(-1023)0x0000000000000 +(-1023)0x0000000000000 - +(-1023)0x0000000000000 +(-0001)0x0000000000000 +(-0001)0x0000000000000 +(-1023)0x0000000000000 +(-1023)0x0000000000000 - +(-1023)0x0000000000000 +(-1023)0x0000000000000 +(-0001)0x0000000000000 +(-0001)0x0000000000000 +(-1023)0x0000000000000 - +(-1023)0x0000000000000 +(-1023)0x0000000000000 +(-1023)0x0000000000000 +(-0001)0x0000000000000 +(-0001)0x0000000000000 - +(-1023)0x0000000000000 +(-1023)0x0000000000000 +(-1023)0x0000000000000 +(-1023)0x0000000000000 +(-1023)0x0000000000000 - models List 3 - 0 1 2 - Hmm 1 - num_inputs 1 - num_states 3 - num_outputs 1 - transition_matrix Array 2 5 5 - +(-1023)0x0000000000000 +(+0000)0x0000000000000 +(-1023)0x0000000000000 +(-1023)0x0000000000000 +(-1023)0x0000000000000 - +(-1023)0x0000000000000 +(-0001)0x0000000000000 +(-0001)0x0000000000000 +(-1023)0x0000000000000 +(-1023)0x0000000000000 - +(-1023)0x0000000000000 +(-1023)0x0000000000000 +(-0001)0x0000000000000 +(-0001)0x0000000000000 +(-1023)0x0000000000000 - +(-1023)0x0000000000000 +(-1023)0x0000000000000 +(-1023)0x0000000000000 +(-0001)0x0000000000000 +(-0001)0x0000000000000 - +(-1023)0x0000000000000 +(-1023)0x0000000000000 +(-1023)0x0000000000000 +(-1023)0x0000000000000 +(-1023)0x0000000000000 - models List 3 - 0 1 2 <BLANKLINE> # Round-trip test >>> f.seek(0) # rewind >>> hmm_mgr1 = read_hmm_mgr(f, mm) >>> hmm_mgr0 == hmm_mgr1 True """ # create YamldataWriter on file yw = YamldataWriter(file, stream_type=OnyxTextWriter.STREAM_TYPE, stream_version=OnyxTextWriter.STREAM_VERSION) stream = OnyxTextWriter() hdr_gen = stream.gen_header("HmmManager", "0") # The first arg is the collection name, the second arg is the object name, the third arg gives # the objects to be written and the fourth arg is called with each object and returns a # generator of tuples to be written for that object. The generator must yield at least one # tuple, which will be written as part of the name/index line; subsequent tuples go on their own # line. all_hmms = [hmm for hmm in hmm_mgr] mgr_gen = stream.gen_indexed_collection("HmmMgr", "Hmm", all_hmms, gen_hmm) yw.write_document(chain(hdr_gen, mgr_gen))