Skip to content

Commit

Permalink
First working version for marginals and MPEs.
Browse files Browse the repository at this point in the history
  • Loading branch information
tomsch420 committed Oct 31, 2023
1 parent 1cf4681 commit 47d69f4
Show file tree
Hide file tree
Showing 6 changed files with 383 additions and 13 deletions.
51 changes: 51 additions & 0 deletions .github/workflows/publish-to-pypi.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
name: Publish Python 🐍 distribution 📦 to PyPI and TestPyPI

on: push

jobs:
build:
name: Build distribution 📦
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: "3.x"

- name: Install pypa/build
run: >-
python3 -m
pip install
build
--user
- name: Build a binary wheel and a source tarball
run: python3 -m build
- name: Store the distribution packages
uses: actions/upload-artifact@v3
with:
name: python-package-distributions
path: dist/

publish-to-pypi:
name: >-
Publish Python 🐍 distribution 📦 to PyPI
if: startsWith(github.ref, 'refs/tags/') # only publish to PyPI on tag pushes
needs:
- build
runs-on: ubuntu-latest
environment:
name: pypi
url: https://pypi.org/p/random_events # Replace <package-name> with your PyPI project name
permissions:
id-token: write # IMPORTANT: mandatory for trusted publishing

steps:
- name: Download all the dists
uses: actions/download-artifact@v3
with:
name: python-package-distributions
path: dist/
- name: Publish distribution 📦 to PyPI
uses: pypa/gh-action-pypi-publish@release/v1
81 changes: 81 additions & 0 deletions .github/workflows/test-and-build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
name: Run tests and build the Python distribution

on:
push:
branches:
- master

# --------------------------------------------------------------------------------------------------------------------

workflow_call:
inputs:
version:
required: true
type: string

python-versions:
required: true
type: string
default: "cp38-cp38"

# ----------------------------------------------------------------------------------------------------------------------

defaults:
run:
shell: bash
working-directory: .

# ----------------------------------------------------------------------------------------------------------------------

concurrency:
group: 'random-events-building-and-deployment'
cancel-in-progress: true

# ----------------------------------------------------------------------------------------------------------------------

jobs:
test-and-build:
name: Build and Test the Python distribution
runs-on: ubuntu-22.04

steps:

- name: Checkout 🛎
uses: actions/checkout@v3

# ----------------------------------------------------------------------------------------------------------------

- name: Setup python 🐍
uses: actions/setup-python@v4
with:
python-version: 3.8
cache: pip

# ----------------------------------------------------------------------------------------------------------------

- name: Install user dependencies 🍼
uses: py-actions/py-dependency-install@v4
with:
path: "requirements.txt"

# ----------------------------------------------------------------------------------------------------------------

- name: Run Tests 🎓
run: |
cd test
PYTHONPATH=../src python -m unittest discover
# ----------------------------------------------------------------------------------------------------------------

- name: Install Sphinx dependencies 📚
uses: py-actions/py-dependency-install@v4
with:
path: "doc/requirements.txt"

# ----------------------------------------------------------------------------------------------------------------

- name: Build Sphinx documentation 📝
working-directory: ./doc
run: |
sudo apt install pandoc
make html
1 change: 0 additions & 1 deletion doc/notebooks/quickstart.ipynb

This file was deleted.

241 changes: 241 additions & 0 deletions doc/notebooks/quickstart.ipynb

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
networkx==3.2
numpy>=1.26.1
networkx==3.1
numpy==1.24.4
random_events>=1.1.1
tabulate>=0.9.0
11 changes: 6 additions & 5 deletions src/fglib2/graphs.py
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,7 @@ def to_latex_equation(self) -> str:
return r"P({}) = {}".format(", ".join(tuple(variable.name for variable in self.variables)),
r" \cdot ".join([str(factor) for factor in self.factor_nodes]))

def brute_force_joint_distribution(self) -> Tuple[np.ndarray[np.ndarray[int]], np.ndarray[float]]:
def brute_force_joint_distribution(self) -> Multinomial:
"""
Compute the joint distribution of the factor graph by brute force.
Expand All @@ -451,15 +451,16 @@ def brute_force_joint_distribution(self) -> Tuple[np.ndarray[np.ndarray[int]], n
"""
worlds = list(itertools.product(*[variable.domain for variable in self.variables]))
worlds = np.array(worlds)
potentials = np.ones(len(worlds))
potentials = np.zeros(tuple(len(variable.domain) for variable in self.variables))

for idx, world in enumerate(worlds):

potential = 1.
for factor in self.factor_nodes:
indices = [self.variables.index(variable) for variable in factor.variables]
potentials[idx] *= factor.distribution.likelihood(world[indices])
potential *= factor.distribution.likelihood(world[indices])
potentials[tuple(world)] = potential

return worlds, potentials
return Multinomial(self.variables, potentials)

def reset(self):
"""
Expand Down
7 changes: 2 additions & 5 deletions test/test_graphs.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,11 +123,8 @@ def test_graph(self):
self.assertEqual(len(self.fglib_graph.edges), len(self.graph.edges))

def test_brute_force(self):
worlds, potentials = self.graph.brute_force_joint_distribution()
for index, variable in enumerate(self.graph.variables):
for value in variable.domain:
indices = np.where(worlds[:, index] == value)[0]
print("P({} = {}) = {}".format(variable.name, value, np.sum(potentials[indices]) / np.sum(potentials)))
distribution = self.graph.brute_force_joint_distribution()
print(distribution.to_tabulate())

def test_calculation_by_hand(self):
x1_to_fa = self.graph.node_of(self.x1).unity()
Expand Down

0 comments on commit 47d69f4

Please sign in to comment.