Skip to content

Commit 4bdb6d9

Browse files
committed
Adds inference code and documentation
1 parent b54861c commit 4bdb6d9

File tree

7 files changed

+1225
-1
lines changed

7 files changed

+1225
-1
lines changed

Diff for: doc_requirements.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Sphinx==1.5

Diff for: docs/Makefile

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Minimal makefile for Sphinx documentation
2+
#
3+
4+
# You can set these variables from the command line.
5+
SPHINXOPTS =
6+
SPHINXBUILD = sphinx-build
7+
SPHINXPROJ = hdhppy
8+
SOURCEDIR = .
9+
BUILDDIR = _build
10+
11+
# Put it first so that "make" without argument is like "make help".
12+
help:
13+
@$(SPHINXBUILD) -M help "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)
14+
15+
.PHONY: help Makefile
16+
17+
# Catch-all target: route all unknown targets to Sphinx using the new
18+
# "make mode" option. $(O) is meant as a shortcut for $(SPHINXOPTS).
19+
%: Makefile
20+
@$(SPHINXBUILD) -M $@ "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)

Diff for: docs/conf.py

+171
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
# -*- coding: utf-8 -*-
2+
#
3+
# hdhp.py documentation build configuration file, created by
4+
# sphinx-quickstart on Fri Dec 9 22:35:31 2016.
5+
#
6+
# This file is execfile()d with the current directory set to its
7+
# containing dir.
8+
#
9+
# Note that not all possible configuration values are present in this
10+
# autogenerated file.
11+
#
12+
# All configuration values have a default; values that are commented out
13+
# serve to show the default.
14+
15+
# If extensions (or modules to document with autodoc) are in another directory,
16+
# add these directories to sys.path here. If the directory is relative to the
17+
# documentation root, use os.path.abspath to make it absolute, like shown here.
18+
#
19+
# import os
20+
# import sys
21+
# sys.path.insert(0, os.path.abspath('.'))
22+
23+
24+
# -- General configuration ------------------------------------------------
25+
26+
# If your documentation needs a minimal Sphinx version, state it here.
27+
#
28+
# needs_sphinx = '1.0'
29+
30+
# Add any Sphinx extension module names here, as strings. They can be
31+
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom
32+
# ones.
33+
extensions = ['sphinx.ext.autodoc',
34+
'sphinx.ext.napoleon',
35+
'sphinx.ext.mathjax',
36+
'sphinx.ext.viewcode']
37+
38+
# Napoleon settings
39+
napoleon_numpy_docstring = True
40+
napoleon_include_init_with_doc = True
41+
napoleon_include_private_with_doc = False
42+
napoleon_include_special_with_doc = True
43+
napoleon_use_admonition_for_examples = False
44+
napoleon_use_admonition_for_notes = False
45+
napoleon_use_admonition_for_references = False
46+
napoleon_use_ivar = False
47+
napoleon_use_param = True
48+
napoleon_use_rtype = True
49+
50+
# Add any paths that contain templates here, relative to this directory.
51+
templates_path = ['_templates']
52+
53+
# The suffix(es) of source filenames.
54+
# You can specify multiple suffix as a list of string:
55+
#
56+
# source_suffix = ['.rst', '.md']
57+
source_suffix = '.rst'
58+
59+
# The master toctree document.
60+
master_doc = 'index'
61+
62+
# General information about the project.
63+
project = u'hdhp.py'
64+
copyright = u'2016, Charalampos Mavroforakis and contributors'
65+
author = u'Charalampos Mavroforakis and contributors'
66+
67+
# The version info for the project you're documenting, acts as replacement for
68+
# |version| and |release|, also used in various other places throughout the
69+
# built documents.
70+
#
71+
# The short X.Y version.
72+
version = u'1.0'
73+
# The full version, including alpha/beta/rc tags.
74+
release = u'1.0'
75+
76+
# The language for content autogenerated by Sphinx. Refer to documentation
77+
# for a list of supported languages.
78+
#
79+
# This is also used if you do content translation via gettext catalogs.
80+
# Usually you set "language" from the command line for these cases.
81+
language = None
82+
83+
# List of patterns, relative to source directory, that match files and
84+
# directories to ignore when looking for source files.
85+
# This patterns also effect to html_static_path and html_extra_path
86+
exclude_patterns = ['_build', 'Thumbs.db', '.DS_Store']
87+
88+
# The name of the Pygments (syntax highlighting) style to use.
89+
pygments_style = 'sphinx'
90+
91+
# If true, `todo` and `todoList` produce output, else they produce nothing.
92+
todo_include_todos = False
93+
94+
95+
# -- Options for HTML output ----------------------------------------------
96+
97+
# The theme to use for HTML and HTML Help pages. See the documentation for
98+
# a list of builtin themes.
99+
#
100+
html_theme = 'alabaster'
101+
102+
# Theme options are theme-specific and customize the look and feel of a theme
103+
# further. For a list of options available for each theme, see the
104+
# documentation.
105+
#
106+
# html_theme_options = {}
107+
108+
# Add any paths that contain custom static files (such as style sheets) here,
109+
# relative to this directory. They are copied after the builtin static files,
110+
# so a file named "default.css" will overwrite the builtin "default.css".
111+
html_static_path = ['_static']
112+
113+
114+
# -- Options for HTMLHelp output ------------------------------------------
115+
116+
# Output file base name for HTML help builder.
117+
htmlhelp_basename = 'hdhppydoc'
118+
119+
120+
# -- Options for LaTeX output ---------------------------------------------
121+
122+
latex_elements = {
123+
# The paper size ('letterpaper' or 'a4paper').
124+
#
125+
# 'papersize': 'letterpaper',
126+
127+
# The font size ('10pt', '11pt' or '12pt').
128+
#
129+
# 'pointsize': '10pt',
130+
131+
# Additional stuff for the LaTeX preamble.
132+
#
133+
# 'preamble': '',
134+
135+
# Latex figure (float) alignment
136+
#
137+
# 'figure_align': 'htbp',
138+
}
139+
140+
# Grouping the document tree into LaTeX files. List of tuples
141+
# (source start file, target name, title,
142+
# author, documentclass [howto, manual, or own class]).
143+
latex_documents = [
144+
(master_doc, 'hdhppy.tex', u'hdhp.py Documentation',
145+
u'Charalampos Mavroforakis and contributors', 'manual'),
146+
]
147+
148+
149+
# -- Options for manual page output ---------------------------------------
150+
151+
# One entry per manual page. List of tuples
152+
# (source start file, name, description, authors, manual section).
153+
man_pages = [
154+
(master_doc, 'hdhppy', u'hdhp.py Documentation',
155+
[author], 1)
156+
]
157+
158+
159+
# -- Options for Texinfo output -------------------------------------------
160+
161+
# Grouping the document tree into Texinfo files. List of tuples
162+
# (source start file, target name, title, author,
163+
# dir menu entry, description, category)
164+
texinfo_documents = [
165+
(master_doc, 'hdhppy', u'hdhp.py Documentation',
166+
author, 'hdhppy', 'One line description of project.',
167+
'Miscellaneous'),
168+
]
169+
170+
171+

