Ejemplo n.º 1
0
class PRPR_Constant_Precisions(OdeModel):
    def __init__(self, config):
        super(PRPR_Constant_Precisions, self).__init__(config)
        self.species = ["OD", "RFP", "YFP", "CFP", "F530", "F480"]
        self.n_species = 6
        self.precisions = NeuralPrecisions(self.n_species, config.params.n_hidden_decoder_precisions, 4)
        self.version = 1

    def initialize_state(self, theta, _treatments):
        n_batch = theta.get_n_batch()
        n_iwae = theta.get_n_samples()
        zero = torch.zeros([n_batch, n_iwae])
        x0 = torch.stack(
            [
                theta.init_x,
                theta.init_rfp,
                theta.init_yfp,
                theta.init_cfp,
                zero,
                zero,
                theta.init_prec_x,
                theta.init_prec_rfp,
                theta.init_prec_yfp,
                theta.init_prec_cfp,
            ],
            axis=2,
        )
        return x0

    def gen_reaction_equations(self, config, theta, treatments, dev_1hot):
        func = PRPR_Constant_RHS(config, theta, treatments, dev_1hot, precisions=self.precisions, version=self.version,)
        return func

    def summaries(self, writer, epoch):
        self.precisions.summaries(writer, epoch)
Ejemplo n.º 2
0
 def __init__(self, config):
     super(DR_Blackbox, self).__init__(config)
     self.n_x = config.params.n_x
     self.n_y = config.params.n_y
     self.n_z = config.params.n_z
     n_latents = self.n_x + self.n_y + self.n_z
     self.n_species = 4
     self.n_latent_species = config.params.n_latent_species
     self.n_hidden_precisions = config.params.n_hidden_decoder_precisions
     self.n_states = self.n_species + self.n_latent_species
     n_inputs = self.n_states + n_latents + self.n_treatments + self.device_depth
     self.precisions = NeuralPrecisions(n_inputs,
                                        self.n_hidden_precisions,
                                        4,
                                        hidden_activation=nn.ReLU)
     self.species = ["OD", "RFP", "YFP", "CFP"]
     # do the other inits now
     self.n_hidden = config.params.n_hidden_decoder
     self.init_latent_species = default_get_value(config.params,
                                                  "init_latent_species",
                                                  0.001)
     self.init_prec = default_get_value(config.params, "init_prec", 0.00001)
     self.offset_layer = nn.Linear(
         self.device_depth,
         self.n_y)  # Default initializer better than glorot_uniform here
     # nn.init.xavier_uniform_(self.offset_layer.weight)
     n = self.n_species + self.n_latent_species + n_latents + self.n_treatments + self.device_depth
     self.neural_states = NeuralStates(n, config.params.n_hidden_decoder,
                                       self.n_states, n_latents)
Ejemplo n.º 3
0
 def __init__(self, config):
     super(Inducer_Constant_Precisions, self).init_with_params(config)
     self.species = ["OD", "RFP", "YFP", "F530", "F480"]
     self.n_species = 5
     self.precisions = NeuralPrecisions(
         self.n_species, config.params.n_hidden_decoder_precisions, 4)
     self.version = 1
Ejemplo n.º 4
0
 def __init__(self, config):
     super(Auto_Constant_Precisions, self).__init__(config)
     self.species = ["OD", "RFP", "F530", "F480"]
     self.n_species = 4
     self.precisions = NeuralPrecisions(
         self.n_species, config.params.n_hidden_decoder_precisions, 4)
     self.version = 1
Ejemplo n.º 5
0
 def __init__(self, config):
     super(Degrader_Constant_Precisions, self).__init__(config)
     self.species = [
         "OD",
         "RFP",
         "YFP",
         "CFP",
         "F530",
         "F480",
         "LuxR",
         "LasR",
         "AiiA",
         "C6",
         "C12",
     ]
     self.n_species = 11
     self.precisions = NeuralPrecisions(self.n_species, config.params.n_hidden_decoder_precisions, 4)
     self.version = 1
