1
0
Fork 0

Refactor AbstractImporter constructor to accept concatenated_samples dataframe/matrix

better_develop
Filippo Martini 4 years ago
parent 4624070877
commit b54b2edf7a
  1. 0
      .nojekyll
  2. 46
      PyCTBN/classes/abstract_importer.py
  3. 13
      PyCTBN/classes/sample_path.py
  4. 3
      PyCTBN/classes/structure_estimator.py
  5. 6
      PyCTBN/classes/trajectory.py
  6. 67
      docs/classes.html
  7. 13
      docs/genindex.html
  8. BIN
      docs/objects.inv
  9. 2
      docs/searchindex.js
  10. 2
      examples/simple_cvs_importer.py
  11. 3
      setup.py
  12. BIN
      sphinx_output/_build/doctrees/classes.doctree
  13. BIN
      sphinx_output/_build/doctrees/environment.pickle
  14. 67
      sphinx_output/_build/html/classes.html
  15. 13
      sphinx_output/_build/html/genindex.html
  16. BIN
      sphinx_output/_build/html/objects.inv
  17. 2
      sphinx_output/_build/html/searchindex.js

@ -2,39 +2,43 @@
import typing import typing
from abc import ABC, abstractmethod from abc import ABC, abstractmethod
import numpy as np
import pandas as pd import pandas as pd
class AbstractImporter(ABC): class AbstractImporter(ABC):
"""Abstract class that exposes all the necessary methods to process the trajectories and the net structure. """Abstract class that exposes all the necessary methods to process the trajectories and the net structure.
:param file_path: the file path :param file_path: the file path, or dataset name if you import already processed data
:type file_path: str :type file_path: str
:_concatenated_samples: Dataframe containing the concatenation of all the processed trajectories :param concatenated_samples: Dataframe or numpy array containing the concatenation of all the processed trajectories
:_df_structure: Dataframe containing the structure of the network (edges) :type concatenated_samples: typing.Union[pandas.DataFrame, numpy.ndarray]
:_df_variables: Dataframe containing the nodes cardinalities :param variables: Dataframe containing the nodes labels and cardinalities
:_sorter: A list containing the columns header (excluding the time column) of the ``_concatenated_samples`` :type variables: pandas.DataFrame
:prior_net_structure: Dataframe containing the structure of the network (edges)
:type prior_net_structure: pandas.DataFrame
:_sorter: A list containing the variables labels in the SAME order as the columns in ``concatenated_samples``
.. warning:: .. warning::
The class members ``_df_variables`` and ``_df_structure`` HAVE to be properly constructed The parameters ``variables`` and ``prior_net_structure`` HAVE to be properly constructed
as Pandas Dataframes with the following structure: as Pandas Dataframes with the following structure:
Header of _df_structure = [From_Node | To_Node] Header of _df_structure = [From_Node | To_Node]
Header of _df_variables = [Variable_Label | Variable_Cardinality] Header of _df_variables = [Variable_Label | Variable_Cardinality]
See the tutorial on how to construct a correct ``concatenated_samples`` Dataframe/ndarray.
.. note::
If you don't have prior network structure just leave ``_df_structure`` set to None.
.. note:: .. note::
See :class:``JsonImporter`` for an example implementation See :class:``JsonImporter`` for an example implementation
""" """
def __init__(self, file_path: str): def __init__(self, file_path: str = None, concatenated_samples: typing.Union[pd.DataFrame, np.ndarray] = None,
variables: pd.DataFrame = None, prior_net_structure: pd.DataFrame = None):
"""Constructor """Constructor
""" """
self._file_path = file_path self._file_path = file_path
self._df_variables = None self._concatenated_samples = concatenated_samples
self._df_structure = None self._df_variables = variables
self._concatenated_samples = None self._df_structure = prior_net_structure
self._sorter = None self._sorter = None
super().__init__() super().__init__()
@ -104,21 +108,27 @@ class AbstractImporter(ABC):
complete_header.extend(shifted_cols_header) complete_header.extend(shifted_cols_header)
self._concatenated_samples = self._concatenated_samples[complete_header] self._concatenated_samples = self._concatenated_samples[complete_header]
def build_list_of_samples_array(self, data_frame: pd.DataFrame) -> typing.List: def build_list_of_samples_array(self, concatenated_sample: typing.Union[pd.DataFrame, np.ndarray]) -> typing.List:
"""Builds a List containing the columns of data_frame and converts them to a numpy array. """Builds a List containing the the delta times numpy array, and the complete transitions matrix
:param data_frame: the dataframe from which the columns have to be extracted and converted :param concatenated_sample: the dataframe/array from which the time, and transitions matrix have to be extracted
:type data_frame: pandas.Dataframe and converted
:type concatenated_sample: typing.Union[pandas.Dataframe, numpy.ndarray]
:return: the resulting list of numpy arrays :return: the resulting list of numpy arrays
:rtype: List :rtype: List
""" """
columns_list = [data_frame[column].to_numpy() for column in data_frame] if isinstance(concatenated_sample, pd.DataFrame):
concatenated_array = concatenated_sample.to_numpy()
columns_list = [concatenated_array[:, 0], concatenated_array[:, 1:].astype(int)]
else:
columns_list = [concatenated_sample[:, 0], concatenated_sample[:, 1:].astype(int)]
return columns_list return columns_list
def clear_concatenated_frame(self) -> None: def clear_concatenated_frame(self) -> None:
"""Removes all values in the dataframe concatenated_samples. """Removes all values in the dataframe concatenated_samples.
""" """
self._concatenated_samples = self._concatenated_samples.iloc[0:0] if isinstance(self._concatenated_samples, pd.DataFrame):
self._concatenated_samples = self._concatenated_samples.iloc[0:0]
@abstractmethod @abstractmethod
def dataset_id(self) -> object: def dataset_id(self) -> object:

