Old engine for Continuous Time Bayesian Networks. Superseded by reCTBN. 🐍
https://github.com/madlabunimib/PyCTBN
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
303 lines
21 KiB
303 lines
21 KiB
|
|
|
|
|
|
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
|
|
<html xml:lang="" lang="" version="-//W3C//DTD XHTML 1.1//EN" xmlns="http://www.w3.org/1999/xhtml">
|
|
<head>
|
|
<meta charset="utf-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
|
|
|
|
<title>Examples — PyCTBN 2.0 documentation</title>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<link href='https://fonts.googleapis.com/css?family=Lato:400,700,400italic,700italic|Roboto+Slab:400,700|Inconsolata:400,700' rel='stylesheet' type='text/css'/>
|
|
<link rel="stylesheet" href="_static/css/pdj.css" type="text/css" />
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<link rel="index" title="Index"
|
|
href="genindex.html"/>
|
|
<link rel="search" title="Search" href="search.html"/>
|
|
<link rel="top" title="PyCTBN 2.0 documentation" href="index.html"/>
|
|
<link rel="prev" title="PyCTBN.PyCTBN.utility package" href="PyCTBN.PyCTBN.utility.html"/>
|
|
|
|
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
|
|
<meta http-equiv="cache-control" content="public" />
|
|
<meta name="robots" content="follow, all" />
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
|
<!-- Add jQuery library -->
|
|
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
|
|
<script src="https://cdnjs.cloudflare.com/ajax/libs/modernizr/2.6.2/modernizr.min.js"></script>
|
|
|
|
</head>
|
|
|
|
<body class="wy-body-for-nav" role="document">
|
|
|
|
<div class="wy-grid-for-nav">
|
|
<nav data-toggle="wy-nav-shift" class="wy-nav-side">
|
|
<div class="wy-side-nav-search">
|
|
<a href="index.html" class="fa fa-home"> PyCTBN </a>
|
|
<div role="search">
|
|
<form id ="rtd-search-form" class="wy-form"
|
|
action="search.html" method="get">
|
|
<input type="text" name="q" placeholder="Search docs" />
|
|
<input type="hidden" name="check_keywords" value="yes" />
|
|
<input type="hidden" name="area" value="default" />
|
|
</form>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
|
|
|
|
|
|
|
|
<p class="caption"><span class="caption-text">Contents:</span></p>
|
|
<ul class="current">
|
|
<li class="toctree-l1"><a class="reference internal" href="PyCTBN.PyCTBN.html">PyCTBN.PyCTBN package</a><ul>
|
|
<li class="toctree-l2"><a class="reference internal" href="PyCTBN.PyCTBN.html#subpackages">Subpackages</a></li>
|
|
<li class="toctree-l2"><a class="reference internal" href="PyCTBN.PyCTBN.html#module-PyCTBN.PyCTBN">Module contents</a></li>
|
|
</ul>
|
|
</li>
|
|
<li class="toctree-l1 current"><a class="current reference internal" href="#">Examples</a><ul>
|
|
<li class="toctree-l2"><a class="reference internal" href="#installation-usage">Installation/Usage</a></li>
|
|
<li class="toctree-l2"><a class="reference internal" href="#implementing-your-own-data-importer">Implementing your own data importer</a></li>
|
|
<li class="toctree-l2"><a class="reference internal" href="#parameters-estimation-example">Parameters Estimation Example</a></li>
|
|
<li class="toctree-l2"><a class="reference internal" href="#structure-estimation-example">Structure Estimation Example</a></li>
|
|
</ul>
|
|
</li>
|
|
</ul>
|
|
|
|
|
|
|
|
|
|
</div>
|
|
|
|
</nav>
|
|
<section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">
|
|
|
|
<nav class="wy-nav-top" id="barra-mobile" role="navigation" aria-label="top navigation">
|
|
<i data-toggle="wy-nav-top" class="fa fa-bars"></i>
|
|
<a href="#">Porão do Juca</a>
|
|
</nav>
|
|
|
|
<div class="wy-nav-content">
|
|
<div class="fundo-claro">
|
|
</div>
|
|
<div class="fundo-escuro">
|
|
</div>
|
|
|
|
<div class="rst-content">
|
|
<div role="navigation" aria-label="breadcrumbs navigation">
|
|
|
|
<!-- <ul class="wy-breadcrumbs"> -->
|
|
<!-- <li><a href="#">Docs</a> »</li> -->
|
|
|
|
<!-- <li>Features</li> -->
|
|
<!-- <li class="wy-breadcrumbs-aside"> -->
|
|
|
|
<!-- <a href="_sources/index.txt" rel="nofollow"> View page source</a> -->
|
|
|
|
<!-- </li> -->
|
|
<!-- </ul> -->
|
|
<!-- <hr/> -->
|
|
</div>
|
|
|
|
<div role="main" class="">
|
|
|
|
<div id="content" class="hfeed entry-container hentry">
|
|
<div class="section" id="examples">
|
|
<h1>Examples<a class="headerlink" href="#examples" title="Permalink to this headline">¶</a></h1>
|
|
<div class="section" id="installation-usage">
|
|
<h2>Installation/Usage<a class="headerlink" href="#installation-usage" title="Permalink to this headline">¶</a></h2>
|
|
<p>Download the release in .tar.gz or .whl format and simply use pip install to install it:</p>
|
|
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span>$pip install PyCTBN-1.0.tar.gz
|
|
</pre></div>
|
|
</div>
|
|
</div>
|
|
<div class="section" id="implementing-your-own-data-importer">
|
|
<h2>Implementing your own data importer<a class="headerlink" href="#implementing-your-own-data-importer" title="Permalink to this headline">¶</a></h2>
|
|
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="sd">"""This example demonstrates the implementation of a simple data importer the extends the class abstract importer to import data in csv format.</span>
|
|
<span class="sd">The net in exam has three ternary nodes and no prior net structure.</span>
|
|
<span class="sd">"""</span>
|
|
|
|
<span class="kn">from</span> <span class="nn">PyCTBN</span> <span class="kn">import</span> <span class="n">AbstractImporter</span>
|
|
|
|
<span class="k">class</span> <span class="nc">CSVImporter</span><span class="p">(</span><span class="n">AbstractImporter</span><span class="p">):</span>
|
|
|
|
<span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">file_path</span><span class="p">):</span>
|
|
<span class="bp">self</span><span class="o">.</span><span class="n">_df_samples_list</span> <span class="o">=</span> <span class="kc">None</span>
|
|
<span class="nb">super</span><span class="p">(</span><span class="n">CSVImporter</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="n">file_path</span><span class="p">)</span>
|
|
|
|
<span class="k">def</span> <span class="nf">import_data</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
|
|
<span class="bp">self</span><span class="o">.</span><span class="n">read_csv_file</span><span class="p">()</span>
|
|
<span class="bp">self</span><span class="o">.</span><span class="n">_sorter</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">build_sorter</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_df_samples_list</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span>
|
|
<span class="bp">self</span><span class="o">.</span><span class="n">import_variables</span><span class="p">()</span>
|
|
<span class="bp">self</span><span class="o">.</span><span class="n">compute_row_delta_in_all_samples_frames</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_df_samples_list</span><span class="p">)</span>
|
|
|
|
<span class="k">def</span> <span class="nf">read_csv_file</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
|
|
<span class="n">df</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">read_csv</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_file_path</span><span class="p">)</span>
|
|
<span class="n">df</span><span class="o">.</span><span class="n">drop</span><span class="p">(</span><span class="n">df</span><span class="o">.</span><span class="n">columns</span><span class="p">[[</span><span class="mi">0</span><span class="p">]],</span> <span class="n">axis</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">inplace</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
|
|
<span class="bp">self</span><span class="o">.</span><span class="n">_df_samples_list</span> <span class="o">=</span> <span class="p">[</span><span class="n">df</span><span class="p">]</span>
|
|
|
|
<span class="k">def</span> <span class="nf">import_variables</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
|
|
<span class="n">values_list</span> <span class="o">=</span> <span class="p">[</span><span class="mi">3</span> <span class="k">for</span> <span class="n">var</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_sorter</span><span class="p">]</span>
|
|
<span class="c1"># initialize dict of lists</span>
|
|
<span class="n">data</span> <span class="o">=</span> <span class="p">{</span><span class="s1">'Name'</span><span class="p">:</span><span class="bp">self</span><span class="o">.</span><span class="n">_sorter</span><span class="p">,</span> <span class="s1">'Value'</span><span class="p">:</span><span class="n">values_list</span><span class="p">}</span>
|
|
<span class="c1"># Create the pandas DataFrame</span>
|
|
<span class="bp">self</span><span class="o">.</span><span class="n">_df_variables</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>
|
|
|
|
<span class="k">def</span> <span class="nf">build_sorter</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">sample_frame</span><span class="p">:</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">)</span> <span class="o">-></span> <span class="n">typing</span><span class="o">.</span><span class="n">List</span><span class="p">:</span>
|
|
<span class="k">return</span> <span class="nb">list</span><span class="p">(</span><span class="n">sample_frame</span><span class="o">.</span><span class="n">columns</span><span class="p">)[</span><span class="mi">1</span><span class="p">:]</span>
|
|
|
|
<span class="k">def</span> <span class="nf">dataset_id</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-></span> <span class="nb">object</span><span class="p">:</span>
|
|
<span class="k">pass</span>
|
|
</pre></div>
|
|
</div>
|
|
</div>
|
|
<div class="section" id="parameters-estimation-example">
|
|
<h2>Parameters Estimation Example<a class="headerlink" href="#parameters-estimation-example" title="Permalink to this headline">¶</a></h2>
|
|
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">PyCTBN</span> <span class="kn">import</span> <span class="n">JsonImporter</span>
|
|
<span class="kn">from</span> <span class="nn">PyCTBN</span> <span class="kn">import</span> <span class="n">SamplePath</span>
|
|
<span class="kn">from</span> <span class="nn">PyCTBN</span> <span class="kn">import</span> <span class="n">NetworkGraph</span>
|
|
<span class="kn">from</span> <span class="nn">PyCTBN</span> <span class="kn">import</span> <span class="n">ParametersEstimator</span>
|
|
|
|
|
|
<span class="k">def</span> <span class="nf">main</span><span class="p">():</span>
|
|
<span class="n">read_files</span> <span class="o">=</span> <span class="n">glob</span><span class="o">.</span><span class="n">glob</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="s1">'./data'</span><span class="p">,</span> <span class="s2">"*.json"</span><span class="p">))</span> <span class="c1">#Take all json files in this dir</span>
|
|
<span class="c1">#import data</span>
|
|
<span class="n">importer</span> <span class="o">=</span> <span class="n">JsonImporter</span><span class="p">(</span><span class="n">read_files</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="s1">'samples'</span><span class="p">,</span> <span class="s1">'dyn.str'</span><span class="p">,</span> <span class="s1">'variables'</span><span class="p">,</span> <span class="s1">'Time'</span><span class="p">,</span> <span class="s1">'Name'</span><span class="p">)</span>
|
|
<span class="n">importer</span><span class="o">.</span><span class="n">import_data</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>
|
|
<span class="c1">#Create a SamplePath Obj passing an already filled AbstractImporter object</span>
|
|
<span class="n">s1</span> <span class="o">=</span> <span class="n">SamplePath</span><span class="p">(</span><span class="n">importer</span><span class="p">)</span>
|
|
<span class="c1">#Build The trajectries and the structural infos</span>
|
|
<span class="n">s1</span><span class="o">.</span><span class="n">build_trajectories</span><span class="p">()</span>
|
|
<span class="n">s1</span><span class="o">.</span><span class="n">build_structure</span><span class="p">()</span>
|
|
<span class="nb">print</span><span class="p">(</span><span class="n">s1</span><span class="o">.</span><span class="n">structure</span><span class="o">.</span><span class="n">edges</span><span class="p">)</span>
|
|
<span class="nb">print</span><span class="p">(</span><span class="n">s1</span><span class="o">.</span><span class="n">structure</span><span class="o">.</span><span class="n">nodes_values</span><span class="p">)</span>
|
|
<span class="c1">#From The Structure Object build the Graph</span>
|
|
<span class="n">g</span> <span class="o">=</span> <span class="n">NetworkGraph</span><span class="p">(</span><span class="n">s1</span><span class="o">.</span><span class="n">structure</span><span class="p">)</span>
|
|
<span class="c1">#Select a node you want to estimate the parameters</span>
|
|
<span class="n">node</span> <span class="o">=</span> <span class="n">g</span><span class="o">.</span><span class="n">nodes</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span>
|
|
<span class="nb">print</span><span class="p">(</span><span class="s2">"Node"</span><span class="p">,</span> <span class="n">node</span><span class="p">)</span>
|
|
<span class="c1">#Init the _graph specifically for THIS node</span>
|
|
<span class="n">g</span><span class="o">.</span><span class="n">fast_init</span><span class="p">(</span><span class="n">node</span><span class="p">)</span>
|
|
<span class="c1">#Use SamplePath and Grpah to create a ParametersEstimator Object</span>
|
|
<span class="n">p1</span> <span class="o">=</span> <span class="n">ParametersEstimator</span><span class="p">(</span><span class="n">s1</span><span class="o">.</span><span class="n">trajectories</span><span class="p">,</span> <span class="n">g</span><span class="p">)</span>
|
|
<span class="c1">#Init the peEst specifically for THIS node</span>
|
|
<span class="n">p1</span><span class="o">.</span><span class="n">fast_init</span><span class="p">(</span><span class="n">node</span><span class="p">)</span>
|
|
<span class="c1">#Compute the parameters</span>
|
|
<span class="n">sofc1</span> <span class="o">=</span> <span class="n">p1</span><span class="o">.</span><span class="n">compute_parameters_for_node</span><span class="p">(</span><span class="n">node</span><span class="p">)</span>
|
|
<span class="c1">#The est CIMS are inside the resultant SetOfCIms Obj</span>
|
|
<span class="nb">print</span><span class="p">(</span><span class="n">sofc1</span><span class="o">.</span><span class="n">actual_cims</span><span class="p">)</span>
|
|
</pre></div>
|
|
</div>
|
|
</div>
|
|
<div class="section" id="structure-estimation-example">
|
|
<h2>Structure Estimation Example<a class="headerlink" href="#structure-estimation-example" title="Permalink to this headline">¶</a></h2>
|
|
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">PyCTBN</span> <span class="kn">import</span> <span class="n">JsonImporter</span>
|
|
<span class="kn">from</span> <span class="nn">PyCTBN</span> <span class="kn">import</span> <span class="n">SamplePath</span>
|
|
<span class="kn">from</span> <span class="nn">PyCTBN</span> <span class="kn">import</span> <span class="n">StructureEstimator</span>
|
|
|
|
<span class="k">def</span> <span class="nf">structure_estimation_example</span><span class="p">():</span>
|
|
|
|
<span class="c1"># read the json files in ./data path</span>
|
|
<span class="n">read_files</span> <span class="o">=</span> <span class="n">glob</span><span class="o">.</span><span class="n">glob</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="s1">'./data'</span><span class="p">,</span> <span class="s2">"*.json"</span><span class="p">))</span>
|
|
<span class="c1"># initialize a JsonImporter object for the first file</span>
|
|
<span class="n">importer</span> <span class="o">=</span> <span class="n">JsonImporter</span><span class="p">(</span><span class="n">read_files</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="s1">'samples'</span><span class="p">,</span> <span class="s1">'dyn.str'</span><span class="p">,</span> <span class="s1">'variables'</span><span class="p">,</span> <span class="s1">'Time'</span><span class="p">,</span> <span class="s1">'Name'</span><span class="p">)</span>
|
|
<span class="c1"># import the data at index 0 of the outer json array</span>
|
|
<span class="n">importer</span><span class="o">.</span><span class="n">import_data</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>
|
|
<span class="c1"># construct a SamplePath Object passing a filled AbstractImporter</span>
|
|
<span class="n">s1</span> <span class="o">=</span> <span class="n">SamplePath</span><span class="p">(</span><span class="n">importer</span><span class="p">)</span>
|
|
<span class="c1"># build the trajectories</span>
|
|
<span class="n">s1</span><span class="o">.</span><span class="n">build_trajectories</span><span class="p">()</span>
|
|
<span class="c1"># build the real structure</span>
|
|
<span class="n">s1</span><span class="o">.</span><span class="n">build_structure</span><span class="p">()</span>
|
|
<span class="c1"># construct a StructureEstimator object</span>
|
|
<span class="n">se1</span> <span class="o">=</span> <span class="n">StructureEstimator</span><span class="p">(</span><span class="n">s1</span><span class="p">,</span> <span class="mf">0.1</span><span class="p">,</span> <span class="mf">0.1</span><span class="p">)</span>
|
|
<span class="c1"># call the ctpc algorithm</span>
|
|
<span class="n">se1</span><span class="o">.</span><span class="n">ctpc_algorithm</span><span class="p">()</span>
|
|
<span class="c1"># the adjacency matrix of the estimated structure</span>
|
|
<span class="nb">print</span><span class="p">(</span><span class="n">se1</span><span class="o">.</span><span class="n">adjacency_matrix</span><span class="p">())</span>
|
|
<span class="c1"># save results to a json file</span>
|
|
<span class="n">se1</span><span class="o">.</span><span class="n">save_results</span><span class="p">()</span>
|
|
</pre></div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
|
|
</div>
|
|
<footer>
|
|
|
|
<div class="rst-footer-buttons" role="navigation" aria-label="footer navigation">
|
|
|
|
|
|
<a href="PyCTBN.PyCTBN.utility.html" class="btn btn-neutral" title="PyCTBN.PyCTBN.utility package"><span class="fa fa-arrow-circle-left"></span> Previous</a>
|
|
|
|
</div>
|
|
|
|
|
|
<hr/>
|
|
|
|
<div role="contentinfo">
|
|
<p>
|
|
© Copyright 2021, Bregoli Alessandro, Martini Filippo, Moretti Luca.
|
|
</p>
|
|
</div>
|
|
|
|
Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/jucacrispim/sphinx_pdj_theme">theme</a> provided by <a href="http://poraodojuca.net">Porão do Juca</a>.
|
|
|
|
</footer>
|
|
</div>
|
|
</div>
|
|
|
|
|
|
<script type="text/javascript">
|
|
var DOCUMENTATION_OPTIONS = {
|
|
URL_ROOT:'./',
|
|
VERSION:'2.0',
|
|
COLLAPSE_INDEX:false,
|
|
FILE_SUFFIX:'.html',
|
|
HAS_SOURCE: true
|
|
};
|
|
</script>
|
|
<script type="text/javascript" src="_static/jquery.js">
|
|
|
|
</script>
|
|
<script type="text/javascript" src="_static/underscore.js">
|
|
|
|
</script>
|
|
<script type="text/javascript" src="_static/doctools.js">
|
|
|
|
</script>
|
|
<script type="text/javascript" src="_static/language_data.js">
|
|
|
|
</script>
|
|
|
|
|
|
|
|
<script type="text/javascript"
|
|
src="_static/js/theme.js"></script>
|
|
|
|
<script type="text/javascript"
|
|
src="_static/js/pdj.js"></script>
|
|
|
|
|
|
|
|
<script type="text/javascript">
|
|
jQuery(function () {
|
|
SphinxRtdTheme.StickyNav.enable();
|
|
});
|
|
</script>
|
|
|
|
|
|
</body>
|
|
</html> |