Ejemplo n.º 6
0
 def __init__(self, config):
     super(Relay_Constant_Precisions, self).init_with_params(config)
     self.species = [
         "OD",
         "RFP",
         "YFP",
         "CFP",
         "F530",
         "F480",
         "LuxR",
         "LasR",
         "LasI",
         "LuxI",
         "C6",
         "C12",
     ]
     self.n_species = 12
     self.precisions = NeuralPrecisions(
         self.n_species, config.params.n_hidden_decoder_precisions, 4)
     self.version = 1
Ejemplo n.º 7
0
class DR_Constant_Precisions(DR_Constant):
    def __init__(self, config):
        super(DR_Constant_Precisions, self).__init__(config)
        self.species = ["OD", "RFP", "YFP", "CFP", "F530", "F480", "LuxR", "LasR"]
        self.n_species = 8
        self.precisions = NeuralPrecisions(self.n_species, config.params.n_hidden_decoder_precisions, 4)
        self.version = 1

    def initialize_state(self, theta, _treatments):
        n_batch = theta.get_n_batch()
        n_iwae = theta.get_n_samples()
        zero = torch.zeros([n_batch, n_iwae])
        x0 = torch.stack(
            [
                theta.init_x,
                theta.init_rfp,
                theta.init_yfp,
                theta.init_cfp,
                zero,
                zero,
                theta.init_luxR,
                theta.init_lasR,
                theta.init_prec_x,
                theta.init_prec_rfp,
                theta.init_prec_yfp,
                theta.init_prec_cfp,
            ],
            axis=2,
        )
        return x0

    def gen_reaction_equations(self, config, theta, treatments, dev_1hot):
        func = DR_Constant_RHS(config, theta, treatments, dev_1hot, precisions=self.precisions, version=self.version,)
        self.aR = func.aR
        self.aS = func.aS
        return func

    def summaries(self, writer, epoch):
        variable_summaries(writer, epoch, self.aR, "aR.conditioned")
        variable_summaries(writer, epoch, self.aS, "aS.conditioned")
        self.precisions.summaries(writer, epoch)
Ejemplo n.º 8
0
class DR_Blackbox(OdeModel):
    def __init__(self, config):
        super(DR_Blackbox, self).__init__(config)
        self.n_x = config.params.n_x
        self.n_y = config.params.n_y
        self.n_z = config.params.n_z
        n_latents = self.n_x + self.n_y + self.n_z
        self.n_species = 4
        self.n_latent_species = config.params.n_latent_species
        self.n_hidden_precisions = config.params.n_hidden_decoder_precisions
        self.n_states = self.n_species + self.n_latent_species
        n_inputs = self.n_states + n_latents + self.n_treatments + self.device_depth
        self.precisions = NeuralPrecisions(n_inputs,
                                           self.n_hidden_precisions,
                                           4,
                                           hidden_activation=nn.ReLU)
        self.species = ["OD", "RFP", "YFP", "CFP"]
        # do the other inits now
        self.n_hidden = config.params.n_hidden_decoder
        self.init_latent_species = default_get_value(config.params,
                                                     "init_latent_species",
                                                     0.001)
        self.init_prec = default_get_value(config.params, "init_prec", 0.00001)
        self.offset_layer = nn.Linear(
            self.device_depth,
            self.n_y)  # Default initializer better than glorot_uniform here
        # nn.init.xavier_uniform_(self.offset_layer.weight)
        n = self.n_species + self.n_latent_species + n_latents + self.n_treatments + self.device_depth
        self.neural_states = NeuralStates(n, config.params.n_hidden_decoder,
                                          self.n_states, n_latents)

    def condition_theta(self, theta, dev_1hot, writer, epoch):
        """Condition on device information by mapping param_cond = f(param, d; phi) where d is one-hot rep of device"""
        # n_batch = theta.get_n_batch()
        n_iwae = theta.get_n_samples()
        devices = dev_1hot.unsqueeze(1).repeat([1, n_iwae, 1])
        offset = self.offset_layer(devices)
        for i in range(self.n_y):
            pname = "y%d" % (i + 1)
            v = getattr(theta, pname) + offset[:, :, i]
            setattr(theta, pname, v)
        return theta

    def initialize_state(self, theta, _treatments):
        n_batch = theta.get_n_batch()
        n_iwae = theta.get_n_samples()
        x0 = torch.stack(
            [theta.init_x, theta.init_rfp, theta.init_yfp, theta.init_cfp],
            axis=2)
        h0 = torch.full([n_batch, n_iwae, self.n_latent_species],
                        self.init_latent_species)
        prec0 = torch.full([n_batch, n_iwae, 4], self.init_prec)
        return torch.cat([x0, h0, prec0], dim=2)

    def gen_reaction_equations(self, config, theta, treatments, dev_1hot):
        func = DR_Blackbox_RHS(
            self.neural_states,
            self.offset_layer,
            config,
            theta,
            treatments,
            dev_1hot,
            precisions=self.precisions,
        )
        return func

    def observe(self, x_sample, _theta):
        # x0 = [theta.x0, theta.rfp0, theta.yfp0, theta.cfp0]
        x_predict = [
            x_sample[:, :, 0, :],
            x_sample[:, :, 0, :] * x_sample[:, :, 1, :],
            x_sample[:, :, 0, :] * x_sample[:, :, 2, :],
            x_sample[:, :, 0, :] * x_sample[:, :, 3, :],
        ]
        x_predict = torch.stack(x_predict, axis=-1).permute(0, 1, 3, 2)
        return x_predict

    def summaries(self, writer, epoch):
        self.neural_states.summaries(writer, epoch)
        self.precisions.summaries(writer, epoch)
