Commits

Miha Stajdohar committed 1d90bc6

Added reference.

Comments (0)

Files changed (12)

+Add mm to the PYTHONPATH for examples to work.
+# Makefile for Sphinx documentation
+#
+
+# You can set these variables from the command line.
+SPHINXOPTS    =
+SPHINXBUILD   = sphinx-build
+PAPER         =
+
+# Internal variables.
+PAPEROPT_a4     = -D latex_paper_size=a4
+PAPEROPT_letter = -D latex_paper_size=letter
+#ALLSPHINXOPTS   = -d _build/doctrees $(PAPEROPT_$(PAPER)) $(SPHINXOPTS) .
+ALLSPHINXOPTS   = $(PAPEROPT_$(PAPER)) $(SPHINXOPTS) rst
+
+.PHONY: help clean html dirhtml pickle json htmlhelp qthelp latex changes linkcheck doctest
+
+help:
+	@echo "Please use \`make <target>' where <target> is one of"
+	@echo "  html      to make standalone HTML files"
+	@echo "  dirhtml   to make HTML files named index.html in directories"
+	@echo "  epub	   to make epub file"
+	@echo "  pickle    to make pickle files"
+	@echo "  json      to make JSON files"
+	@echo "  htmlhelp  to make HTML files and a HTML help project"
+	@echo "  qthelp    to make HTML files and a qthelp project"
+	@echo "  latex     to make LaTeX files, you can set PAPER=a4 or PAPER=letter"
+	@echo "  changes   to make an overview of all changed/added/deprecated items"
+	@echo "  linkcheck to check all external links for integrity"
+	@echo "  doctest   to run all doctests embedded in the documentation (if enabled)"
+
+clean:
+	-rm -rf _* *.html html .doctrees .buildinfo searchindex.js *.epub epub rst/_build
+
+epub:
+	$(SPHINXBUILD) -b epub $(ALLSPHINXOPTS) epub
+	mv epub/*.epub .
+	@echo
+	@echo "Build finished. The epub pages are in "
+
+html:
+	$(SPHINXBUILD) -b html $(ALLSPHINXOPTS) html
+	@echo
+	@echo "Build finished. The HTML pages are in .. ."
+
+dirhtml:
+	$(SPHINXBUILD) -b dirhtml $(ALLSPHINXOPTS) .dirhtml
+	@echo
+	@echo "Build finished. The HTML pages are in .dirhtml."
+
+pickle:
+	$(SPHINXBUILD) -b pickle $(ALLSPHINXOPTS) .pickle
+	@echo
+	@echo "Build finished; now you can process the pickle files."
+
+json:
+	$(SPHINXBUILD) -b json $(ALLSPHINXOPTS) .json
+	@echo
+	@echo "Build finished; now you can process the JSON files."
+
+htmlhelp:
+	$(SPHINXBUILD) -b htmlhelp $(ALLSPHINXOPTS) .htmlhelp
+	@echo
+	@echo "Build finished; now you can run HTML Help Workshop with the" \
+	      ".hhp project file in .htmlhelp."
+
+qthelp:
+	$(SPHINXBUILD) -b qthelp $(ALLSPHINXOPTS) .qthelp
+	@echo
+	@echo "Build finished; now you can run "qcollectiongenerator" with the" \
+	      ".qhcp project file in .qthelp, like this:"
+	@echo "# qcollectiongenerator .qthelp/OrangeWidgetCatalog.qhcp"
+	@echo "To view the help file:"
+	@echo "# assistant -collectionFile .qthelp/OrangeWidgetCatalog.qhc"
+
+latex:
+	$(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) .latex
+	@echo
+	@echo "Build finished; the LaTeX files are in .latex."
+	@echo "Run \`make all-pdf' or \`make all-ps' in that directory to" \
+	      "run these through (pdf)latex."
+
+changes:
+	$(SPHINXBUILD) -b changes $(ALLSPHINXOPTS) .changes
+	@echo
+	@echo "The overview file is in .changes."
+
+linkcheck:
+	$(SPHINXBUILD) -b linkcheck $(ALLSPHINXOPTS) .linkcheck
+	@echo
+	@echo "Link check complete; look for any errors in the above output " \
+	      "or in .linkcheck/output.txt."
+
+doctest:
+	$(SPHINXBUILD) -b doctest $(ALLSPHINXOPTS) .doctest
+	@echo "Testing of doctests in the sources finished, look at the " \
+	      "results in .doctest/output.txt."
+Building
+========
+
+The documentation can be built with Sphinx 1.0 or newer. Download it at
+http://sphinx.pocoo.org/. Orange, numpy and numpydoc Sphinx extension needs to 
+be installed. To build the documentation, run
+
+    make html
+
+which will create a directory "html" containing the documentation. If make is
+not installed on your machine, run
+
+    sphinx-build -b html rst html
+
+in docs. The last two parameters are the input and output directory.
+
+Structure
+=========
+
+The actual documentation is intermixed from docs/rst and documented Python 
+modules in mm. 
+
+Example scripts are in examples.
+# -*- coding: utf-8 -*-
+#
+# reference documentation build configuration file, created by
+# sphinx-quickstart on Wed Nov 17 12:52:23 2010.
+#
+# This file is execfile()d with the current directory set to its containing dir.
+#
+# Note that not all possible configuration values are present in this
+# autogenerated file.
+#
+# All configuration values have a default; values that are commented out
+# serve to show the default.
+
+import sys, os, os.path
+
+#rewrite formatargs function with different defaults
+sys.path.insert(0, os.path.dirname(__file__))
+import myinspect
+import sphinx.ext.autodoc
+import numpydoc
+sphinx.ext.autodoc.inspect = myinspect
+numpydoc.docscrape.inspect = myinspect
+
+#disable deprecation decorators for the documentation
+os.environ["orange_no_deprecated_members"] = "1"
+
+# If extensions (or modules to document with autodoc) are in another directory,
+# add these directories to sys.path here. If the directory is relative to the
+# documentation root, use os.path.abspath to make it absolute, like shown here.
+sys.path.append(os.path.abspath('../../mm'))
+
+# -- General configuration -----------------------------------------------------
+
+# Add any Sphinx extension module names here, as strings. They can be extensions
+# coming with Sphinx (named 'sphinx.ext.*') or your custom ones.
+extensions = ['sphinx.ext.autodoc', 'sphinx.ext.doctest', 'sphinx.ext.intersphinx', 'sphinx.ext.todo', 'sphinx.ext.coverage', 'sphinx.ext.pngmath', 'sphinx.ext.ifconfig', 'numpydoc']
+
+
+# Numpydoc generates autosummary directives for all undocumented members. Orange documentation only includes documented
+# member, so _str_member_list is modified to return [] where a list of undocumented members is originally returned.
+numpydoc.docscrape_sphinx.SphinxDocString._str_member_list # if numpydoc changes, this line will cause an error
+numpydoc.docscrape_sphinx.SphinxDocString._str_member_list = lambda x, y : []
+
+
+# Add any paths that contain templates here, relative to this directory.
+templates_path = ['_templates']
+
+# The suffix of source filenames.
+source_suffix = '.rst'
+
+# The encoding of source files.
+#source_encoding = 'utf-8'
+
+# The master toctree document.
+master_doc = 'index'
+
+# General information about the project.
+project = u'Model Map'
+copyright = u'Miha Stajdohar, FRI UL'
+
+# The version info for the project you're documenting, acts as replacement for
+# |version| and |release|, also used in various other places throughout the
+# built documents.
+#
+# The short X.Y version.
+version = '0.1'
+# The full version, including alpha/beta/rc tags.
+release = '0.1 beta'
+
+# The language for content autogenerated by Sphinx. Refer to documentation
+# for a list of supported languages.
+#language = None
+
+# There are two options for replacing |today|: either, you set today to some
+# non-false value, then it is used:
+#today = ''
+# Else, today_fmt is used as the format for a strftime call.
+#today_fmt = '%B %d, %Y'
+
+# List of documents that shouldn't be included in the build.
+#unused_docs = []
+
+# List of directories, relative to source directory, that shouldn't be searched
+# for source files.
+exclude_trees = ['_build']
+
+# The reST default role (used for this markup: `text`) to use for all documents.
+#default_role = None
+
+# If true, '()' will be appended to :func: etc. cross-reference text.
+#add_function_parentheses = True
+
+# If true, the current module name will be prepended to all description
+# unit titles (such as .. function::).
+#add_module_names = True
+
+# If true, sectionauthor and moduleauthor directives will be shown in the
+# output. They are ignored by default.
+#show_authors = False
+
+# The name of the Pygments (syntax highlighting) style to use.
+pygments_style = 'sphinx'
+
+# A list of ignored prefixes for module index sorting.
+#modindex_common_prefix = []
+
+
+# -- Options for HTML output ---------------------------------------------------
+
+# The theme to use for HTML and HTML Help pages.  Major themes that come with
+# Sphinx are currently 'default' and 'sphinxdoc'.
+html_theme = 'sphinxdoc'
+
+# Theme options are theme-specific and customize the look and feel of a theme
+# further.  For a list of options available for each theme, see the
+# documentation.
+#html_theme_options = {"collapsiblesidebar": "false"}
+
+if html_theme == "orange_theme":
+    html_theme_options.update({"orangeheaderfooter": "false"})
+
+# Add any paths that contain custom themes here, relative to this directory.
+#html_theme_path = ["../../sphinx-ext/themes"]
+
+# The name for this set of Sphinx documents.  If None, it defaults to
+# "<project> v<release> documentation".
+html_title = "Model Map Reference"
+
+# A shorter title for the navigation bar.  Default is the same as html_title.
+#html_short_title = None
+
+# The name of an image file (relative to this directory) to place at the top
+# of the sidebar.
+#html_logo = None
+
+# The name of an image file (within the static path) to use as favicon of the
+# docs.  This file should be a Windows icon file (.ico) being 16x16 or 32x32
+# pixels large.
+#html_favicon = None
+
+# Add any paths that contain custom static files (such as style sheets) here,
+# relative to this directory. They are copied after the builtin static files,
+# so a file named "default.css" will overwrite the builtin "default.css".
+html_static_path = []
+
+# If not '', a 'Last updated on:' timestamp is inserted at every page bottom,
+# using the given strftime format.
+#html_last_updated_fmt = '%b %d, %Y'
+
+# If true, SmartyPants will be used to convert quotes and dashes to
+# typographically correct entities.
+#html_use_smartypants = True
+
+# Custom sidebar templates, maps document names to template names.
+#html_sidebars = {}
+
+# Additional templates that should be rendered to pages, maps page names to
+# template names.
+#html_additional_pages = {}
+
+# If false, no module index is generated.
+#html_use_modindex = True
+
+# If false, no index is generated.
+#html_use_index = True
+
+# If true, the index is split into individual pages for each letter.
+#html_split_index = False
+
+# If true, links to the reST sources are added to the pages.
+#html_show_sourcelink = True
+
+# If true, an OpenSearch description file will be output, and all pages will
+# contain a <link> tag referring to it.  The value of this option must be the
+# base URL from which the finished HTML is served.
+#html_use_opensearch = ''
+
+# If nonempty, this is the file name suffix for HTML files (e.g. ".xhtml").
+#html_file_suffix = ''
+
+# Output file base name for HTML help builder.
+htmlhelp_basename = 'referencedoc'
+
+
+# -- Options for LaTeX output --------------------------------------------------
+
+# The paper size ('letter' or 'a4').
+#latex_paper_size = 'letter'
+
+# The font size ('10pt', '11pt' or '12pt').
+#latex_font_size = '10pt'
+
+# Grouping the document tree into LaTeX files. List of tuples
+# (source start file, target name, title, author, documentclass [howto/manual]).
+latex_documents = [
+  ('index', 'reference.tex', u'Reference',
+   u't', 'manual'),
+]
+
+# The name of an image file (relative to this directory) to place at the top of
+# the title page.
+#latex_logo = None
+
+# For "manual" documents, if this is true, then toplevel headings are parts,
+# not chapters.
+#latex_use_parts = False
+
+# Additional stuff for the LaTeX preamble.
+#latex_preamble = ''
+
+# Documents to append as an appendix to all manuals.
+#latex_appendices = []
+
+# If false, no module index is generated.
+#latex_use_modindex = True
+
+# -- Options for Epub output ---------------------------------------------------
+
+# Bibliographic Dublin Core info.
+epub_title = u'Model Map Reference'
+epub_author = u'Miha Stajdohar'
+epub_publisher = u'Miha Stajdohar'
+epub_copyright = u'Miha Stajdohar, FRI UL'
+
+# The language of the text. It defaults to the language option
+# or en if the language is not set.
+epub_language = 'en'
+
+# The scheme of the identifier. Typical schemes are ISBN or URL.
+#epub_scheme = ''
+
+# The unique identifier of the text. This can be a ISBN number
+# or the project homepage.
+#epub_identifier = ''
+
+# A unique identification for the text.
+#epub_uid = ''
+
+# HTML files that should be inserted before the pages created by sphinx.
+# The format is a list of tuples containing the path and title.
+#epub_pre_files = []
+
+# HTML files that should be inserted after the pages created by sphinx.
+# The format is a list of tuples containing the path and title.
+#epub_post_files = []
+
+# A list of files that should not be packed into the epub file.
+epub_exclude_files = ["index.html", "genindex.html", "py-modindex.html", "search.html"]
+
+# The depth of the table of contents in toc.ncx.
+#epub_tocdepth = 3
+
+# Allow duplicate toc entries.
+#epub_tocdup = True
+
+# Example configuration for intersphinx: refer to the Python standard library.
+intersphinx_mapping = {'http://docs.python.org/': None}
+
+import types
+from sphinx.ext import autodoc
+
+def maybe_skip_member(app, what, name, obj, skip, options):
+    #print app, what, name, obj, skip, options
+
+    #allow class methods that begin with __ and end with __
+    if isinstance(obj, types.MethodType) \
+        and not isinstance(options.members, list) \
+        and name.startswith("__") \
+        and name.endswith("__") \
+        and (obj.__doc__ != None or options.get("undoc-members", False)):
+            return False
+
+class SingletonDocumenter(autodoc.ModuleLevelDocumenter):
+    """
+    Specialized Documenter subclass for singleton items.
+    """
+    objtype = 'singleton'
+    directivetype = 'data'
+
+    member_order = 40
+
+    @classmethod
+    def can_document_member(cls, member, membername, isattr, parent):
+        return isinstance(parent, autodoc.ModuleDocumenter) and isattr
+
+    def document_members(self, all_members=False):
+        pass
+
+    def add_content(self, more_content, no_docstring=False):
+        self.add_line(u'Singleton instance of :py:class:`~%s`.' % (self.object.__class__.__name__,), '<autodoc>')
+
+def setup(app):
+    app.connect('autodoc-skip-member', maybe_skip_member)
+    app.add_autodocumenter(SingletonDocumenter)

docs/rst/index.rst

+###################
+Model Map Reference
+###################
+
+.. toctree::
+   :maxdepth: 1
+   
+   mm.model
+   
+   mm.modelmap
+
+
+****************
+Index and search
+****************
+
+* :ref:`genindex`
+* :ref:`modindex`
+* :ref:`search`

docs/rst/mm.model.rst

+####################
+model (``mm.model``)
+####################
+
+.. automodule:: mm.model
+   :members:

docs/rst/mm.modelmap.rst

+###########################
+modelmap (``mm.modelmap``)
+###########################
+
+.. automodule:: mm.modelmap
+   :members:
+##################
+Model Map (``mm``)
+##################
+
+.. automodule:: mm.model
+   :members:
+   
+   
+.. automodule:: mm.modelmap
+   :members:

docs/rst/myinspect.py

+from inspect import *
+ofas = formatargspec
+
+def delete_while_accurate(s, accuracy=0.0000001):
+    """ Shortens a string containing a float
+    while still keeping the desired accuracy. """
+    orig = float(s)
+    cur = s
+    try:
+        while abs((float(cur[:-1])-orig)/orig) < accuracy:
+            cur = cur[:-1]
+    except:
+        pass
+    return cur
+
+def nfas(*args, **kwargs):
+
+    def fv(value):
+        if isinstance(value, float):
+            cand = str(value)
+            value = delete_while_accurate(cand)
+        return "=" + str(value)
+
+    if "formatvalue" not in kwargs and len(args) < 8:
+        kwargs["formatvalue"] = fv
+
+    return ofas(*args, **kwargs)
+
+formatargspec = nfas
+
+# inspect.getmembers from Python 2.7
+# In Python 2.6 there is try/except missing and function fails on C++-based classes if they define attributes
+# Example: AttributeError: 'Plot' object attribute 'animate_points' is an instance attribute
+def getmembers27(object, predicate=None):
+    """Return all members of an object as (name, value) pairs sorted by name.
+    Optionally, only return members that satisfy a given predicate."""
+    results = []
+    for key in dir(object):
+        try:
+            value = getattr(object, key)
+        except AttributeError:
+            continue
+        if not predicate or predicate(value):
+            results.append((key, value))
+    results.sort()
+    return results
+
+getmembers = getmembers27
+
+if __name__ == "__main__":
+    for a in [ "0", "0.", "-1.3", "1.30000000001" ]:
+        print a, "shorter:", delete_while_accurate(a)

examples/projections.py

 smx = build_map.build_model_matrix(models)
 table = build_map.build_model_data(models)
 
+build_map.save("%s/_projections_/proj" % ROOT, smx=smx, table=table)
+"""
+.. index:: model
+
+*****
+Model
+*****
+
+.. autoclass:: mm.Model
+   :members:
+
+"""
+
 import uuid
 
 from itertools import groupby
     def __init__(self, type_, classifier, probabilities, attributes, \
                  instance_predictions=None, instance_classes=None, \
                  name=None, XAnchors=None, YAnchors=None):
-        """Meta-model, a node in Model Map."""
+        """Meta-model, a node in Model Map.
+        
+        :param type_: model type; must be in MODEL_LIST
+        :type type_: string
+        
+        :param classifier: classifier object of this model
+        :type classifier: :obj:`Orange.classification.Classifier`
+        
+        :param probabilities: list of predicted probabilities (for all classes) 
+        :type probabilities: list of :obj:`numpy.ndarray`
+        
+        :param attributes: list of attribute names
+        :type attributes: list
+        
+        :param instance_predictions: array of predicted classes for all instances
+        :type instance_predictions: :obj:`numpy.ndarray`
+        
+        :param instance_classes: array of true classes for all instances
+        :type instance_classes: :obj:`numpy.ndarray`
+        
+        :param name: model name
+        :type name: string
+        
+        :param XAnchors: 
+        :type XAnchors: list 
+        
+        :param YAnchors: 
+        :type YAnchors: list
+        
+        """
 
         self.uuid = uuid.uuid4().hex
         self.type = type_
         self.attributes = attributes
         self.instance_predictions = instance_predictions
         self.instance_classes = instance_classes
-        self.name = name if name is not None else type
+        self.name = name if name is not None else self.type
         self.XAnchors = XAnchors
         self.YAnchors = YAnchors
 
         #ex["AUC"] = nets[i].items[m]["AUC"].value
         resultsByClass = sorted([(p == c, c) for p, c in zip(self.instance_predictions, self.instance_classes)], key=itemgetter(1))
         groups = []
-        for _k, g in groupby(resultsByClass, lambda x: x[1].value):
+        for _k, g in groupby(resultsByClass, lambda x: x[1]):
             resultsByClass, _classes = zip(*g)
             groups.append(resultsByClass)
         inst["CA by class"] = ', '.join([str(sum(results) / float(len(results))) for results in groups])
-import itertools, math, random, os.path, time, uuid
+"""
+.. index:: model map
+
+***************
+Build Model Map
+***************
+
+.. autoclass:: mm.BuildModelMap
+   :members:
+   
+**************
+Help Functions
+**************
+
+"""
+
+import bz2, itertools, math, random, os.path, time, uuid
 import cPickle as pickle
 
 import scipy.stats
                      learner(table),
                      probabilities,
                      attributes,
-                     instance_predictions,
-                     instance_classes,
+                     np.array([c.value for c in instance_predictions]),
+                     np.array([c.value for c in instance_classes]),
                      XAnchors=XAnchors,
                      YAnchors=YAnchors)
 
         table.extend([model.get_instance(table.domain) for model in models])
         return table
 
-    def save_models(self, models, smx, fn):
-        """Save models to disk."""
+    def save(self, fname, models=None, smx=None, table=None):
+        """Save model map to disk. Model similarity matrix and data table tuple 
+        is pickled and compressed as a bz2 archive.
+        
+        """
 
-        print 'saving matrix'
-#        if type(smx) == type([]):
-#            for s, title in smx:
-#                np.save('%s-%s' % (fn, title), s)
-#        else:
-#            np.save('%s' % (fn), smx)
-#
-#        print 'build out data'
-#
-#        print 'saving out data'
-#        out.save('%s.tab' % (fn))
-#        print 'saving models'
-#        pickle.dump(dict(zip(uuids, models)), open('%s.res' % (fn), "wb"))
+        if models is None and (smx is None or table is None):
+            raise AttributeError("If models is none, smx and table must be given.")
 
+        if models is not None:
+            if type(models) != type([]):
+                raise AttributeError("Attribute models must be a list of models.")
 
+            if len(models) <= 0:
+                raise AttributeError("Attribute models is an empty list.")
+
+        if smx is None:
+            smx = self.build_model_matrix(models)
+
+        if table is None:
+            table = self.build_model_data(models)
+
+        pickle.dump((smx, table), bz2.BZ2File('%s.bz2' % fname, "w"), -1)
+
+    def load(self, fname):
+        """Load a model map. Read compressed tuple containing model similarity 
+        matrix and data table.
+        
+        """
+
+        smx, table = pickle.load(bz2.BZ2File('%s.bz2' % fname, "r"))
+        return smx, table