@ -1,4 +1,7 @@
import numpy as np
import pandas as pd
from .abstract_importer import AbstractImporter from .abstract_importer import AbstractImporter
from .structure import Structure from .structure import Structure
from .trajectory import Trajectory from .trajectory import Trajectory
@ -19,10 +22,16 @@ class SamplePath(object):
"""Constructor Method """Constructor Method
""" """
self._importer = importer self._importer = importer
if (self._importer._df_variables is None or self._importer._concatenated_samples is None): if self._importer._df_variables is None or self._importer._concatenated_samples is None:
raise RuntimeError('The importer object has to contain the all processed data!') raise RuntimeError('The importer object has to contain the all processed data!')
if(self._importer._df_variables.empty or self._importer._concatenated_samples.empty): if self._importer._df_variables.empty:
raise RuntimeError('The importer object has to contain the all processed data!') raise RuntimeError('The importer object has to contain the all processed data!')
if isinstance(self._importer._concatenated_samples, pd.DataFrame):
if self._importer._concatenated_samples.empty:
raise RuntimeError('The importer object has to contain the all processed data!')
if isinstance(self._importer._concatenated_samples, np.ndarray):
if self._importer._concatenated_samples.size == 0:
raise RuntimeError('The importer object has to contain the all processed data!')
self._trajectories = None self._trajectories = None
self._structure = None self._structure = None
self._total_variables_count = None self._total_variables_count = None

@ -259,6 +259,7 @@ class StructureEstimator(object):
graph_to_draw = nx.DiGraph() graph_to_draw = nx.DiGraph()
spurious_edges = self.spurious_edges() spurious_edges = self.spurious_edges()
non_spurious_edges = list(set(self._complete_graph.edges) - set(spurious_edges)) non_spurious_edges = list(set(self._complete_graph.edges) - set(spurious_edges))
print(non_spurious_edges)
edges_colors = ['red' if edge in spurious_edges else 'black' for edge in self._complete_graph.edges] edges_colors = ['red' if edge in spurious_edges else 'black' for edge in self._complete_graph.edges]
graph_to_draw.add_edges_from(spurious_edges) graph_to_draw.add_edges_from(spurious_edges)
graph_to_draw.add_edges_from(non_spurious_edges) graph_to_draw.add_edges_from(non_spurious_edges)
@ -270,7 +271,7 @@ class StructureEstimator(object):
'linewidths':2, 'linewidths':2,
"with_labels":True, "with_labels":True,
"font_size":13, "font_size":13,
'connectionstyle': 'arc3, rad = 0.', 'connectionstyle': 'arc3, rad = 0.1',
"arrowsize": 15, "arrowsize": 15,
"arrowstyle": '<|-', "arrowstyle": '<|-',
"width": 1, "width": 1,

@ -19,11 +19,9 @@ class Trajectory(object):
def __init__(self, list_of_columns: typing.List, original_cols_number: int): def __init__(self, list_of_columns: typing.List, original_cols_number: int):
"""Constructor Method """Constructor Method
""" """
if type(list_of_columns[0][0]) != np.float64: self._times = list_of_columns[0]
raise TypeError('The first array in the list has to be Times') self._actual_trajectory = list_of_columns[1]
self._original_cols_number = original_cols_number self._original_cols_number = original_cols_number
self._actual_trajectory = np.array(list_of_columns[1:], dtype=np.int).T
self._times = np.array(list_of_columns[0], dtype=np.float)
@property @property
def trajectory(self) -> np.ndarray: def trajectory(self) -> np.ndarray:

