Skip to main content

Parsing, executing, and calculating expected information gain for program-form questions.

Project description

Question Programs & Expected Information Gain

This is a package for parsing/executing questions and calculating Expected Information Gain (EIG) for question programs defined on the Battleship Dataset in the paper "Question Asking as Program Generation".

This package provide a Pure python version (slow) and a Python/C++ hybrid version (fast). Both versions have the same API but different implementations.

Installation

This package can be installed using pip

pip install expected-information-gain

Basic Usage

The following example shows how to execute a program on a given board

# define a board using BattleshipHypothesis
from eig.battleship import Ship, BattleshipHypothesis, Parser, Executor
ships = [Ship(ship_label=1, topleft=(0, 0), size=2, orientation='V'),
             Ship(ship_label=2, topleft=(1, 2), size=2, orientation='V')
hypothesis = BattleshipHypothesis(grid_size=3, ships=ships)
# the board looks like this
# B W W
# B W R
# W W R

# parse and execute the program
question = Parser.parse("(bottomright (coloredTiles Red))")
executor = Executor(question)
executor.execute(hypothesis)    # (2, 2)

# we can also evaluate general arithmic and logical expressions, with whatever hypothesis provided
question2 = Parser.parse("(and (not (< 4 9)) (== (+ 1 3) 4))")
executor2 = Executor(question)
executor.execute(hypothesis)    # False

The next example shows how to calculate Expected Information Gain on a partly revealed board

# first we need to construct a hypothesis space 
# We suggest to do this as an initialization step, and use this instance every time
# Because this step is time consuming, and may take several seconds to finish.
from eig.battleship import BattleshipHypothesisSpace
hypotheses = BattleshipHypothesisSpace(grid_size=6, ship_labels=[1, 2, 3], 
            ship_sizes=[2, 3, 4], orientations=['V', 'H'])

# suppose we have a program and a partly revealed board
import numpy as np
program = "..."
board = np.array([...])

# next we can calculate EIG as follows
from eig import compute_eig_basic
from eig.battleship.program import ProgramSyntaxError
try:
    score = compute_eig_basic(hypotheses, program, board)
except ProgramSyntaxError:          # if the program is invalid, a ProgramSyntaxError will be raised
    # do something
except RuntimeError:                # if error happens during execution, a RuntimeError will be raised
    # do something

Advanced Usage

We also provide some advanced interfaces, which are more efficient when the users need to calculate EIG for many programs on one given board, and they also allows users to incorporate more complicated distributions.

# construct the hypothesis space
from eig.battleship import BattleshipHypothesisSpace
hypotheses = BattleshipHypothesisSpace(grid_size=6, ship_labels=[1, 2, 3], 
            ship_sizes=[2, 3, 4], orientations=['V', 'H'])

# calculate EIG as follows
from eig import compute_eig, Bayes, Context
from eig.battleship import Parser, Executor
from eig.battleship.program import ProgramSyntaxError
try:
    ast = Parser.parse(program)     # parse the program into abstract syntax tree
    executor = Executor(ast)        # obtain an executor to execute the program
    prior = EqualSizesDistribution(ship_labels=[1, 2, 3])   # a more cognitive inspired prior distribution
    belief = eig.Bayes(hypotheses, prior)       # a prior belief given the hypothesis space
    context = eig.Context(hypotheses, belief)   # context stores the posterior belief
    context.observe(board)                      # update posterior belief given the board
    score = eig.compute_eig(executor, context)  # compute EIG given program and posterior belief
except ProgramSyntaxError:          # if the program is invalid, a ProgramSyntaxError will be raised
    # do something
except RuntimeError:                # if error happens during execution, a RuntimeError will be raised
    # do something

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

expected-information-gain-1.0.3.tar.gz (23.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

expected_information_gain-1.0.3-cp36-cp36m-macosx_10_7_x86_64.whl (213.0 kB view details)

Uploaded CPython 3.6mmacOS 10.7+ x86-64

File details

Details for the file expected-information-gain-1.0.3.tar.gz.

File metadata

  • Download URL: expected-information-gain-1.0.3.tar.gz
  • Upload date:
  • Size: 23.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.4.2 requests/2.18.4 setuptools/39.1.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.6.5

File hashes

Hashes for expected-information-gain-1.0.3.tar.gz
Algorithm Hash digest
SHA256 fa5b660959b3d5fdf0b361256071dda69f6b5af1ec8d17328b35368d9cb60bfe
MD5 2e0d0bd311e9925f21bf7168c6d22611
BLAKE2b-256 3f417f07b274124896f1e79bcbef306990992e0ac83acc131285f4cef36133c8

See more details on using hashes here.

File details

Details for the file expected_information_gain-1.0.3-cp36-cp36m-macosx_10_7_x86_64.whl.

File metadata

File hashes

Hashes for expected_information_gain-1.0.3-cp36-cp36m-macosx_10_7_x86_64.whl
Algorithm Hash digest
SHA256 c3e3e6128fbe92515accaac0f5aa6594f4a952b352a4e033bea46ef7543db3fd
MD5 f4838cc76c37cc2f60070d88abbd2b91
BLAKE2b-256 284ff82a52e6d3fe3dd56d6d6620348872df324e8ecfeb13857898a9c7044898

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page