Diff for: docs/index.rst

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
.. hdhp.py documentation master file, created by
2+
sphinx-quickstart on Fri Dec 9 22:35:31 2016.
3+
You can adapt this file completely to your liking, but it should at least
4+
contain the root `toctree` directive.
5+
6+
Welcome to hdhp.py's documentation!
7+
===================================
8+
9+
.. toctree::
10+
:maxdepth: 2
11+
:caption: Contents:
12+
13+
The ``infer`` function
14+
======================
15+
.. automodule:: hdhp
16+
:members: infer
17+
18+
The ``HDHProcess`` object
19+
=========================
20+
.. autoclass:: HDHProcess
21+
:members:
22+
23+
24+
.. Indices and tables
25+
.. ==================
26+
27+
.. * :ref:`genindex`
28+
.. * :ref:`modindex`
29+
.. * :ref:`search`

Diff for: hdhp/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from generative_model import HDHProcess
2+
from smc import Particle, infer
23

34

45
__all__ = ['utils']

Diff for: hdhp/generative_model.py

+118-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
:copyright: 2016 Charalampos Mavroforakis, <[email protected]> and contributors.
1111
:license: ISC
1212
"""
13-
from __future__ import division
13+
from __future__ import division, print_function
1414

1515
import datetime
1616
from collections import defaultdict
@@ -918,3 +918,120 @@ def plot(self, num_samples=500, T_min=0, T_max=None, start_date=None,
918918
plt.savefig(filename + '.pdf', transparent=True)
919919
sns.set_style(None)
920920
return fig
921+
922+
def user_patterns(self, user):
923+
"""Returns a list with the patterns that a user has adopted.
924+
925+
Parameters
926+
----------
927+
user : int
928+
"""
929+
pattern_list = [self.dish_on_table_per_user[user][table]
930+
for table in self.table_history_per_user[user]]
931+
return list(set(pattern_list))
932+
933+
def show_annotated_events(self, user=None, patterns=None, show_time=True,
934+
T_min=0, T_max=None):
935+
"""Returns a string where each event is annotated with the inferred
936+
pattern.
937+
938+
939+
Parameters
940+
----------
941+
user : int, default is None
942+
If given, the events returned are limited to the selected user
943+
944+
patterns : list, default is None
945+
If not None, an event is return only if it belongs to one of the
946+
selected patterns
947+
948+
show_time : bool, default is True
949+
Controls whether the time of the event will be shown
950+
951+
T_min : float, default is 0
952+
Controls the minimum timestamp after which the events will be shown
953+
954+
T_max : float, default is None
955+
If given, T_max controls the maximum timestamp shown
956+
957+
958+
Returns
959+
-------
960+
str
961+
"""
962+
if patterns is not None and type(patterns) is not set:
963+
patterns = set(patterns)
964+
965+
if show_time:
966+
return '\n'.join(['%5.3g pattern=%3d task=%3d (u=%d) %s' %
967+
(t, dish, table, u, doc)
968+
for u in range(self.num_users)
969+
for ((t, doc), (table, dish)) in
970+
zip([(t, d)
971+
for t, d in zip(self.time_history_per_user[u],
972+
self.document_history_per_user[u])],
973+
[(table, self.dish_on_table_per_user[u][table])
974+
for table in self.table_history_per_user[u]])
975+
if (user is None or user == u) and
976+
(patterns is None or dish in patterns)
977+
and t >= T_min
978+
and (T_max is None or (T_max is not None and t <= T_max))])
979+
else:
980+
return '\n'.join(['pattern=%3d task=%3d (u=%d) %s'
981+
% (dish, table, u, doc)
982+
for u in range(self.num_users)
983+
for ((t, doc), (table, dish)) in
984+
zip([(t, d)
985+
for t, d in zip(self.time_history_per_user[u],
986+
self.document_history_per_user[u])],
987+
[(table, self.dish_on_table_per_user[u][table])
988+
for table in self.table_history_per_user[u]])
989+
if (user is None or user == u) and
990+
(patterns is None or dish in patterns)
991+
and t >= T_min
992+
and (T_max is None or (T_max is not None and t <= T_max))])
993+
994+
def show_pattern_content(self, patterns=None, words=0, detail_threshold=5):
995+
"""Shows the content distrubution of the inferred patterns.
996+
997+
998+
Parameters
999+
----------
1000+
patterns : list, default is None
1001+
If not None, only the content of the selected patterns will be
1002+
shown
1003+
1004+
words : int, default is 0
1005+
A positive number that control how many words will be shown.
1006+
The words are being shown sorted by their likelihood, starting
1007+
with the most probable.
1008+
1009+
detail_threshold : int, default is 5
1010+
A positive number that sets the lower bound in the number of times
1011+
that a word appeared in a pattern so that its count is shown.
1012+
1013+
1014+
Returns
1015+
-------
1016+
str
1017+
"""
1018+
if patterns is None:
1019+
patterns = self.per_pattern_word_count.keys()
1020+
text = ['___Pattern %d___ \n%s\n%s'
1021+
% (pattern,
1022+
'\n'.join(['%s : %d'
1023+
% (k, v)
1024+
for i, (k, v)
1025+
in enumerate(sorted(self.per_pattern_word_counts[pattern].iteritems(),
1026+
key=lambda x: (x[1], x[0]),
1027+
reverse=True))
1028+
if v >= detail_threshold and (words == 0 or i < words)]
1029+
),
1030+
' '.join([k for i, (k, v)
1031+
in enumerate(sorted(self.per_pattern_word_counts[pattern].iteritems(),
1032+
key=lambda x: (x[1], x[0]),
1033+
reverse=True))
1034+
if v < detail_threshold and (words == 0 or i < words)])
1035+
)
1036+
for pattern in self.per_pattern_word_counts if pattern in patterns]
1037+
return '\n\n'.join(text)

0 commit comments

Comments
 (0)