@ -134,36 +134,31 @@
<span id="classes-abstract-importer-module"></span><h2>classes.abstract_importer module<a class="headerlink" href="#module-classes.abstract_importer" title="Permalink to this headline"></a></h2> <span id="classes-abstract-importer-module"></span><h2>classes.abstract_importer module<a class="headerlink" href="#module-classes.abstract_importer" title="Permalink to this headline"></a></h2>
<dl class="py class"> <dl class="py class">
<dt id="classes.abstract_importer.AbstractImporter"> <dt id="classes.abstract_importer.AbstractImporter">
<em class="property">class </em><code class="sig-prename descclassname">classes.abstract_importer.</code><code class="sig-name descname">AbstractImporter</code><span class="sig-paren">(</span><em class="sig-param"><span class="n">file_path</span><span class="p">:</span> <span class="n">str</span></em><span class="sig-paren">)</span><a class="headerlink" href="#classes.abstract_importer.AbstractImporter" title="Permalink to this definition"></a></dt> <em class="property">class </em><code class="sig-prename descclassname">classes.abstract_importer.</code><code class="sig-name descname">AbstractImporter</code><span class="sig-paren">(</span><em class="sig-param"><span class="n">file_path</span><span class="p">:</span> <span class="n">str</span> <span class="o">=</span> <span class="default_value">None</span></em>, <em class="sig-param"><span class="n">concatenated_samples</span><span class="p">:</span> <span class="n">Union<span class="p">[</span>pandas.core.frame.DataFrame<span class="p">, </span>numpy.ndarray<span class="p">]</span></span> <span class="o">=</span> <span class="default_value">None</span></em>, <em class="sig-param"><span class="n">variables</span><span class="p">:</span> <span class="n">pandas.core.frame.DataFrame</span> <span class="o">=</span> <span class="default_value">None</span></em>, <em class="sig-param"><span class="n">prior_net_structure</span><span class="p">:</span> <span class="n">pandas.core.frame.DataFrame</span> <span class="o">=</span> <span class="default_value">None</span></em><span class="sig-paren">)</span><a class="headerlink" href="#classes.abstract_importer.AbstractImporter" title="Permalink to this definition"></a></dt>
<dd><p>Bases: <code class="xref py py-class docutils literal notranslate"><span class="pre">abc.ABC</span></code></p> <dd><p>Bases: <code class="xref py py-class docutils literal notranslate"><span class="pre">abc.ABC</span></code></p>
<p>Abstract class that exposes all the necessary methods to process the trajectories and the net structure.</p> <p>Abstract class that exposes all the necessary methods to process the trajectories and the net structure.</p>
<dl class="field-list simple"> <dl class="field-list simple">
<dt class="field-odd">Parameters</dt> <dt class="field-odd">Parameters</dt>
<dd class="field-odd"><p><strong>file_path</strong> (<em>str</em>) – the file path</p> <dd class="field-odd"><ul class="simple">
</dd> <li><p><strong>file_path</strong> (<em>str</em>) – the file path, or dataset name if you import already processed data</p></li>
<dt class="field-even">_concatenated_samples</dt> <li><p><strong>concatenated_samples</strong> (<em>typing.Union</em><em>[</em><em>pandas.DataFrame</em><em>, </em><em>numpy.ndarray</em><em>]</em>) – Dataframe or numpy array containing the concatenation of all the processed trajectories</p></li>
<dd class="field-even"><p>Dataframe containing the concatenation of all the processed trajectories</p> <li><p><strong>variables</strong> (<em>pandas.DataFrame</em>) – Dataframe containing the nodes labels and cardinalities</p></li>
</dd> </ul>
<dt class="field-odd">_df_structure</dt>
<dd class="field-odd"><p>Dataframe containing the structure of the network (edges)</p>
</dd> </dd>
<dt class="field-even">_df_variables</dt> <dt class="field-even">Prior_net_structure</dt>
<dd class="field-even"><p>Dataframe containing the nodes cardinalities</p> <dd class="field-even"><p>Dataframe containing the structure of the network (edges)</p>
</dd> </dd>
<dt class="field-odd">_sorter</dt> <dt class="field-odd">_sorter</dt>
<dd class="field-odd"><p>A list containing the columns header (excluding the time column) of the <code class="docutils literal notranslate"><span class="pre">_concatenated_samples</span></code></p> <dd class="field-odd"><p>A list containing the variables labels in the SAME order as the columns in <code class="docutils literal notranslate"><span class="pre">concatenated_samples</span></code></p>
</dd> </dd>
</dl> </dl>
<div class="admonition warning"> <div class="admonition warning">
<p class="admonition-title">Warning</p> <p class="admonition-title">Warning</p>
<p>The class members <code class="docutils literal notranslate"><span class="pre">_df_variables</span></code> and <code class="docutils literal notranslate"><span class="pre">_df_structure</span></code> HAVE to be properly constructed <p>The parameters <code class="docutils literal notranslate"><span class="pre">variables</span></code> and <code class="docutils literal notranslate"><span class="pre">prior_net_structure</span></code> HAVE to be properly constructed
as Pandas Dataframes with the following structure: as Pandas Dataframes with the following structure:
Header of _df_structure = [From_Node | To_Node] Header of _df_structure = [From_Node | To_Node]
Header of _df_variables = [Variable_Label | Variable_Cardinality]</p> Header of _df_variables = [Variable_Label | Variable_Cardinality]
</div> See the tutorial on how to construct a correct <code class="docutils literal notranslate"><span class="pre">concatenated_samples</span></code> Dataframe/ndarray.</p>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>If you don’t have prior network structure just leave <code class="docutils literal notranslate"><span class="pre">_df_structure</span></code> set to None.</p>
</div> </div>
<div class="admonition note"> <div class="admonition note">
<p class="admonition-title">Note</p> <p class="admonition-title">Note</p>
@ -171,11 +166,12 @@ Header of _df_variables = [Variable_Label | Variable_Cardinality]</p>
</div> </div>
<dl class="py method"> <dl class="py method">
<dt id="classes.abstract_importer.AbstractImporter.build_list_of_samples_array"> <dt id="classes.abstract_importer.AbstractImporter.build_list_of_samples_array">
<code class="sig-name descname">build_list_of_samples_array</code><span class="sig-paren">(</span><em class="sig-param"><span class="n">data_frame</span><span class="p">:</span> <span class="n">pandas.core.frame.DataFrame</span></em><span class="sig-paren">)</span> &#x2192; List<a class="headerlink" href="#classes.abstract_importer.AbstractImporter.build_list_of_samples_array" title="Permalink to this definition"></a></dt> <code class="sig-name descname">build_list_of_samples_array</code><span class="sig-paren">(</span><em class="sig-param"><span class="n">concatenated_sample</span><span class="p">:</span> <span class="n">Union<span class="p">[</span>pandas.core.frame.DataFrame<span class="p">, </span>numpy.ndarray<span class="p">]</span></span></em><span class="sig-paren">)</span> &#x2192; List<a class="headerlink" href="#classes.abstract_importer.AbstractImporter.build_list_of_samples_array" title="Permalink to this definition"></a></dt>
<dd><p>Builds a List containing the columns of data_frame and converts them to a numpy array.</p> <dd><p>Builds a List containing the the delta times numpy array, and the complete transitions matrix</p>
<dl class="field-list simple"> <dl class="field-list simple">
<dt class="field-odd">Parameters</dt> <dt class="field-odd">Parameters</dt>
<dd class="field-odd"><p><strong>data_frame</strong> (<em>pandas.Dataframe</em>) – the dataframe from which the columns have to be extracted and converted</p> <dd class="field-odd"><p><strong>concatenated_sample</strong> (<em>typing.Union</em><em>[</em><em>pandas.Dataframe</em><em>, </em><em>numpy.ndarray</em><em>]</em>) – the dataframe/array from which the time, and transitions matrix have to be extracted
and converted</p>
</dd> </dd>
<dt class="field-even">Returns</dt> <dt class="field-even">Returns</dt>
<dd class="field-even"><p>the resulting list of numpy arrays</p> <dd class="field-even"><p>the resulting list of numpy arrays</p>
@ -981,6 +977,11 @@ contain the mentioned data.</p>
Clears all the unused dataframes in <code class="docutils literal notranslate"><span class="pre">_importer</span></code> Object</p> Clears all the unused dataframes in <code class="docutils literal notranslate"><span class="pre">_importer</span></code> Object</p>
</dd></dl> </dd></dl>
<dl class="py method">
<dt id="classes.sample_path.SamplePath.has_prior_net_structure">
<em class="property">property </em><code class="sig-name descname">has_prior_net_structure</code><a class="headerlink" href="#classes.sample_path.SamplePath.has_prior_net_structure" title="Permalink to this definition"></a></dt>
<dd></dd></dl>
<dl class="py method"> <dl class="py method">
<dt id="classes.sample_path.SamplePath.structure"> <dt id="classes.sample_path.SamplePath.structure">
<em class="property">property </em><code class="sig-name descname">structure</code><a class="headerlink" href="#classes.sample_path.SamplePath.structure" title="Permalink to this definition"></a></dt> <em class="property">property </em><code class="sig-name descname">structure</code><a class="headerlink" href="#classes.sample_path.SamplePath.structure" title="Permalink to this definition"></a></dt>
@ -1228,7 +1229,7 @@ The class member <code class="docutils literal notranslate"><span class="pre">_t
<dl class="py method"> <dl class="py method">
<dt id="classes.structure_estimator.StructureEstimator.adjacency_matrix"> <dt id="classes.structure_estimator.StructureEstimator.adjacency_matrix">
<code class="sig-name descname">adjacency_matrix</code><span class="sig-paren">(</span><span class="sig-paren">)</span> &#x2192; numpy.ndarray<a class="headerlink" href="#classes.structure_estimator.StructureEstimator.adjacency_matrix" title="Permalink to this definition"></a></dt> <code class="sig-name descname">adjacency_matrix</code><span class="sig-paren">(</span><span class="sig-paren">)</span> &#x2192; numpy.ndarray<a class="headerlink" href="#classes.structure_estimator.StructureEstimator.adjacency_matrix" title="Permalink to this definition"></a></dt>
<dd><p>Converts the estimated structrure <code class="docutils literal notranslate"><span class="pre">_complete_graph</span></code> to a boolean adjacency matrix representation.</p> <dd><p>Converts the estimated structure <code class="docutils literal notranslate"><span class="pre">_complete_graph</span></code> to a boolean adjacency matrix representation.</p>
<dl class="field-list simple"> <dl class="field-list simple">
<dt class="field-odd">Returns</dt> <dt class="field-odd">Returns</dt>
<dd class="field-odd"><p>The adjacency matrix of the graph <code class="docutils literal notranslate"><span class="pre">_complete_graph</span></code></p> <dd class="field-odd"><p>The adjacency matrix of the graph <code class="docutils literal notranslate"><span class="pre">_complete_graph</span></code></p>
@ -1343,6 +1344,13 @@ it is performed also the chi_test.</p>
</dl> </dl>
</dd></dl> </dd></dl>
<dl class="py method">
<dt id="classes.structure_estimator.StructureEstimator.save_plot_estimated_structure_graph">
<code class="sig-name descname">save_plot_estimated_structure_graph</code><span class="sig-paren">(</span><span class="sig-paren">)</span> &#x2192; None<a class="headerlink" href="#classes.structure_estimator.StructureEstimator.save_plot_estimated_structure_graph" title="Permalink to this definition"></a></dt>
<dd><p>Plot the estimated structure in a graphical model style.
Spurious edges are colored in red.</p>
</dd></dl>
<dl class="py method"> <dl class="py method">
<dt id="classes.structure_estimator.StructureEstimator.save_results"> <dt id="classes.structure_estimator.StructureEstimator.save_results">
<code class="sig-name descname">save_results</code><span class="sig-paren">(</span><span class="sig-paren">)</span> &#x2192; None<a class="headerlink" href="#classes.structure_estimator.StructureEstimator.save_results" title="Permalink to this definition"></a></dt> <code class="sig-name descname">save_results</code><span class="sig-paren">(</span><span class="sig-paren">)</span> &#x2192; None<a class="headerlink" href="#classes.structure_estimator.StructureEstimator.save_results" title="Permalink to this definition"></a></dt>
@ -1350,6 +1358,23 @@ it is performed also the chi_test.</p>
The file is named as the input dataset but the <cite>results_</cite> word is appended to the results file.</p> The file is named as the input dataset but the <cite>results_</cite> word is appended to the results file.</p>
</dd></dl> </dd></dl>
<dl class="py method">
<dt id="classes.structure_estimator.StructureEstimator.spurious_edges">
<code class="sig-name descname">spurious_edges</code><span class="sig-paren">(</span><span class="sig-paren">)</span> &#x2192; List<a class="headerlink" href="#classes.structure_estimator.StructureEstimator.spurious_edges" title="Permalink to this definition"></a></dt>
<dd><dl class="simple">
<dt>Return the spurious edges present in the estimated structure, if a prior net structure is present in</dt><dd><p><code class="docutils literal notranslate"><span class="pre">_sample_path.structure</span></code>.</p>
</dd>
</dl>
<dl class="field-list simple">
<dt class="field-odd">Returns</dt>
<dd class="field-odd"><p>A list containing the spurious edges</p>
</dd>
<dt class="field-even">Return type</dt>
<dd class="field-even"><p>List</p>
</dd>
</dl>
</dd></dl>
</dd></dl> </dd></dl>
</div> </div>

