Ejemplo n.º 1
0
 def parameters(self, first_theta):
     """Get a parameter set for this theta record.
     first_theta is the number of the first theta in this record
     """
     pset = ParameterSet()
     current_theta = first_theta
     for theta in self.root.all('theta'):
         init = theta.init.tokens[0].eval
         fix = bool(theta.find('FIX'))
         if theta.find('low'):
             if theta.low.find('NEG_INF'):
                 lower = min_lower_bound
             else:
                 lower = theta.low.tokens[0].eval
         else:
             lower = min_lower_bound
         if theta.find('up'):
             if theta.up.find('POS_INF'):
                 upper = max_upper_bound
             else:
                 upper = theta.up.tokens[0].eval
         else:
             upper = max_upper_bound
         multiple = theta.find('n')
         if multiple:
             n = multiple.INT
         else:
             n = 1
         for i in range(0, n):
             new_par = Parameter(f'THETA({current_theta})', init, lower,
                                 upper, fix)
             current_theta += 1
             pset.add(new_par)
     return pset
Ejemplo n.º 2
0
def test_pset_remove_fixed():
    p1 = Parameter('Y', 9, fix=False)
    p2 = Parameter('X', 3, fix=True)
    p3 = Parameter('Z', 1, fix=False)
    pset = ParameterSet([p1, p2, p3])
    pset.remove_fixed()
    assert len(pset) == 2
    assert pset['Y'] == Parameter('Y', 9)
Ejemplo n.º 3
0
def test_pset_fix():
    p1 = Parameter('Y', 9, fix=False)
    p2 = Parameter('X', 3, fix=True)
    p3 = Parameter('Z', 1, fix=False)
    pset = ParameterSet([p1, p2, p3])
    assert pset.fix == {'Y': False, 'X': True, 'Z': False}
    fixedness = {'Y': True, 'X': True, 'Z': True}
    pset.fix = fixedness
    assert pset.fix == {'Y': True, 'X': True, 'Z': True}
Ejemplo n.º 4
0
def test_pset_eq():
    p1 = Parameter('Y', 9)
    p2 = Parameter('X', 3)
    p3 = Parameter('Z', 1)
    pset1 = ParameterSet([p1, p2, p3])
    pset2 = ParameterSet([p1, p2])
    assert pset1 != pset2
    pset3 = ParameterSet([p1, p3, p2])
    assert pset1 != pset3
    assert pset1 == pset1
Ejemplo n.º 5
0
    def parameters(self):
        """Get the ParameterSet of all parameters
        """
        try:
            return self._parameters
        except AttributeError:
            pass

        next_theta = 1
        params = ParameterSet()
        for theta_record in self.control_stream.get_records('THETA'):
            thetas = theta_record.parameters(next_theta)
            params.update(thetas)
            next_theta += len(thetas)
        next_omega = 1
        previous_size = None
        for omega_record in self.control_stream.get_records('OMEGA'):
            omegas, next_omega, previous_size = omega_record.parameters(
                next_omega, previous_size)
            params.update(omegas)
        next_sigma = 1
        previous_size = None
        for sigma_record in self.control_stream.get_records('SIGMA'):
            sigmas, next_sigma, previous_size = sigma_record.parameters(
                next_sigma, previous_size)
            params.update(sigmas)
        self._parameters = params
        return params
Ejemplo n.º 6
0
def test_pset_names():
    p1 = Parameter('Y', 9)
    p2 = Parameter('X', 3)
    p3 = Parameter('Z', 1)
    pset = ParameterSet([p1, p2, p3])
    assert pset.names == ['Y', 'X', 'Z']
    assert pset.symbols == [symbol('Y'), symbol('X'), symbol('Z')]
Ejemplo n.º 7
0
def test_pset_index():
    p = Parameter('Y', 9)
    pset = ParameterSet((p, ))
    assert len(pset) == 1
    assert pset['Y'] is p

    p2 = Parameter('Z', 5)
    pset.add(p2)

    assert len(pset) == 2

    # Check that the parameter set keeps the insertion order upon iteration
    for i, param in enumerate(pset):
        if i == 0:
            assert param is p
        else:
            assert param is p2
Ejemplo n.º 8
0
def test_pset_inits():
    p1 = Parameter('Y', 9)
    p2 = Parameter('X', 3)
    p3 = Parameter('Z', 1)
    pset = ParameterSet([p1, p2, p3])
    pset.inits = {'X': 28}
    assert len(pset) == 3
    assert pset['X'] == Parameter('X', 28)
    assert pset['Y'] == Parameter('Y', 9)
    assert pset['Z'] == Parameter('Z', 1)

    with pytest.raises(KeyError):
        pset.inits = {'CL': 0}

    pset.inits = {'X': 0, 'Y': 2, 'Z': 5}
    assert len(pset) == 3
    assert pset['X'] == Parameter('X', 0)
    assert pset['Y'] == Parameter('Y', 2)
    assert pset['Z'] == Parameter('Z', 5)
Ejemplo n.º 9
0
def test_pset_repr():
    p1 = Parameter('Y', 9, fix=False)
    pset = ParameterSet([p1])
    assert type(repr(pset)) == str
    assert type(pset._repr_html_()) == str
    pset = ParameterSet()
    assert type(repr(pset)) == str
    assert type(pset._repr_html_()) == str