Ejemplo n.º 9
0
class Relay_Constant_Precisions(OdeModel):
    def __init__(self, config):
        super(Relay_Constant_Precisions, self).init_with_params(config)
        self.species = [
            "OD",
            "RFP",
            "YFP",
            "CFP",
            "F530",
            "F480",
            "LuxR",
            "LasR",
            "LasI",
            "LuxI",
            "C6",
            "C12",
        ]
        self.n_species = 12
        self.precisions = NeuralPrecisions(
            self.n_species, config.params.n_hidden_decoder_precisions, 4)
        self.version = 1

    def initialize_state(self, theta, _treatments):
        n_batch = theta.get_n_batch()
        n_iwae = theta.get_n_samples()
        zero = torch.zeros([n_batch, n_iwae])

        treatments_transformed = torch.clamp(
            torch.exp(_treatments) - 1.0, 1e-12, 1e6)
        c6a, c12a = torch.unbind(treatments_transformed, axis=1)
        c6 = torch.transpose(c6a.repeat([n_iwae, 1]), 0, 1)
        c12 = torch.transpose(c12a.repeat([n_iwae, 1]), 0, 1)

        x0 = torch.stack(
            [
                theta.init_x,
                theta.init_rfp,
                theta.init_yfp,
                theta.init_cfp,
                zero,
                zero,
                theta.init_luxR,
                theta.init_lasR,
                theta.init_luxI,
                theta.init_lasI,
                c6,
                c12,
                theta.init_prec_x,
                theta.init_prec_rfp,
                theta.init_prec_yfp,
                theta.init_prec_cfp,
            ],
            axis=2,
        )
        return x0

    def gen_reaction_equations(self,
                               config,
                               theta,
                               treatments,
                               dev_1hot,
                               condition_on_device=True):
        func = Relay_Constant_RHS(
            config,
            theta,
            treatments,
            dev_1hot,
            condition_on_device,
            precisions=self.precisions,
            version=self.version,
        )
        self.aR = func.aR
        self.aS = func.aS
        return func

    def summaries(self, writer, epoch):
        variable_summaries(writer, epoch, self.aR, "aR.conditioned")
        variable_summaries(writer, epoch, self.aS, "aS.conditioned")
        self.precisions.summaries(writer, epoch)