@ -134,6 +134,7 @@
| <a href="#E"><strong>E</strong></a> | <a href="#E"><strong>E</strong></a>
| <a href="#F"><strong>F</strong></a> | <a href="#F"><strong>F</strong></a>
| <a href="#G"><strong>G</strong></a> | <a href="#G"><strong>G</strong></a>
| <a href="#H"><strong>H</strong></a>
| <a href="#I"><strong>I</strong></a> | <a href="#I"><strong>I</strong></a>
| <a href="#J"><strong>J</strong></a> | <a href="#J"><strong>J</strong></a>
| <a href="#M"><strong>M</strong></a> | <a href="#M"><strong>M</strong></a>
@ -406,6 +407,14 @@
</ul></td> </ul></td>
</tr></table> </tr></table>
<h2 id="H">H</h2>
<table style="width: 100%" class="indextable genindextable"><tr>
<td style="width: 33%; vertical-align: top;"><ul>
<li><a href="classes.html#classes.sample_path.SamplePath.has_prior_net_structure">has_prior_net_structure() (classes.sample_path.SamplePath property)</a>
</li>
</ul></td>
</tr></table>
<h2 id="I">I</h2> <h2 id="I">I</h2>
<table style="width: 100%" class="indextable genindextable"><tr> <table style="width: 100%" class="indextable genindextable"><tr>
<td style="width: 33%; vertical-align: top;"><ul> <td style="width: 33%; vertical-align: top;"><ul>
@ -547,6 +556,8 @@
<table style="width: 100%" class="indextable genindextable"><tr> <table style="width: 100%" class="indextable genindextable"><tr>
<td style="width: 33%; vertical-align: top;"><ul> <td style="width: 33%; vertical-align: top;"><ul>
<li><a href="classes.html#classes.sample_path.SamplePath">SamplePath (class in classes.sample_path)</a> <li><a href="classes.html#classes.sample_path.SamplePath">SamplePath (class in classes.sample_path)</a>
</li>
<li><a href="classes.html#classes.structure_estimator.StructureEstimator.save_plot_estimated_structure_graph">save_plot_estimated_structure_graph() (classes.structure_estimator.StructureEstimator method)</a>
</li> </li>
<li><a href="classes.html#classes.structure_estimator.StructureEstimator.save_results">save_results() (classes.structure_estimator.StructureEstimator method)</a> <li><a href="classes.html#classes.structure_estimator.StructureEstimator.save_results">save_results() (classes.structure_estimator.StructureEstimator method)</a>
</li> </li>
@ -558,6 +569,8 @@
</li> </li>
</ul></td> </ul></td>
<td style="width: 33%; vertical-align: top;"><ul> <td style="width: 33%; vertical-align: top;"><ul>
<li><a href="classes.html#classes.structure_estimator.StructureEstimator.spurious_edges">spurious_edges() (classes.structure_estimator.StructureEstimator method)</a>
</li>
<li><a href="classes.html#classes.conditional_intensity_matrix.ConditionalIntensityMatrix.state_residence_times">state_residence_times() (classes.conditional_intensity_matrix.ConditionalIntensityMatrix property)</a> <li><a href="classes.html#classes.conditional_intensity_matrix.ConditionalIntensityMatrix.state_residence_times">state_residence_times() (classes.conditional_intensity_matrix.ConditionalIntensityMatrix property)</a>
</li> </li>
<li><a href="classes.html#classes.conditional_intensity_matrix.ConditionalIntensityMatrix.state_transition_matrix">state_transition_matrix() (classes.conditional_intensity_matrix.ConditionalIntensityMatrix property)</a> <li><a href="classes.html#classes.conditional_intensity_matrix.ConditionalIntensityMatrix.state_transition_matrix">state_transition_matrix() (classes.conditional_intensity_matrix.ConditionalIntensityMatrix property)</a>

