{"id":34073053,"url":"https://github.com/openpra-org/inverse-canopy","last_synced_at":"2026-04-11T10:01:49.602Z","repository":{"id":230083765,"uuid":"778431185","full_name":"openpra-org/inverse-canopy","owner":"openpra-org","description":"An inverse estimation technique for back-fitting conditional/functional event probability distributions in an event tree to match target end-state frequencies.","archived":false,"fork":false,"pushed_at":"2024-12-30T17:25:43.000Z","size":2436,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2024-12-30T18:19:28.945Z","etag":null,"topics":["event-tree","fault-tree","pra","probabilistic-risk-assessment","probability-distribution","reliability","reliability-engineering","risk-assessment"],"latest_commit_sha":null,"homepage":"https://pypi.org/project/inverse-canopy/","language":"Jupyter Notebook","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"agpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/openpra-org.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":"CODEOWNERS","security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-03-27T17:57:00.000Z","updated_at":"2024-12-30T17:25:45.000Z","dependencies_parsed_at":null,"dependency_job_id":"fae6d7df-8fb9-4bbf-965d-db688b7d366d","html_url":"https://github.com/openpra-org/inverse-canopy","commit_stats":null,"previous_names":["openpra-org/inverse-canopy"],"tags_count":6,"template":false,"template_full_name":null,"purl":"pkg:github/openpra-org/inverse-canopy","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/openpra-org%2Finverse-canopy","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/openpra-org%2Finverse-canopy/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/openpra-org%2Finverse-canopy/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/openpra-org%2Finverse-canopy/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/openpra-org","download_url":"https://codeload.github.com/openpra-org/inverse-canopy/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/openpra-org%2Finverse-canopy/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":27723359,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-12-14T02:00:11.348Z","response_time":56,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["event-tree","fault-tree","pra","probabilistic-risk-assessment","probability-distribution","reliability","reliability-engineering","risk-assessment"],"created_at":"2025-12-14T08:35:40.222Z","updated_at":"2025-12-14T08:35:41.457Z","avatar_url":"https://github.com/openpra-org.png","language":"Jupyter Notebook","funding_links":[],"categories":[],"sub_categories":[],"readme":"# InverseCanopy\n\n[![DOI](https://zenodo.org/badge/DOI/10.5281/zenodo.10888233.svg)](https://doi.org/10.5281/zenodo.10888233)\n\nBack-fit conditional/functional event probability distributions in an event tree to match target end-state\nfrequencies.\n\n## How to Use\n\nBelow is a step-by-step example of how to use `inverse-canopy` in your project.\n\n### Step 1: Install the Package\n\nFirst, you need to import `InverseCanopy` from `inverse_canopy`, along with `tensorflow` and `numpy`.\n\n```shell\n!pip install inverse-canopy\n```\n\nAlternatively, you can install the CUDA or Metal accelerated versions. No code changes are required as long as the\nappropriate version is installed.\n#### inverse-canopy CUDA [Linux]\n```shell\n# optionally, first check if an nvidia GPU is available\n!nvidia-smi\n!pip install inverse-canopy[cuda]\n```\n\n#### inverse-canopy Metal [macOS]\n```shell\n!pip install inverse-canopy[metal]\n```\n\n```python\nfrom inverse_canopy import InverseCanopy\nimport tensorflow as tf\nimport numpy as np\n```\n\n### Step 2: Define Your Parameters\n\nYou'll need to set up some parameters for the model to work with. These include the number of samples, learning rate, data type, and more.\n\n```python\ntunable = {\n 'num_samples': 1000000,                # Number of Monte Carlo samples, you don't need too many for smooth functions\n 'learning_rate': 0.1,                  # Learning rate for gradient updates\n 'dtype': tf.float64,                   # Use 64-bit floats for calculations\n 'epsilon': 1e-30,                      # Helps avoid log(0) errors\n 'max_steps': 5000,                     # Maximum optimization steps\n 'patience': 50,                        # Steps to wait for improvement before stopping\n 'initiating_event_frequency': 5.0e-1,  # set the initiating event (IE) frequency here\n 'freeze_initiating_event': True,       # set to False if you'd like to predict the IE frequency as well\n}\n```\n\n### Step 3: Set Up Conditional Events and End States\n\nDefine the conditional events and end states for your model. This includes names, bounds for mean and standard\ndeviation, initial values, and the probabilities for each end state.\n\n```python\nconditional_events = {\n    'names': ['OCSP', 'RSIG', 'RROD', 'SPTR', 'BPHR', 'DHRS|BPHR', 'DHRS|~SPTR', 'DHRL|~BPHR', 'DHRL|~DHRS|BPHR', 'DHRL|~DHRS|~SPTR'],\n    'bounds': {\n        'mean': {\n            'min': 1e-14,\n            'max': 1.00,\n        },\n        'std': {\n            'min': 1e-10,\n            'max': 1e8,\n        },\n     },\n    'initial': {\n       'mean': 5e-1,\n       'std': 1e8,\n    }\n}\n\nend_states = {\n    'OCSP-1': {\n        'sequence': [1, 0, 0, np.nan, 0, np.nan, np.nan, 0, np.nan, np.nan],\n        'probability': 3.24e-2,\n    },\n    'OCSP-2': {\n        'sequence': [1, 0, 0, np.nan, 0, np.nan, np.nan, 1, np.nan, np.nan],\n        'probability': 2.80e-10,\n    },\n    'OCSP-3': {\n        'sequence': [1, 0, 0, np.nan, 1, 0, np.nan, np.nan, 0, np.nan],\n        'probability': 5.81e-4,\n    },\n    'OCSP-4': {\n        'sequence': [1, 0, 0, np.nan, 1, 0, np.nan, np.nan, 1, np.nan],\n        'probability': 1.0e-11,\n    },\n    'OCSP-5': {\n        'sequence': [1, 0, 0, np.nan, 1, 1, np.nan, np.nan, np.nan, np.nan],\n        'probability': 1.9e-11,\n    },\n    'OCSP-6': {\n        'sequence': [1, 0, 1, 0, np.nan, np.nan, 0, np.nan, np.nan, 0],\n        'probability': 1.0e-11,\n    },\n    'OCSP-7': {\n        'sequence': [1, 0, 1, 0, np.nan, np.nan, 0, np.nan, np.nan, 1],\n        'probability': 1.0e-11,\n    },\n    'OCSP-8': {\n        'sequence': [1, 0, 1, 0, np.nan, np.nan, 1, np.nan, np.nan, np.nan],\n        'probability': 1.0e-11,\n    },\n\n    'OCSP-9': {\n        'sequence': [1, 0, 1, 1, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan],\n        'probability': 1.5e-10,\n    },\n\n    'OCSP-10': {\n        'sequence': [1, 1, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan],\n        'probability': 1.10e-7,\n    },    \n}\n```\n\n### Step 4: Initialize and Fit the Model\n\nCreate an instance of `InverseCanopy` with your conditional events, end states, and tunable parameters. Then, fit the model.\n\n```python\nmodel = InverseCanopy(conditional_events, end_states, tunable)\nmodel.fit(steps=tunable['max_steps'], patience=tunable['patience'], learning_rate=tunable['learning_rate'], legacy=False)\n```\n\n### Step 5: Summarize the Results\n\nFinally, you can summarize the results of the model. This can include showing a plot of the results and displaying metrics.\n\n```python\nmodel.summarize(show_plot=True, show_metrics=True)\n```\n\nAnd that's it! You've successfully used `inverse-canopy` to back-fit conditional/functional event probabilities in an \nevent tree.\n\n\n## Example Output\nCheckout the [demo jupyter notebook](notebooks/demo.ipynb).\n\n```jupyterpython\nmodel = InverseCanopy(conditional_events, end_states, tunable)\nmodel.fit(steps=tunable['max_steps'], patience=tunable['patience'], learning_rate=tunable['learning_rate'], legacy=False)\n```\n```pycon\ntunable initialized: dtype=\u003cdtype: 'float64'\u003e, epsilon=1e-30\nlearning_rate: 0.1,patience: 50,min_improvement: 0.001,max_steps: 5000,seed: 372\nStep 0: Loss = 10.2756362110866064, performing 182.7 it/sec\nStep 100: Loss = 1.0622255351865935, performing 1035.0 it/sec\nStep 200: Loss = 0.6733805583746367, performing 1026.8 it/sec\nStep 300: Loss = 0.4514006773778768, performing 1039.3 it/sec\nStep 400: Loss = 0.2480285319362284, performing 1061.4 it/sec\nStep 500: Loss = 0.0719976443349221, performing 1113.5 it/sec\nStep 600: Loss = 0.0155362116961599, performing 1122.6 it/sec\nNo improvement since Step 603, early stopping.\n[Best]  Step 602: Loss = 0.0094149955203317\n[Final] Step 652: Loss = 0.0167395344814137\n\npredicted end states\n-------------------------------------\n          5th       Mean      95th\nOCSP-1   3.24e-02  3.24e-02  3.24e-02\nOCSP-2   2.73e-10  2.78e-10  2.84e-10\nOCSP-3   5.77e-04  5.77e-04  5.77e-04\nOCSP-4   9.86e-12  9.94e-12  1.00e-11\nOCSP-5   1.89e-11  1.90e-11  1.91e-11\nOCSP-6   9.67e-12  9.98e-12  1.03e-11\nOCSP-7   9.72e-12  1.00e-11  1.04e-11\nOCSP-8   9.74e-12  1.00e-11  1.04e-11\nOCSP-9   1.46e-10  1.51e-10  1.56e-10\nOCSP-10  1.10e-07  1.10e-07  1.10e-07\n\n\npredicted conditional events\n----------------------------------------------\n                   5th       Mean      95th\nOCSP              1.00e+00  1.00e+00  1.00e+00\nRSIG              3.33e-06  3.33e-06  3.33e-06\nRROD              5.31e-09  5.48e-09  5.65e-09\nSPTR              8.34e-01  8.34e-01  8.34e-01\nBPHR              1.75e-02  1.75e-02  1.75e-02\nDHRS|BPHR         3.28e-08  3.29e-08  3.31e-08\nDHRS|~SPTR        3.34e-01  3.34e-01  3.34e-01\nDHRL|~BPHR        8.41e-09  8.57e-09  8.75e-09\nDHRL|~DHRS|BPHR   1.71e-08  1.72e-08  1.74e-08\nDHRL|~DHRS|~SPTR  5.01e-01  5.01e-01  5.01e-01\n```\n\n```jupyterpython\nmodel.summarize(show_plot=True, show_metrics=False)\n```\n![output](https://gcdnb.pbrd.co/images/DDj4Nv536IJh.png?o=1 \"demo output plot\")\n\n## Development\n\nInstall dev packages as `pip install -e \".[dev]\"`\n\n### Upload to PyPI\n\n- `python setup.py sdist bdist_wheel`\n- `pip install twine`\n- `twine upload dist/*`\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fopenpra-org%2Finverse-canopy","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fopenpra-org%2Finverse-canopy","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fopenpra-org%2Finverse-canopy/lists"}