def write_pb2(cmpd, filename, binary=True): """Convert mb.Compound to Protobuf Message file. Parameters ---------- cmpd : mb.Compound filename : str binary: bool, default True If True, will print a binary file If False, will print to a text file """ cmpd_to_proto = {} root_proto = compound_pb2.Compound() root_proto = _mb_to_proto(cmpd, root_proto) cmpd_to_proto[cmpd] = root_proto for sub_cmpd in cmpd.successors(): parent_cmpd = sub_cmpd.parent sub_proto = cmpd_to_proto[parent_cmpd].children.add() sub_proto = _mb_to_proto(sub_cmpd, sub_proto) cmpd_to_proto[sub_cmpd] = sub_proto _add_proto_bonds(cmpd, root_proto) if binary: with open(filename, "wb") as f: f.write(root_proto.SerializeToString()) else: with open(filename, "w") as f: PrintMessage(root_proto, f)
def write_proto(message: Message, filename: str): if not os.path.exists(os.path.dirname(filename)): try: os.makedirs(os.path.dirname(filename)) except OSError as exc: # Guard against race condition if exc.errno != errno.EEXIST: raise binary_filename = filename + '.binarypb' with open(binary_filename, "wb") as f: f.write(message.SerializeToString()) text_filename = filename + '.textproto' with open(text_filename, "w") as f: # These comments point to the proto to use as a schema for IDEs. f.write("# proto-file: proto/npc_infos.proto\n") f.write("# proto-message: NpcInfos\n\n") PrintMessage(message, f)
def write_pb2(cmpd, filename, binary=True): """ Convert mb.Compound to Protobuf Message file Parameters --------- cmpd : mb.Compound filename : str binary: bool, default True If True, will print a binary file If False, will print to a text file Todo: This could be more elegantly detected Notes ---- Todo: Handle Ports in the protocol buffer (.proto) and in this writer/reader """ cmpd_to_proto = {} root_proto = compound_pb2.Compound() root_proto = _mb_to_proto(cmpd, root_proto) cmpd_to_proto[cmpd] = root_proto for sub_cmpd in cmpd.successors(): parent_cmpd = sub_cmpd.parent sub_proto = cmpd_to_proto[parent_cmpd].children.add() sub_proto = _mb_to_proto(sub_cmpd, sub_proto) cmpd_to_proto[sub_cmpd] = sub_proto _add_proto_bonds(cmpd, root_proto) if binary: with open(filename, 'wb') as f: f.write(root_proto.SerializeToString()) else: with open(filename, 'w') as f: PrintMessage(root_proto, f)
def identity_list(dim): """Returns the list of entries of a dim-dimensional identity matrix.""" ide = dim * dim * [0.0] for i in range(0, dim * dim, dim + 1): ide[i] = 1.0 return ide if __name__ == "__main__": # DP gamma hyperprior dp_prior = mixing_prior_pb2.DPPrior() dp_prior.gamma_prior.totalmass_prior.shape = 4.0 dp_prior.gamma_prior.totalmass_prior.rate = 2.0 with open("resources/asciipb/dp_gamma_prior.asciipb", "w") as f: PrintMessage(dp_prior, f) # PY fixed values py_prior = mixing_prior_pb2.PYPrior() py_prior.fixed_values.strength = 1.0 py_prior.fixed_values.discount = 0.1 with open("resources/asciipb/py_fixed.asciipb", "w") as f: PrintMessage(py_prior, f) # NNIG NGG hyperprior nnig_prior = hierarchy_prior_pb2.NNIGPrior() nnig_prior.ngg_prior.mean_prior.mean = 5.5 nnig_prior.ngg_prior.mean_prior.var = 2.25 nnig_prior.ngg_prior.var_scaling_prior.shape = 0.2 nnig_prior.ngg_prior.var_scaling_prior.rate = 0.6 nnig_prior.ngg_prior.shape = 1.5
def identity_list(dim): """Returns the list of entries of a dim-dimensional identity matrix.""" ide = dim * dim * [0.0] for i in range(0, dim * dim, dim + 1): ide[i] = 1.0 return ide if __name__ == "__main__": # DP gamma hyperprior dp_prior = mixing_prior_pb2.DPPrior() dp_prior.gamma_prior.totalmass_prior.shape = 4.0 dp_prior.gamma_prior.totalmass_prior.rate = 2.0 with open("resources/asciipb/dp_gamma_prior.asciipb", "w") as f: PrintMessage(dp_prior, f) # PY fixed values py_prior = mixing_prior_pb2.PYPrior() py_prior.fixed_values.strength = 1.0 py_prior.fixed_values.discount = 0.1 with open("resources/asciipb/py_fixed.asciipb", "w") as f: PrintMessage(py_prior, f) # LSB normal hyperprior dim = 2 mu00 = dim * [0.0] sig00 = [5.0 * _ for _ in identity_list(dim)] step = 0.025 n_comp = 3 lsb_prior = mixing_prior_pb2.LogSBPrior()