Ejemplo n.º 10
0
 def _read_parameters(self):
     next_theta = 1
     params = ParameterSet()
     for theta_record in self.control_stream.get_records('THETA'):
         thetas = theta_record.parameters(next_theta,
                                          seen_labels=set(params.names))
         params.update(thetas)
         next_theta += len(thetas)
     next_omega = 1
     previous_size = None
     for omega_record in self.control_stream.get_records('OMEGA'):
         omegas, next_omega, previous_size = omega_record.parameters(
             next_omega, previous_size, seen_labels=set(params.names))
         params.update(omegas)
     next_sigma = 1
     previous_size = None
     for sigma_record in self.control_stream.get_records('SIGMA'):
         sigmas, next_sigma, previous_size = sigma_record.parameters(
             next_sigma, previous_size, seen_labels=set(params.names))
         params.update(sigmas)
     self._parameters = params
     self._old_parameters = params.copy()
Ejemplo n.º 11
0
 def parameters(self, start_omega, previous_size):
     """Get a ParameterSet for this omega record
     """
     row = start_omega
     block = self.root.find('block')
     bare_block = self.root.find('bare_block')
     same = bool(self.root.find('same'))
     parameters = ParameterSet()
     if not (block or bare_block):
         for node in self.root.all('diag_item'):
             init = node.init.NUMERIC
             fixed = bool(node.find('FIX'))
             sd = bool(node.find('SD'))
             var = bool(node.find('VAR'))
             n = node.n.INT if node.find('n') else 1
             if sd and var:
                 raise ModelSyntaxError(f'Initial estimate for {self.name.upper} cannot be both'
                                        f' on SD and VAR scale\n{self.root}')
             if init == 0 and not fixed:
                 raise ModelSyntaxError(f'If initial estimate for {self.name.upper} is 0 it'
                                        f' must be set to FIX')
             if sd:
                 init = init ** 2
             for _ in range(n):
                 name = f'{self.name}({row},{row})'
                 param = Parameter(name, init, lower=0, fix=fixed)
                 parameters.add(param)
                 row += 1
         size = 1
         next_omega = row
     else:
         inits = []
         if bare_block:
             size = previous_size
         else:
             size = self.root.block.size.INT
         fix, sd, corr, cholesky = self._block_flags()
         for node in self.root.all('omega'):
             init = node.init.NUMERIC
             n = node.n.INT if node.find('n') else 1
             inits += [init] * n
         if not same:
             if size != pharmpy.math.triangular_root(len(inits)):
                 raise ModelSyntaxError('Wrong number of inits in BLOCK')
             if not cholesky:
                 A = pharmpy.math.flattened_to_symmetric(inits)
                 if corr:
                     for i in range(size):
                         for j in range(size):
                             if i != j:
                                 if sd:
                                     A[i, j] = A[i, i] * A[j, j] * A[i, j]
                                 else:
                                     A[i, j] = math.sqrt(A[i, i]) * math.sqrt(A[j, j]) * A[i, j]
                 if sd:
                     np.fill_diagonal(A, A.diagonal()**2)
             else:
                 L = np.zeros((size, size))
                 inds = np.tril_indices_from(L)
                 L[inds] = inits
                 A = L @ L.T
             for i in range(size):
                 for j in range(0, i + 1):
                     name = f'{self.name}({i + start_omega},{j + start_omega})'
                     init = A[i, j]
                     lower = None if i != j else 0
                     param = Parameter(name, init, lower=lower, fix=fix)
                     parameters.add(param)
         next_omega = start_omega + size
     return parameters, next_omega, size
Ejemplo n.º 12
0
def test_pset_lower_upper():
    p1 = Parameter('X', 0, lower=-1, upper=1)
    p2 = Parameter('Y', 1, lower=0)
    pset = ParameterSet([p1, p2])
    assert pset.lower == {'X': -1, 'Y': 0}
    assert pset.upper == {'X': 1, 'Y': sympy.oo}
Ejemplo n.º 13
0
    def parameters(self, first_theta, seen_labels=None):
        """Get a parameter set for this theta record.
        first_theta is the number of the first theta in this record
        """
        if seen_labels is None:
            seen_labels = set()
        pset = ParameterSet()
        current_theta = first_theta
        for theta in self.root.all('theta'):
            init = theta.init.tokens[0].eval
            fix = bool(theta.find('FIX'))
            if theta.find('low'):
                if theta.low.find('NEG_INF'):
                    lower = min_lower_bound
                else:
                    lower = theta.low.tokens[0].eval
            else:
                lower = min_lower_bound
            if theta.find('up'):
                if theta.up.find('POS_INF'):
                    upper = max_upper_bound
                else:
                    upper = theta.up.tokens[0].eval
            else:
                upper = max_upper_bound
            multiple = theta.find('n')
            if multiple:
                n = multiple.INT
            else:
                n = 1
            for i in range(0, n):
                name = None
                import pharmpy.plugins.nonmem as nonmem

                if nonmem.conf.parameter_names == 'comment':
                    # needed to avoid circular import with Python 3.6
                    found = False
                    for subnode in self.root.tree_walk():
                        if id(subnode) == id(theta):
                            if found:
                                break
                            else:
                                found = True
                                continue
                        if found and subnode.rule == 'NEWLINE':
                            m = re.search(r';\s*([a-zA-Z_]\w*)', str(subnode))
                            if m:
                                name = m.group(1)
                                break
                    if name in seen_labels:
                        warnings.warn(
                            f'The parameter name {name} is duplicated. Falling back to basic '
                            f'NONMEM names.')
                        name = None

                if not name:
                    name = f'THETA({current_theta})'
                seen_labels.add(name)
                new_par = Parameter(name, init, lower, upper, fix)
                current_theta += 1
                pset.add(new_par)

        if not self.name_map:
            self.name_map = {
                name: first_theta + i
                for i, name in enumerate(pset.names)
            }
        return pset