iam-git / WellMet (public) (License: MIT) (since 2021-08-31) (hash sha1)
WellMet is pure Python framework for spatial structural reliability analysis. Or, more specifically, for "failure probability estimation and detection of failure surfaces by adaptive sequential decomposition of the design domain".

/wellmet/misc.py (1820a0defee7802a776124d9f0aa6198d6976d28) (3234 bytes) (mode 100644) (type blob)

#!/usr/bin/env python
# coding: utf-8

import numpy as np
from scipy import interpolate

from . import sball # for Isocurves
from . import IS_stat

def pf_entropy(pf):
    return -pf*np.log(pf) - (1-pf)*np.log(1-pf)


class RBF_surrogate:
    def __init__(self, sample_box, space='R'):
        self.sample_box = sample_box
        self.space = space
        self._nsim = -100500
        self.update()
    
    def __call__(self, *args):
        self.update()
        self.rbf(*args)
    
    def update(self):
        if self.sample_box.nsim > self._nsim:
            sample_space = getattr(self.sample_box, self.space)
            self.rbf = interpolate.Rbf(*sample_space.T, self.sample_box.g_values, function='gaussian')
            self._nsim = self.sample_box.nsim

    def get_pf_estimation(self, nis=100000):
        self.update()
        nodes = IS_stat.IS_norm(self.sample_box.f_model, mean=0, std=2.5, sampling_space='G', nis=nis, design=None)
        gi = self.rbf(*getattr(nodes, self.space).T)
        return np.sum(nodes.w[gi<0])/nis



def get_isodistances(f_model, r, nrod=200):
    phi = np.linspace(0, 6.283185307, nrod, endpoint=True)
    cos_phi = np.cos(phi)
    sin_phi = np.sin(phi)
    
    sample_G = np.array((cos_phi, sin_phi)).T * r
    return f_model.new_sample(sample_G, space='G', extend=True)


def isolevels_2d(pdf, weighting_pdf_const, r_levels, from_top=None):
    """
    weighting_pdf_const = 1 / (xmax - xmin) / (ymax - ymin)
    """
    s_ball = sball.Sball(2) # nvar=2
    p_levels = []
    for r in r_levels:
        p_levels.append(1 - s_ball.get_pf(r))
    
    return isolevels(pdf, weighting_pdf_const, p_levels, from_top)




def isolevels(pdf, weighting_pdf_const, p_levels, from_top=None):
    """
    weighting_pdf_const = 1 / (xmax - xmin) / (ymax - ymin)
    """
    #č třeba P prostor doopravdy zlobí, takže zkusím nějak tak
    if from_top is None:
        weights = pdf / weighting_pdf_const
        p_all = np.sum(weights) / len(pdf)
        #č prečo víme, že celková pravděpodobnost může bejt nekoněčně velká
        if p_all <= 1:
            from_top = True
        else:
            from_top = False
            
    max_pdf = np.max(pdf)
    pdf_levels = []
    if from_top:
        # descending
        sorted_pdf = np.flip(np.sort(pdf))
        p_cumsum = np.cumsum(sorted_pdf) / weighting_pdf_const / len(pdf)
        for p in p_levels:
            # little bit tricky, didn't find numpy method for this
            mask = p_cumsum <= p
            level_down_bound = np.max(sorted_pdf[~mask], initial=0)
            level_up_bound = np.min(sorted_pdf[mask], initial=max_pdf)
            pdf_levels.append((level_down_bound + level_up_bound) / 2)
            
    else: # from bottom
        sorted_pdf = np.sort(pdf)
        p_cumsum = np.cumsum(sorted_pdf) / weighting_pdf_const / len(pdf)
        for p in p_levels:
            # little bit tricky, didn't find numpy method for this
            mask = p_cumsum <= 1-p
            level_down_bound = np.max(sorted_pdf[mask], initial=0)
            level_up_bound = np.min(sorted_pdf[~mask], initial=max_pdf)
            pdf_levels.append((level_down_bound + level_up_bound) / 2)

    return pdf_levels


Mode Type Size Ref File
100644 blob 26 aed04ad7c97da717e759111aa8dd7cd48768647f .gitignore
100644 blob 1093 263306d87c51114b1320be2ee3277ea0bff99b1f LICENSE
100644 blob 5165 c9a2ecc2110771d29b800aee6152fd3a3d239e80 README.md
100644 blob 2084 6cd17c9e68ac9734b1881157c553856bd2e034de cli_example.py
100644 blob 1257 52ad8257fd62a3dc12f8d08eaf73a7cfb5d392b8 gui_example.py
100644 blob 81 fed528d4a7a148fd0bf0b0198a6461f8c91b87e9 pyproject.toml
100644 blob 795 7f9286ab2094e7dddfb6e1c5e49396fa7d79e67c setup.cfg
100644 blob 54 ee2a480d94ead7579fdddabda39a672e31b90ced setup.py
040000 tree - c71333f92448098d44613a5de568eb3f3788abbe wellmet
Hints:
Before first commit, do not forget to setup your git environment:
git config --global user.name "your_name_here"
git config --global user.email "your@email_here"

Clone this repository using HTTP(S):
git clone https://rocketgit.com/user/iam-git/WellMet

Clone this repository using ssh (do not forget to upload a key first):
git clone ssh://rocketgit@ssh.rocketgit.com/user/iam-git/WellMet

Clone this repository using git:
git clone git://git.rocketgit.com/user/iam-git/WellMet

You are allowed to anonymously push to this repository.
This means that your pushed commits will automatically be transformed into a merge request:
... clone the repository ...
... make some changes and some commits ...
git push origin main