Binary file not shown.

File diff suppressed because one or more lines are too long

@ -44,7 +44,7 @@ class CSVImporter(AbstractImporter):
def main(): def main():
read_files = glob.glob(os.path.join('../../data', "*.csv")) read_files = glob.glob(os.path.join('../data', "*.csv"))
print(read_files[0]) print(read_files[0])
csvimp = CSVImporter(read_files[0]) csvimp = CSVImporter(read_files[0])
csvimp.import_data() csvimp.import_data()

@ -1,6 +1,5 @@
from setuptools import setup, find_packages from setuptools import setup, find_packages
print(find_packages('.', exclude=['PyCTBN.tests']))
setup(name='PyCTBN', setup(name='PyCTBN',
version='1.0', version='1.0',
@ -12,7 +11,7 @@ setup(name='PyCTBN',
packages=find_packages('.', exclude=['PyCTBN.tests']), packages=find_packages('.', exclude=['PyCTBN.tests']),
#packages=['PyCTBN.classes'], #packages=['PyCTBN.classes'],
install_requires=[ install_requires=[
'numpy', 'pandas', 'networkx', 'scipy', 'tqdm'], 'numpy', 'pandas', 'networkx', 'scipy', 'matplotlib', 'tqdm'],
dependency_links=['https://github.com/numpy/numpy', 'https://github.com/pandas-dev/pandas', dependency_links=['https://github.com/numpy/numpy', 'https://github.com/pandas-dev/pandas',
'https://github.com/networkx/networkx', 'https://github.com/scipy/scipy', 'https://github.com/networkx/networkx', 'https://github.com/scipy/scipy',
'https://github.com/tqdm/tqdm'], 'https://github.com/tqdm/tqdm'],

@ -134,36 +134,31 @@
<span id="classes-abstract-importer-module"></span><h2>classes.abstract_importer module<a class="headerlink" href="#module-classes.abstract_importer" title="Permalink to this headline"></a></h2> <span id="classes-abstract-importer-module"></span><h2>classes.abstract_importer module<a class="headerlink" href="#module-classes.abstract_importer" title="Permalink to this headline"></a></h2>
<dl class="py class"> <dl class="py class">
<dt id="classes.abstract_importer.AbstractImporter"> <dt id="classes.abstract_importer.AbstractImporter">
<em class="property">class </em><code class="sig-prename descclassname">classes.abstract_importer.</code><code class="sig-name descname">AbstractImporter</code><span class="sig-paren">(</span><em class="sig-param"><span class="n">file_path</span><span class="p">:</span> <span class="n">str</span></em><span class="sig-paren">)</span><a class="headerlink" href="#classes.abstract_importer.AbstractImporter" title="Permalink to this definition"></a></dt> <em class="property">class </em><code class="sig-prename descclassname">classes.abstract_importer.</code><code class="sig-name descname">AbstractImporter</code><span class="sig-paren">(</span><em class="sig-param"><span class="n">file_path</span><span class="p">:</span> <span class="n">str</span> <span class="o">=</span> <span class="default_value">None</span></em>, <em class="sig-param"><span class="n">concatenated_samples</span><span class="p">:</span> <span class="n">Union<span class="p">[</span>pandas.core.frame.DataFrame<span class="p">, </span>numpy.ndarray<span class="p">]</span></span> <span class="o">=</span> <span class="default_value">None</span></em>, <em class="sig-param"><span class="n">variables</span><span class="p">:</span> <span class="n">pandas.core.frame.DataFrame</span> <span class="o">=</span> <span class="default_value">None</span></em>, <em class="sig-param"><span class="n">prior_net_structure</span><span class="p">:</span> <span class="n">pandas.core.frame.DataFrame</span> <span class="o">=</span> <span class="default_value">None</span></em><span class="sig-paren">)</span><a class="headerlink" href="#classes.abstract_importer.AbstractImporter" title="Permalink to this definition"></a></dt>
<dd><p>Bases: <code class="xref py py-class docutils literal notranslate"><span class="pre">abc.ABC</span></code></p> <dd><p>Bases: <code class="xref py py-class docutils literal notranslate"><span class="pre">abc.ABC</span></code></p>
<p>Abstract class that exposes all the necessary methods to process the trajectories and the net structure.</p> <p>Abstract class that exposes all the necessary methods to process the trajectories and the net structure.</p>
<dl class="field-list simple"> <dl class="field-list simple">
<dt class="field-odd">Parameters</dt> <dt class="field-odd">Parameters</dt>
<dd class="field-odd"><p><strong>file_path</strong> (<em>str</em>) – the file path</p> <dd class="field-odd"><ul class="simple">
</dd> <li><p><strong>file_path</strong> (<em>str</em>) – the file path, or dataset name if you import already processed data</p></li>
<dt class="field-even">_concatenated_samples</dt> <li><p><strong>concatenated_samples</strong> (<em>typing.Union</em><em>[</em><em>pandas.DataFrame</em><em>, </em><em>numpy.ndarray</em><em>]</em>) – Dataframe or numpy array containing the concatenation of all the processed trajectories</p></li>
<dd class="field-even"><p>Dataframe containing the concatenation of all the processed trajectories</p> <li><p><strong>variables</strong> (<em>pandas.DataFrame</em>) – Dataframe containing the nodes labels and cardinalities</p></li>
</dd> </ul>
<dt class="field-odd">_df_structure</dt>
<dd class="field-odd"><p>Dataframe containing the structure of the network (edges)</p>
</dd> </dd>
<dt class="field-even">_df_variables</dt> <dt class="field-even">Prior_net_structure</dt>
<dd class="field-even"><p>Dataframe containing the nodes cardinalities</p> <dd class="field-even"><p>Dataframe containing the structure of the network (edges)</p>
</dd> </dd>
<dt class="field-odd">_sorter</dt> <dt class="field-odd">_sorter</dt>
<dd class="field-odd"><p>A list containing the columns header (excluding the time column) of the <code class="docutils literal notranslate"><span class="pre">_concatenated_samples</span></code></p> <dd class="field-odd"><p>A list containing the variables labels in the SAME order as the columns in <code class="docutils literal notranslate"><span class="pre">concatenated_samples</span></code></p>
</dd> </dd>
</dl> </dl>
<div class="admonition warning"> <div class="admonition warning">
<p class="admonition-title">Warning</p> <p class="admonition-title">Warning</p>
<p>The class members <code class="docutils literal notranslate"><span class="pre">_df_variables</span></code> and <code class="docutils literal notranslate"><span class="pre">_df_structure</span></code> HAVE to be properly constructed <p>The parameters <code class="docutils literal notranslate"><span class="pre">variables</span></code> and <code class="docutils literal notranslate"><span class="pre">prior_net_structure</span></code> HAVE to be properly constructed
as Pandas Dataframes with the following structure: as Pandas Dataframes with the following structure:
Header of _df_structure = [From_Node | To_Node] Header of _df_structure = [From_Node | To_Node]
Header of _df_variables = [Variable_Label | Variable_Cardinality]</p> Header of _df_variables = [Variable_Label | Variable_Cardinality]
</div> See the tutorial on how to construct a correct <code class="docutils literal notranslate"><span class="pre">concatenated_samples</span></code> Dataframe/ndarray.</p>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>If you don’t have prior network structure just leave <code class="docutils literal notranslate"><span class="pre">_df_structure</span></code> set to None.</p>
</div> </div>
<div class="admonition note"> <div class="admonition note">
<p class="admonition-title">Note</p> <p class="admonition-title">Note</p>
@ -171,11 +166,12 @@ Header of _df_variables = [Variable_Label | Variable_Cardinality]</p>
</div> </div>
<dl class="py method"> <dl class="py method">
<dt id="classes.abstract_importer.AbstractImporter.build_list_of_samples_array"> <dt id="classes.abstract_importer.AbstractImporter.build_list_of_samples_array">
<code class="sig-name descname">build_list_of_samples_array</code><span class="sig-paren">(</span><em class="sig-param"><span class="n">data_frame</span><span class="p">:</span> <span class="n">pandas.core.frame.DataFrame</span></em><span class="sig-paren">)</span> &#x2192; List<a class="headerlink" href="#classes.abstract_importer.AbstractImporter.build_list_of_samples_array" title="Permalink to this definition"></a></dt> <code class="sig-name descname">build_list_of_samples_array</code><span class="sig-paren">(</span><em class="sig-param"><span class="n">concatenated_sample</span><span class="p">:</span> <span class="n">Union<span class="p">[</span>pandas.core.frame.DataFrame<span class="p">, </span>numpy.ndarray<span class="p">]</span></span></em><span class="sig-paren">)</span> &#x2192; List<a class="headerlink" href="#classes.abstract_importer.AbstractImporter.build_list_of_samples_array" title="Permalink to this definition"></a></dt>
<dd><p>Builds a List containing the columns of data_frame and converts them to a numpy array.</p> <dd><p>Builds a List containing the the delta times numpy array, and the complete transitions matrix</p>
<dl class="field-list simple"> <dl class="field-list simple">
<dt class="field-odd">Parameters</dt> <dt class="field-odd">Parameters</dt>
<dd class="field-odd"><p><strong>data_frame</strong> (<em>pandas.Dataframe</em>) – the dataframe from which the columns have to be extracted and converted</p> <dd class="field-odd"><p><strong>concatenated_sample</strong> (<em>typing.Union</em><em>[</em><em>pandas.Dataframe</em><em>, </em><em>numpy.ndarray</em><em>]</em>) – the dataframe/array from which the time, and transitions matrix have to be extracted
and converted</p>
</dd> </dd>
<dt class="field-even">Returns</dt> <dt class="field-even">Returns</dt>
<dd class="field-even"><p>the resulting list of numpy arrays</p> <dd class="field-even"><p>the resulting list of numpy arrays</p>
@ -981,6 +977,11 @@ contain the mentioned data.</p>
Clears all the unused dataframes in <code class="docutils literal notranslate"><span class="pre">_importer</span></code> Object</p> Clears all the unused dataframes in <code class="docutils literal notranslate"><span class="pre">_importer</span></code> Object</p>
</dd></dl> </dd></dl>
<dl class="py method">
<dt id="classes.sample_path.SamplePath.has_prior_net_structure">
<em class="property">property </em><code class="sig-name descname">has_prior_net_structure</code><a class="headerlink" href="#classes.sample_path.SamplePath.has_prior_net_structure" title="Permalink to this definition"></a></dt>
<dd></dd></dl>
<dl class="py method"> <dl class="py method">
<dt id="classes.sample_path.SamplePath.structure"> <dt id="classes.sample_path.SamplePath.structure">
<em class="property">property </em><code class="sig-name descname">structure</code><a class="headerlink" href="#classes.sample_path.SamplePath.structure" title="Permalink to this definition"></a></dt> <em class="property">property </em><code class="sig-name descname">structure</code><a class="headerlink" href="#classes.sample_path.SamplePath.structure" title="Permalink to this definition"></a></dt>
@ -1228,7 +1229,7 @@ The class member <code class="docutils literal notranslate"><span class="pre">_t
<dl class="py method"> <dl class="py method">
<dt id="classes.structure_estimator.StructureEstimator.adjacency_matrix"> <dt id="classes.structure_estimator.StructureEstimator.adjacency_matrix">
<code class="sig-name descname">adjacency_matrix</code><span class="sig-paren">(</span><span class="sig-paren">)</span> &#x2192; numpy.ndarray<a class="headerlink" href="#classes.structure_estimator.StructureEstimator.adjacency_matrix" title="Permalink to this definition"></a></dt> <code class="sig-name descname">adjacency_matrix</code><span class="sig-paren">(</span><span class="sig-paren">)</span> &#x2192; numpy.ndarray<a class="headerlink" href="#classes.structure_estimator.StructureEstimator.adjacency_matrix" title="Permalink to this definition"></a></dt>
<dd><p>Converts the estimated structrure <code class="docutils literal notranslate"><span class="pre">_complete_graph</span></code> to a boolean adjacency matrix representation.</p> <dd><p>Converts the estimated structure <code class="docutils literal notranslate"><span class="pre">_complete_graph</span></code> to a boolean adjacency matrix representation.</p>
<dl class="field-list simple"> <dl class="field-list simple">
<dt class="field-odd">Returns</dt> <dt class="field-odd">Returns</dt>
<dd class="field-odd"><p>The adjacency matrix of the graph <code class="docutils literal notranslate"><span class="pre">_complete_graph</span></code></p> <dd class="field-odd"><p>The adjacency matrix of the graph <code class="docutils literal notranslate"><span class="pre">_complete_graph</span></code></p>
@ -1343,6 +1344,13 @@ it is performed also the chi_test.</p>
</dl> </dl>
</dd></dl> </dd></dl>
<dl class="py method">
<dt id="classes.structure_estimator.StructureEstimator.save_plot_estimated_structure_graph">
<code class="sig-name descname">save_plot_estimated_structure_graph</code><span class="sig-paren">(</span><span class="sig-paren">)</span> &#x2192; None<a class="headerlink" href="#classes.structure_estimator.StructureEstimator.save_plot_estimated_structure_graph" title="Permalink to this definition"></a></dt>
<dd><p>Plot the estimated structure in a graphical model style.
Spurious edges are colored in red.</p>
</dd></dl>
<dl class="py method"> <dl class="py method">
<dt id="classes.structure_estimator.StructureEstimator.save_results"> <dt id="classes.structure_estimator.StructureEstimator.save_results">
<code class="sig-name descname">save_results</code><span class="sig-paren">(</span><span class="sig-paren">)</span> &#x2192; None<a class="headerlink" href="#classes.structure_estimator.StructureEstimator.save_results" title="Permalink to this definition"></a></dt> <code class="sig-name descname">save_results</code><span class="sig-paren">(</span><span class="sig-paren">)</span> &#x2192; None<a class="headerlink" href="#classes.structure_estimator.StructureEstimator.save_results" title="Permalink to this definition"></a></dt>
@ -1350,6 +1358,23 @@ it is performed also the chi_test.</p>
The file is named as the input dataset but the <cite>results_</cite> word is appended to the results file.</p> The file is named as the input dataset but the <cite>results_</cite> word is appended to the results file.</p>
</dd></dl> </dd></dl>
<dl class="py method">
<dt id="classes.structure_estimator.StructureEstimator.spurious_edges">
<code class="sig-name descname">spurious_edges</code><span class="sig-paren">(</span><span class="sig-paren">)</span> &#x2192; List<a class="headerlink" href="#classes.structure_estimator.StructureEstimator.spurious_edges" title="Permalink to this definition"></a></dt>
<dd><dl class="simple">
<dt>Return the spurious edges present in the estimated structure, if a prior net structure is present in</dt><dd><p><code class="docutils literal notranslate"><span class="pre">_sample_path.structure</span></code>.</p>
</dd>
</dl>
<dl class="field-list simple">
<dt class="field-odd">Returns</dt>
<dd class="field-odd"><p>A list containing the spurious edges</p>
</dd>
<dt class="field-even">Return type</dt>
<dd class="field-even"><p>List</p>
</dd>
</dl>
</dd></dl>
</dd></dl> </dd></dl>
</div> </div>

@ -134,6 +134,7 @@
| <a href="#E"><strong>E</strong></a> | <a href="#E"><strong>E</strong></a>
| <a href="#F"><strong>F</strong></a> | <a href="#F"><strong>F</strong></a>
| <a href="#G"><strong>G</strong></a> | <a href="#G"><strong>G</strong></a>
| <a href="#H"><strong>H</strong></a>
| <a href="#I"><strong>I</strong></a> | <a href="#I"><strong>I</strong></a>
| <a href="#J"><strong>J</strong></a> | <a href="#J"><strong>J</strong></a>
| <a href="#M"><strong>M</strong></a> | <a href="#M"><strong>M</strong></a>
@ -406,6 +407,14 @@
</ul></td> </ul></td>
</tr></table> </tr></table>
<h2 id="H">H</h2>
<table style="width: 100%" class="indextable genindextable"><tr>
<td style="width: 33%; vertical-align: top;"><ul>
<li><a href="classes.html#classes.sample_path.SamplePath.has_prior_net_structure">has_prior_net_structure() (classes.sample_path.SamplePath property)</a>
</li>
</ul></td>
</tr></table>
<h2 id="I">I</h2> <h2 id="I">I</h2>
<table style="width: 100%" class="indextable genindextable"><tr> <table style="width: 100%" class="indextable genindextable"><tr>
<td style="width: 33%; vertical-align: top;"><ul> <td style="width: 33%; vertical-align: top;"><ul>
@ -547,6 +556,8 @@
<table style="width: 100%" class="indextable genindextable"><tr> <table style="width: 100%" class="indextable genindextable"><tr>
<td style="width: 33%; vertical-align: top;"><ul> <td style="width: 33%; vertical-align: top;"><ul>
<li><a href="classes.html#classes.sample_path.SamplePath">SamplePath (class in classes.sample_path)</a> <li><a href="classes.html#classes.sample_path.SamplePath">SamplePath (class in classes.sample_path)</a>
</li>
<li><a href="classes.html#classes.structure_estimator.StructureEstimator.save_plot_estimated_structure_graph">save_plot_estimated_structure_graph() (classes.structure_estimator.StructureEstimator method)</a>
</li> </li>
<li><a href="classes.html#classes.structure_estimator.StructureEstimator.save_results">save_results() (classes.structure_estimator.StructureEstimator method)</a> <li><a href="classes.html#classes.structure_estimator.StructureEstimator.save_results">save_results() (classes.structure_estimator.StructureEstimator method)</a>
</li> </li>
@ -558,6 +569,8 @@
</li> </li>
</ul></td> </ul></td>
<td style="width: 33%; vertical-align: top;"><ul> <td style="width: 33%; vertical-align: top;"><ul>
<li><a href="classes.html#classes.structure_estimator.StructureEstimator.spurious_edges">spurious_edges() (classes.structure_estimator.StructureEstimator method)</a>
</li>
<li><a href="classes.html#classes.conditional_intensity_matrix.ConditionalIntensityMatrix.state_residence_times">state_residence_times() (classes.conditional_intensity_matrix.ConditionalIntensityMatrix property)</a> <li><a href="classes.html#classes.conditional_intensity_matrix.ConditionalIntensityMatrix.state_residence_times">state_residence_times() (classes.conditional_intensity_matrix.ConditionalIntensityMatrix property)</a>
</li> </li>
<li><a href="classes.html#classes.conditional_intensity_matrix.ConditionalIntensityMatrix.state_transition_matrix">state_transition_matrix() (classes.conditional_intensity_matrix.ConditionalIntensityMatrix property)</a> <li><a href="classes.html#classes.conditional_intensity_matrix.ConditionalIntensityMatrix.state_transition_matrix">state_transition_matrix() (classes.conditional_intensity_matrix.ConditionalIntensityMatrix property)</a>

File diff suppressed because one or more lines are too long