<commit_before>import numpy as np
import re

_if_idx_str = ("""if (exist("idx", "var"));\n"""
              """  idx = idx + 1;\n"""
              """else;\n"""
              """  idx = 1;\n"""
              """end;"""
              )

_num_pattern = "([0-9]+[.]?[0-9]*[Ee]?[+-]?[0-9]*)"

_numpy_array_pattern = r"\[[0-9\sEe+-.,%]*\]"
_matlab_array_pattern = r"\[[0-9\sEe+-.%]*\]"

_comment_array_pattern = r"\[([\d\sEe+-.,]*\s*[%#]+\s*[%#\w\s.,+-]*)\]"
_comment_line_pattern = r"([\d\s\tEe+-.]*)\s*([%#]*)\s*([#\w\s]*\n)"

_lhs_variable_pattern = r"(\w+)\s*(\(idx.*?\))"
_rhs_variable_pattern = r"(\w+)\s*\(idx.*?\)\s*=\s*(.*)"

_zeros_pattern = r"(zeros)\((.*)\)"

_detector_pattern = r"(DET\w+)\s*=\s*np.array\("

def _replace_comments(s):
    """Replaces matlab comments with python arrays in string s."""
    s = s.replace('%', '#')
    return s

def _replace_semicolons(s):
    """Replaces matlab semicolons with nothing in string s."""
    s = s.replace(';', '')
    return s

def _replace_arrays(s):
    """Replaces matlab arrays with numpy arrays in string s."""

    # Replace matlab arrays with python lists
    arrays = re.findall(_matlab_array_pattern, s)
    for a in arrays:
        new_a = re.sub(_num_pattern, lambda mo: mo.group(0) + ',', a)
        s = s.replace(a, new_a)

    # Encapsulate python lists in numpy arrays
    s = re.sub(_numpy_array_pattern, lambda mo: 'np.array(' + mo.group(0) + ')', s)

    return s

def parse_res(resfile, write_py=False):
    """Converts a serpent results ``*_res.m`` output file to a dictionary (and
    optionally to a ``*_res.py`` file).

    Parameters
    ----------
    resfile : str or file-like object
        Path to results file or a res file handle.
    write_py : bool, optional
        Flag for whether to write the res file to an analogous python file.

    Returns
    -------
    res : dict
        Dictionary of the parsed results.  Please see the Serpent manual for
        a complete description of contents.

    """
    if isinstance(resfile, basestring):
        with open(resfile, 'r') as mfile:
            f = mfile.read()
    else:
        f = resfile.read()

    # Keep comments around
    f = _replace_comments(f)

    # Grab the number of 'if' statements
    IDX = f.count(_if_idx_str)

    # Replace if statements with something more meaningful
    fpart = f.partition(_if_idx_str)
    f = fpart[0] + "idx = 0" + fpart[2]
    f = f.replace(_if_idx_str, 'idx += 1')

    # Replace matlab Arrays
    f = _replace_arrays(f)

    # Add imports to header
    header = "import numpy as np\n\n"

    # Find all variables and shape
    vars_shape = np.unique( re.findall(_lhs_variable_pattern, f) )
    vars_dtype = dict( re.findall(_rhs_variable_pattern, f) )
    # Initialize variables to zero
    header = header + "# Initialize variables\n"
    for vs in vars_shape:
        # Determine shape
        s = re.search(r'\[.*:(.*?)\]', vs[1])
        if s == None:
            vs_shape = ""
        else:
            vs_shape = s.group(1)
            vs_shape = vs_shape.split()
            vs_shape = ", ".join(vs_shape)

        # Determine Data type
        rhs = vars_dtype[vs[0]]
        if ("\'" in rhs) or ("\"" in rhs):
            dt = "'S{0}'".format(int( s.group(1) ))
            vs_shape = ""
        elif ('.' in rhs) or ('E' in rhs) or ('e' in rhs):
            dt = "float"
        else:
            dt = "int"

        zero_line = "{0} = np.zeros([{1}, {2}], dtype={3})\n".format(vs[0], IDX, vs_shape, dt)
        header = header + zero_line

    # Add IDx to file
    if 0 < IDX:
        header = header + "\n\n# Maximum Index\n\nIDX = {0}\n\n".format(IDX)

    # Add header to file
    f = header + f

    # Replace variable overrides
    vars = np.unique( re.findall("(" + _lhs_variable_pattern + ")", f) )
    for v in vars:
        f = f.replace(v[0], "{0}[idx] ".format(v[1]))

    # Remove semicolons
    f = _replace_semicolons(f)

    # Write the file out
    if write_py:
        if isinstance(resfile, basestring):
            new_filename = resfile.rpartition('.')[0] + '.py'
        else:
            new_filename = resfile.name.rpartition('.')[0] + '.py'
        with open(new_filename, 'w') as pyfile:
            pyfile.write(f)

    # Execute the adjusted file
    res = {}
    exec(f, {}, res)

    return res

def parse_dep(depfile, write_py=False, make_mats=True):
    """Converts a serpent depletion ``*_dep.m`` output file to a dictionary (and
    optionally to a ``*_dep.py`` file).

    Parameters
    ----------
    depfile : str or file-like object
        Path to depletion file or a dep file handle.
    write_py : bool, optional
        Flag for whether to write the dep file to an analogous python file.
    make_mats : bool, optional
        Flag for whether or not to build Materials out of mass data and add
        these to the return dictionary.  Materials so added have names which
        end in '_MATERIAL'.

    Returns
    -------
    dep : dict
        Dictionary of the parsed depletion information.  Please see the Serpent
        manual for a complete description of contents.

    """
    if isinstance(depfile, basestring):
        with open(depfile, 'r') as mfile:
            f = mfile.read()
    else:
        f = depfile.read()

    # Keep comments around
    f = _replace_comments(f)

    # Replace matlab Arrays
    f = _replace_arrays(f)

    # Now to find and convert arrays that have comments in them
    comment_arrays = re.findall("(" + _comment_array_pattern + ")", f)
    for ca in comment_arrays:
        new_ca = ca[0]
        comment_lines = re.findall("(" + _comment_line_pattern + ")", ca[1])
        for cl in comment_lines:
            new_cl = re.sub(_num_pattern, lambda mo: mo.group(0) + ',', cl[1])
            if new_cl[0] == '\n':
                new_cl = "\n    [" + new_cl.strip() + "], "
            else:
                new_cl = "    [" + new_cl.strip() + "], "

            new_ca = new_ca.replace(cl[1], new_cl)

        new_ca = 'np.array( ' + new_ca + ' )'
        f = f.replace(ca[0], new_ca)

    # Indent close of array
    f = f.replace("\n] )", "\n    ] )")

    # Replace MatLab zeros with numpy zeros
    f = re.sub(_zeros_pattern, lambda mo: "np.zeros((" + mo.group(2) + "))", f)

    # Replace some math operators
    f = f.replace('.*', "*")
    f = f.replace('./', "/")

    # Remove semicolons
    f = _replace_semicolons(f)

    # Add imports to header
    header = "import numpy as np\n"
    if make_mats:
        header += "from pyne.material import Material\n"
    header += "\n"

    # Add materials
    footer = ""
    if make_mats:
        mat_gen_line = "{name}MATERIAL = [{name}VOLUME * Material(dict(zip(ZAI[:-2], {name}MDENS[:-2, col]))) for col in range(len(DAYS))]\n"
        footer += "\n\n# Construct materials\n"
        base_names = re.findall('(MAT_\w*_)MDENS = ', f)
        for base_name in base_names:
            footer += mat_gen_line.format(name=base_name)
        footer += "TOT_MATERIAL = [Material(dict(zip(ZAI[:-2], TOT_MASS[:-2, col]))) for col in range(len(DAYS))]\n"

    # Add header & footer to file
    f = header + f + footer

    # Write the file out
    if write_py:
        if isinstance(depfile, basestring):
            new_filename = depfile.rpartition('.')[0] + '.py'
        else:
            new_filename = depfile.name.rpartition('.')[0] + '.py'
        with open(new_filename, 'w') as pyfile:
            pyfile.write(f)

    # Execute the adjusted file
    dep = {}
    exec(f, {}, dep)

    return dep

def parse_det(detfile, write_py=False):
    """Converts a serpent detector ``*_det.m`` output file to a dictionary (and
    optionally to a ``*_det.py`` file).

    Parameters
    ----------
    detfile : str or file-like object
        Path to detector file or a det file handle.
    write_py : bool, optional
        Flag for whether to write the det file to an analogous python file.

    Returns
    -------
    det : dict
        Dictionary of the parsed detector.  Please see the Serpent manual for
        a complete description of contents.

    """
    if isinstance(detfile, basestring):
        with open(detfile, 'r') as mfile:
            f = mfile.read()
    else:
        f = detfile.read()

    # Keep comments around
    f = _replace_comments(f)

    # Replace matlab Arrays
    f = _replace_arrays(f)

    # Find detector variable names
    det_names = re.findall(_detector_pattern, f)
    det_names = np.unique(det_names)

    # Append detector reshaping
    f += "\n\n# Reshape detectors\n"
    for dn in det_names:
        if dn + "E" in det_names:
            f += "{name}.shape = ({name}_VALS, 13)\n".format(name=dn)
        else:
            f += "{name}.shape = ({name_min_E}_EBINS, 3)\n".format(name=dn, name_min_E=dn[:-1])

    # Add imports to header
    header = "import numpy as np\n\n"

    # Add header to file
    f = header + f

    # Remove semicolons
    f = _replace_semicolons(f)

    # Write the file out
    if write_py:
        if isinstance(detfile, basestring):
            new_filename = detfile.rpartition('.')[0] + '.py'
        else:
            new_filename = detfile.name.rpartition('.')[0] + '.py'
        with open(new_filename, 'w') as pyfile:
            pyfile.write(f)

    # Execute the adjusted file
    det = {}
    exec(f, {}, det)

    return det
<commit_msg>FIX for serpernt reader issue whereby new versions of numpy int arrays are np.int64 and do not cast to Python integers automatically.<commit_after>import numpy as np
import re

_if_idx_str = ("""if (exist("idx", "var"));\n"""
              """  idx = idx + 1;\n"""
              """else;\n"""
              """  idx = 1;\n"""
              """end;"""
              )

_num_pattern = "([0-9]+[.]?[0-9]*[Ee]?[+-]?[0-9]*)"

_numpy_array_pattern = r"\[[0-9\sEe+-.,%]*\]"
_matlab_array_pattern = r"\[[0-9\sEe+-.%]*\]"

_comment_array_pattern = r"\[([\d\sEe+-.,]*\s*[%#]+\s*[%#\w\s.,+-]*)\]"
_comment_line_pattern = r"([\d\s\tEe+-.]*)\s*([%#]*)\s*([#\w\s]*\n)"

_lhs_variable_pattern = r"(\w+)\s*(\(idx.*?\))"
_rhs_variable_pattern = r"(\w+)\s*\(idx.*?\)\s*=\s*(.*)"

_zeros_pattern = r"(zeros)\((.*)\)"

_detector_pattern = r"(DET\w+)\s*=\s*np.array\("

def _replace_comments(s):
    """Replaces matlab comments with python arrays in string s."""
    s = s.replace('%', '#')
    return s

def _replace_semicolons(s):
    """Replaces matlab semicolons with nothing in string s."""
    s = s.replace(';', '')
    return s

def _replace_arrays(s):
    """Replaces matlab arrays with numpy arrays in string s."""

    # Replace matlab arrays with python lists
    arrays = re.findall(_matlab_array_pattern, s)
    for a in arrays:
        new_a = re.sub(_num_pattern, lambda mo: mo.group(0) + ',', a)
        s = s.replace(a, new_a)

    # Encapsulate python lists in numpy arrays
    s = re.sub(_numpy_array_pattern, lambda mo: 'np.array(' + mo.group(0) + ')', s)

    return s

def parse_res(resfile, write_py=False):
    """Converts a serpent results ``*_res.m`` output file to a dictionary (and
    optionally to a ``*_res.py`` file).

    Parameters
    ----------
    resfile : str or file-like object
        Path to results file or a res file handle.
    write_py : bool, optional
        Flag for whether to write the res file to an analogous python file.

    Returns
    -------
    res : dict
        Dictionary of the parsed results.  Please see the Serpent manual for
        a complete description of contents.

    """
    if isinstance(resfile, basestring):
        with open(resfile, 'r') as mfile:
            f = mfile.read()
    else:
        f = resfile.read()

    # Keep comments around
    f = _replace_comments(f)

    # Grab the number of 'if' statements
    IDX = f.count(_if_idx_str)

    # Replace if statements with something more meaningful
    fpart = f.partition(_if_idx_str)
    f = fpart[0] + "idx = 0" + fpart[2]
    f = f.replace(_if_idx_str, 'idx += 1')

    # Replace matlab Arrays
    f = _replace_arrays(f)

    # Add imports to header
    header = "import numpy as np\n\n"

    # Find all variables and shape
    vars_shape = np.unique( re.findall(_lhs_variable_pattern, f) )
    vars_dtype = dict( re.findall(_rhs_variable_pattern, f) )
    # Initialize variables to zero
    header = header + "# Initialize variables\n"
    for vs in vars_shape:
        # Determine shape
        s = re.search(r'\[.*:(.*?)\]', vs[1])
        if s == None:
            vs_shape = ""
        else:
            vs_shape = s.group(1)
            vs_shape = vs_shape.split()
            vs_shape = ", ".join(vs_shape)

        # Determine Data type
        rhs = vars_dtype[vs[0]]
        if ("\'" in rhs) or ("\"" in rhs):
            dt = "'S{0}'".format(int( s.group(1) ))
            vs_shape = ""
        elif ('.' in rhs) or ('E' in rhs) or ('e' in rhs):
            dt = "float"
        else:
            dt = "int"

        zero_line = "{0} = np.zeros([{1}, {2}], dtype={3})\n".format(vs[0], IDX, vs_shape, dt)
        header = header + zero_line

    # Add IDx to file
    if 0 < IDX:
        header = header + "\n\n# Maximum Index\n\nIDX = {0}\n\n".format(IDX)

    # Add header to file
    f = header + f

    # Replace variable overrides
    vars = np.unique( re.findall("(" + _lhs_variable_pattern + ")", f) )
    for v in vars:
        f = f.replace(v[0], "{0}[idx] ".format(v[1]))

    # Remove semicolons
    f = _replace_semicolons(f)

    # Write the file out
    if write_py:
        if isinstance(resfile, basestring):
            new_filename = resfile.rpartition('.')[0] + '.py'
        else:
            new_filename = resfile.name.rpartition('.')[0] + '.py'
        with open(new_filename, 'w') as pyfile:
            pyfile.write(f)

    # Execute the adjusted file
    res = {}
    exec(f, {}, res)

    return res

def parse_dep(depfile, write_py=False, make_mats=True):
    """Converts a serpent depletion ``*_dep.m`` output file to a dictionary (and
    optionally to a ``*_dep.py`` file).

    Parameters
    ----------
    depfile : str or file-like object
        Path to depletion file or a dep file handle.
    write_py : bool, optional
        Flag for whether to write the dep file to an analogous python file.
    make_mats : bool, optional
        Flag for whether or not to build Materials out of mass data and add
        these to the return dictionary.  Materials so added have names which
        end in '_MATERIAL'.

    Returns
    -------
    dep : dict
        Dictionary of the parsed depletion information.  Please see the Serpent
        manual for a complete description of contents.

    """
    if isinstance(depfile, basestring):
        with open(depfile, 'r') as mfile:
            f = mfile.read()
    else:
        f = depfile.read()

    # Keep comments around
    f = _replace_comments(f)

    # Replace matlab Arrays
    f = _replace_arrays(f)

    # Now to find and convert arrays that have comments in them
    comment_arrays = re.findall("(" + _comment_array_pattern + ")", f)
    for ca in comment_arrays:
        new_ca = ca[0]
        comment_lines = re.findall("(" + _comment_line_pattern + ")", ca[1])
        for cl in comment_lines:
            new_cl = re.sub(_num_pattern, lambda mo: mo.group(0) + ',', cl[1])
            if new_cl[0] == '\n':
                new_cl = "\n    [" + new_cl.strip() + "], "
            else:
                new_cl = "    [" + new_cl.strip() + "], "

            new_ca = new_ca.replace(cl[1], new_cl)

        new_ca = 'np.array( ' + new_ca + ' )'
        f = f.replace(ca[0], new_ca)

    # Indent close of array
    f = f.replace("\n] )", "\n    ] )")

    # Replace MatLab zeros with numpy zeros
    f = re.sub(_zeros_pattern, lambda mo: "np.zeros((" + mo.group(2) + "))", f)

    # Replace some math operators
    f = f.replace('.*', "*")
    f = f.replace('./', "/")

    # Remove semicolons
    f = _replace_semicolons(f)

    # Add imports to header
    header = "import numpy as np\n"
    if make_mats:
        header += "from pyne.material import Material\n"
    header += "\n"

    # Add materials
    footer = ""
    if make_mats:
        mat_gen_line = "{name}MATERIAL = [{name}VOLUME * Material(dict(zip(zai[:-2], {name}MDENS[:-2, col]))) for col in cols]\n"
        footer += "\n\n# Construct materials\nzai = map(int, ZAI)\ncols = range(len(DAYS))\n"
        base_names = re.findall('(MAT_\w*_)MDENS = ', f)
        for base_name in base_names:
            footer += mat_gen_line.format(name=base_name)
        footer += "TOT_MATERIAL = [Material(dict(zip(zai[:-2], TOT_MASS[:-2, col]))) for col in cols]\n"
        footer += "del zai, cols\n"

    # Add header & footer to file
    f = header + f + footer

    # Write the file out
    if write_py:
        if isinstance(depfile, basestring):
            new_filename = depfile.rpartition('.')[0] + '.py'
        else:
            new_filename = depfile.name.rpartition('.')[0] + '.py'
        with open(new_filename, 'w') as pyfile:
            pyfile.write(f)

    # Execute the adjusted file
    dep = {}
    exec(f, {}, dep)

    return dep

def parse_det(detfile, write_py=False):
    """Converts a serpent detector ``*_det.m`` output file to a dictionary (and
    optionally to a ``*_det.py`` file).

    Parameters
    ----------
    detfile : str or file-like object
        Path to detector file or a det file handle.
    write_py : bool, optional
        Flag for whether to write the det file to an analogous python file.

    Returns
    -------
    det : dict
        Dictionary of the parsed detector.  Please see the Serpent manual for
        a complete description of contents.

    """
    if isinstance(detfile, basestring):
        with open(detfile, 'r') as mfile:
            f = mfile.read()
    else:
        f = detfile.read()

    # Keep comments around
    f = _replace_comments(f)

    # Replace matlab Arrays
    f = _replace_arrays(f)

    # Find detector variable names
    det_names = re.findall(_detector_pattern, f)
    det_names = np.unique(det_names)

    # Append detector reshaping
    f += "\n\n# Reshape detectors\n"
    for dn in det_names:
        if dn + "E" in det_names:
            f += "{name}.shape = ({name}_VALS, 13)\n".format(name=dn)
        else:
            f += "{name}.shape = ({name_min_E}_EBINS, 3)\n".format(name=dn, name_min_E=dn[:-1])

    # Add imports to header
    header = "import numpy as np\n\n"

    # Add header to file
    f = header + f

    # Remove semicolons
    f = _replace_semicolons(f)

    # Write the file out
    if write_py:
        if isinstance(detfile, basestring):
            new_filename = detfile.rpartition('.')[0] + '.py'
        else:
            new_filename = detfile.name.rpartition('.')[0] + '.py'
        with open(new_filename, 'w') as pyfile:
            pyfile.write(f)

    # Execute the adjusted file
    det = {}
    exec(f, {}, det)

    return det
<|endoftext|>


<commit_before>from satchless.item import InsufficientStock, StockedItem
from datetime import date

from django.utils import timezone
from django.db import models
from django_prices.models import PriceField
from django.core.exceptions import ValidationError

from registration.models import Tournament

class Product(models.Model, StockedItem):
    """An abstract class that embodies everything that can be sold."""
    price = PriceField('Price',
                       currency='EUR',
                       max_digits=5,
                       decimal_places=2,
                       blank=False,
                       default=0.0)
    stock = models.PositiveSmallIntegerField('Product Stock',
                                             blank=False,
                                             default=0)
    date_added = models.DateField('Date added')
    last_modified = models.DateTimeField('Last modified')

    def get_price_per_item(self):
        return price

    class Meta:
        abstract = True

class TournamentProductUtilitiesManager(models.Manager):
    def create_tournament_product(self, tournament, price=0.0, stock=0):
        tourney_product = TournamentProduct(tournament=tournament,
                                            price=price,
                                            stock=stock,
                                            date_added=date.today(),
                                            last_modified=timezone.now())
        tourney_product.save()
        return tourney_product

    def update_tournament_product(self, product_id, **kwargs):
        additional_attributes = {'price', 'stock'}
        tourney_product = TournamentProduct.objects.get(pk=product_id)
        for attribute, value in kwargs.items():
            assert attribute in additional_attributes
            setattr(tourney_product, attribute, value)
        tourney_product.save()
        return tourney_product

    def delete_tournament_product(self, product_id):
        tourney_product = TournamentProduct.objects.get(pk=product_id)
        tourney_product.delete()

class TournamentProduct(Product):
    tournament = models.OneToOneField(Tournament)
    objects = models.Manager()
    utilities = TournamentProductUtilitiesManager()

    def get_stock(self):
        return stock

    def save(self, *args, **kwargs):
        " Override the save method to check the stock "
        if self.stock > self.tournament.get_available_spots():
            msg = 'Stock of a TournamentProduct cannot be greater than the \
                tournament available spots'
            raise ValidationError(msg)
        super(TournamentProduct, self).save(*args, **kwargs)
<commit_msg>corrected small stock related mistake<commit_after>from satchless.item import InsufficientStock, StockedItem
from datetime import date

from django.utils import timezone
from django.db import models
from django_prices.models import PriceField
from django.core.exceptions import ValidationError

from registration.models import Tournament

class Product(models.Model, StockedItem):
    """An abstract class that embodies everything that can be sold."""
    price = PriceField('Price',
                       currency='EUR',
                       max_digits=5,
                       decimal_places=2,
                       blank=False,
                       default=0.0)
    stock = models.PositiveSmallIntegerField('Product Stock',
                                             blank=False,
                                             default=0)
    date_added = models.DateField('Date added')
    last_modified = models.DateTimeField('Last modified')

    def get_price_per_item(self):
        return price

    class Meta:
        abstract = True

class TournamentProductUtilitiesManager(models.Manager):
    def create_tournament_product(self, tournament, price=0.0, stock=0):
        tourney_product = TournamentProduct(tournament=tournament,
                                            price=price,
                                            stock=stock,
                                            date_added=date.today(),
                                            last_modified=timezone.now())
        tourney_product.save()
        return tourney_product

    def update_tournament_product(self, product_id, **kwargs):
        additional_attributes = {'price', 'stock'}
        tourney_product = TournamentProduct.objects.get(pk=product_id)
        for attribute, value in kwargs.items():
            assert attribute in additional_attributes
            setattr(tourney_product, attribute, value)
        tourney_product.save()
        return tourney_product

    def delete_tournament_product(self, product_id):
        tourney_product = TournamentProduct.objects.get(pk=product_id)
        tourney_product.delete()

class TournamentProduct(Product):
    tournament = models.OneToOneField(Tournament)
    objects = models.Manager()
    utilities = TournamentProductUtilitiesManager()

    def get_stock(self):
        return self.stock

    def save(self, *args, **kwargs):
        " Override the save method to check the stock "
        if self.stock > self.tournament.get_available_spots():
            msg = 'Stock of a TournamentProduct cannot be greater than the \
                tournament available spots'
            raise ValidationError(msg)
        super(TournamentProduct, self).save(*args, **kwargs)
<|endoftext|>


<commit_before># Rosie Aboody
# Joey Velez-Ginorio
# Julian Jara-Ettinger
# Curse of Knowledge Project
# -----------------------------------------------------------------------------

import itertools
import numpy as np

class GenerateHypothesisSpace():
	"""
		Class which holds several hypothesis space generator functions.

	"""

	def __init__(self, blockList):
		self.blockList = blockList
		self.unorderedArgs = self.unorderedArgs(self.blockList)
		self.orderedArgs = self.orderedArgs(self.blockList)

	def simpleDepthSampler(self, depth, uniform):
		"""
			Samples AND, OR hypotheses at several depths.
		"""
		x = lambda x: ''.join(x)	# Beautiful function to make hypotheses pretty

		hypotheses = []
		args = []

		for i in range(1,depth+1):
			args = itertools.chain(args, map(x,itertools.combinations('ABCDE',i)))

		args = [[i] for i in args]

		args = list(args)

		y = lambda y: self.Or(*y)

		for i in range(1, depth+1):
			hypotheses = itertools.chain(hypotheses, map(y,
												itertools.combinations('ABCDE',i)))

		hypotheses = args + list(hypotheses)

		if uniform:
			prior = list()
			prior = [1.0/len(hypotheses) for i in hypotheses]

		else:
			prior = list()
			for h in hypotheses:
				prior.append(1.0/self.priorHelp(h))
			normal = sum(prior)
			prior = [i/normal for i in prior]

		return [hypotheses, prior, [''.join(i) for i in self.unorderedArgs]]

	def depthSampler(self, depth,uniform=True):
		"""
			Samples AND, OR hypotheses at several depths.

		"""
		x = lambda x: ''.join(x)	# Beautiful function to make hypotheses pretty

		hypotheses = []
		args = []

		for i in range(1,depth+1):
			args = itertools.chain(args, itertools.imap(x,itertools.combinations('ABCDE',i)))

		args = list(args)

		y = lambda y: self.Or(*y)

		for i in range(1, depth+1):
			hypotheses = itertools.chain(hypotheses, itertools.imap(y,
											itertools.combinations(args,i)))

		hypotheses = list(hypotheses)
		if uniform:
			prior = list()
			prior = [1.0/len(hypotheses) for i in hypotheses]

		else:
			prior = list()
			for h in hypotheses:
				prior.append(1.0/self.priorHelp(h))
			normal = sum(prior)
			prior = [i/(5+normal) for i in prior]

		return [hypotheses, prior, [''.join(i) for i in self.unorderedArgs]]

	"""
		Same thing as depthSampler, except you first specify how many samples
		you want from the hypothesis space depthSampler gives.
	"""
	def random_depth_sampler(self, samples, depth, uniform=True, th = ['BE']):

		temp = self.depthSampler(depth,uniform)
		hyps = temp[0]
		arg = temp[2]

		if len(hyps) < samples:
			print 'Desired sample size is larger than total hypothesis space, choose larger depth'
			return None

		final_hyps = list()
		final_hyps.append(th)

		for i in range(samples-1):
			ind = np.random.choice(len(hyps))
			while hyps[ind] == th:
				ind = np.random.choice(len(hyps))

			temp = hyps.pop(ind)
			final_hyps.append(temp)

		if uniform:
			prior = list()
			prior = [1.0/len(final_hyps) for i in final_hyps]

		np.random.shuffle(final_hyps)

		return [final_hyps, prior, arg]

	def random_teacher(self, num_examples):

		temp = self.depthSampler(1,uniform=True)
		examples = temp[2]

		return list(np.random.choice(examples,size=num_examples,replace=False))

	def permute_teacher(self, teacherData):

		final_teacher_data = list()

		if len(teacherData) < 8:
			final_teacher_data = itertools.permutations(teacherData)
			final_teacher_data = [list(i) for i in final_teacher_data]

		else:
			temp = list(itertools.permutations(teacherData))
			idx = np.random.choice(len(temp), 10000, replace=False)
			for i in idx:
				final_teacher_data.append(temp[i])

			final_teacher_data = [list(i) for i in final_teacher_data]

		return final_teacher_data

	def priorHelp(self, hypothesis):
		total = 0
		for h in hypothesis:
			total += len(h)

		return total

	def unorderedArgs(self, blockList):
		"""
			Generates a list of arguments for the unordered set of hypothesis
			generators. Takes a blockList, and generates every combination.

			Param:
				blockList - a list of characters
				uniform - boolean, if true, will set prior to uniform distribution
		"""

		# Initialize empty action space
		args = list()
		args += blockList

		# Generate every possible combination of arguments from blockList
		for i in range(2, len(blockList)+1):
			for arg in itertools.combinations(blockList, i):
				args.append(list(arg))

		return args

	def orderedArgs(self, blockList):
		"""
			Generates a list of arguments for the unordered set of hypothesis
			generators. Takes a blockList, and generates every combination.

			Param:
				blockList - a list of characters
		"""

		# Initialize empty action space
		args = list()
		args += blockList

		# Generate every possible combination of arguments from blockList
		for i in range(2, len(blockList)+1):
			for arg in itertools.permutations(blockList, i):
				args.append(list(arg))

		return args

	def unorderedOr(self, uniform=True):
		"""
			Hypothesis Space #1:
			Generates a list of hypotheses, including all combinations of
			Or logic rules.

			Param:
				uniform - if true, prior for H is uniform, else t.b.d

		"""

		# Initializes hypothesis space and prior
		hypothesisSpace = list()
		hypothesisSpacePrior = list()

		# Add the single-block hypotheses to hypothesis space
		hypothesisSpace += [[i] for i in self.unorderedArgs[0:len(self.blockList)]]

		# Remove the single-block hypotheses from arguments
		args = self.unorderedArgs[len(self.blockList):]

		# Use args as arguments for Or(), add to hyp. space
		for arg in args:
			hypothesisSpace.append(self.Or(*arg))

		if uniform:
			# Calculate prior distribution of hypothesis space
			hypothesisSpacePrior = [1.0/len(self.unorderedArgs) for i in self.unorderedArgs]

		return [hypothesisSpace, hypothesisSpacePrior, [''.join(i) for i in self.unorderedArgs]]

	def unorderedAnd(self, uniform=True):
		"""
			Hypothesis Space #1:
			Generates a list of hypotheses, including all combinations of
			And logic rules.

			Param:
				uniform - if true, prior for H is uniform, else t.b.d

		"""

		# Initializes hypothesis space and prior
		hypothesisSpace = list()
		hypothesisSpacePrior = list()

		# Add the single-block hypotheses to hypothesis space
		hypothesisSpace += [[i] for i in self.unorderedArgs[0:len(self.blockList)]]

		# Remove the single-block hypotheses from arguments
		args = self.unorderedArgs[len(self.blockList):]

		# Use args as arguments for Or(), add to hyp. space
		for arg in args:
			hypothesisSpace.append(self.And(*arg))

		if uniform:
			# Calculate prior distribution of hypothesis space
			hypothesisSpacePrior = [1.0/len(self.unorderedArgs) for i in self.unorderedArgs]

		return [hypothesisSpace, hypothesisSpacePrior, [''.join(i) for i in self.unorderedArgs]]

	def unorderedAndDepth(self, depth, uniform=True):
		"""
			Hypothesis Space #1:
			Generates a list of hypotheses, including all combinations of
			And logic rules.
			Param:
				uniform - if true, prior for H is uniform, else t.b.d
		"""

		# Initializes hypothesis space and prior
		hypothesisSpace = list()
		hypothesisSpacePrior = list()

		# Add the single-block hypotheses to hypothesis space
		hypothesisSpace += [[i] for i in self.unorderedArgs[0:len(self.blockList)]]

		# Remove the single-block hypotheses from arguments
		args = self.unorderedArgs[len(self.blockList):]
		args = [i for i in args if len(i) <= depth]

		# Use args as arguments for Or(), add to hyp. space
		for arg in args:
			hypothesisSpace.append(self.And(*arg))

		if uniform:
			# Calculate prior distribution of hypothesis space
			hypothesisSpacePrior = [1.0/len(hypothesisSpace) for i in hypothesisSpace]

		else:
			for h in hypothesisSpace:
				hypothesisSpacePrior.append(1.0/self.priorHelp(h))
				normal = sum(hypothesisSpacePrior)
				hypothesisSpacePrior = [i/normal for i in hypothesisSpacePrior]

		return [hypothesisSpace, hypothesisSpacePrior, [''.join(i) for i in self.unorderedArgs]]

	def unorderedAndOr(self, uniform = True):
		"""
			Hypothesis Space # 3:
			Generates a list of hypotheses, including all combinations of
			And and Or logic rules.

			Param:
				uniform - if ture, prior for H is uniform, else t.b.d.
		"""

		# Initializes hypothesis space and prior
		hypothesisSpace = list()
		hypothesisSpacePrior = list()

		# Add hypotheses to the hypothesis space
		hypothesisSpace += [[i] for i in self.unorderedArgs[0:len(self.blockList)]]

		# Remove the single-block hypotheses from arguments so they don't get
		# needlessly duplicated.
		args = self.unorderedArgs[len(self.blockList):]

		# Use args as arguments for And() as well as Or(), add to hypothesis space
		for arg in args:
			hypothesisSpace.append(self.And(*arg))
			hypothesisSpace.append(self.Or(*arg))

		if uniform:
			# Calculate prior distribution of hypothesis space
			hypothesisSpacePrior = [1.0/len(hypothesisSpace) for i in hypothesisSpace]

		# hypothesisSpace = all possible And & Or hypotheses
		# hypothesisSpacePrior = either uniform, or simplicity biased (tba)
		# the last statement makes up the actionSpace, by taking all the combinations
		# we made in unorderedArgs and simply taking away all the spaces etc.
		return [hypothesisSpace, hypothesisSpacePrior, [''.join(i) for i in self.unorderedArgs]]

	def orderedAnd(self, uniform = True):

		"""
			Hypothesis space #4
			Generates a list of ordered hypotheses, including all combinations
			of the And logical operator.
		"""
		# Initializes the hypothesis space and prior
		hypothesisSpace, hypothesisSpacePrior = list(), list()

		# Add hypotheses to the hypothesis space:
		hypothesisSpace += [[i] for i in self.orderedArgs[0:len(self.blockList)]]

		# Remove the single-block hypotheses from the list of arguments
		args = self.orderedArgs[len(self.blockList):]

		# use the args as arguments for the And() function & add to hypothesisSpace
		for arg in args:
			hypothesisSpace.append(self.And(*arg))

		if uniform:
			# Calculate prior
			hypothesisSpacePrior = [1.0/len(self.orderedArgs) for i in self.orderedArgs]

		return [hypothesisSpace, hypothesisSpacePrior, [''.join(i) for i in self.unorderedArgs]]

	def orderedAndOr(self, uniform = True):

		"""
			Hypothesis space # 5:
			Generates a lsit of ordered hypotheses, including all combinations of
			the And && Or logical operators
		"""

		# Initializes the hypothesis space & prior
		hypothesisSpace, hypothesisSpacePrior = list(), list()

		# Add hypotheses to the hypothesis space:
		hypothesisSpace += [[i] for i in self.orderedArgs[0:len(self.blockList)]]

		# remove the single-block hypotheses from the list of arguments
		args = self.orderedArgs[len(self.blockList):]
		args2 = self.unorderedArgs[len(self.blockList):]

		# use the args as arguments for the And() and Or() functions and add to hyopthesisSpace
		for arg in args:
			hypothesisSpace.append(self.And(*arg))
		for arg in args2:
			hypothesisSpace.append(self.Or(*arg))

		if uniform:
			# calculate prior
			hypothesisSpacePrior = [1.0/len(hypothesisSpace) for i in hypothesisSpace]

		return [hypothesisSpace, hypothesisSpacePrior, [''.join(i) for i in self.unorderedArgs]]

	def Or(self, *args):
		"""
			Logical Or, e.g. Or('A','B') = ['A','B']
			Can handle 2 or more arguments

			Param:
				*args - May accept any argument, but
				for the model, block characters generally used

		"""

		# Our return list
		temp = list()

		# Sift through arguments to add to final list
		for arg in args:

			# If argument is a tuple, convert to list then add
			if type(arg) is tuple:
				temp.append(list(arg))

			# Standard character, add as usual
			else:
				temp.append(arg)

		return temp

	# def And(self, *args):

	# 	"""
	# 		Logical And, e.g. And('A','B') -> ['AB','BA']
	# 		Can handle 2 or more arguments.

	# 		Param:
	# 			*args - May accept any argument, but
	# 			for the model, block characters generally used.

	# 	"""

	# 	args = list(args)

	# 	# Convert arguments to list if not already
	# 	for i in range(len(args)):
 # 			if type(args[i]) is not list:
 # 				args[i] = list([args[i]])

 # 		# Initialize final list
	# 	final = list()

	# 	# Generate all permutations of arguments
	# 	temp = list(itertools.permutations(args))

	# 	# Compute all products within each permutation
	# 	for arg in temp:
	# 		final.append(list([''.join(s) for s in list(itertools.product(*arg))]))

	# 	return [''.join(i) for i in final]

   	def And(self,*args):

 		args = list(args)

 		for i in range(len(args)):

 			if type(args[i]) is not list:
 				args[i] = list([args[i]])

		return [''.join(s) for s in list(itertools.product(*args))]<commit_msg>fixed permute teacher to work well for examples greater than 8<commit_after># Rosie Aboody
# Joey Velez-Ginorio
# Julian Jara-Ettinger
# Curse of Knowledge Project
# -----------------------------------------------------------------------------

import itertools
import numpy as np

class GenerateHypothesisSpace():
	"""
		Class which holds several hypothesis space generator functions.

	"""

	def __init__(self, blockList):
		self.blockList = blockList
		self.unorderedArgs = self.unorderedArgs(self.blockList)
		self.orderedArgs = self.orderedArgs(self.blockList)

	def simpleDepthSampler(self, depth, uniform):
		"""
			Samples AND, OR hypotheses at several depths.
		"""
		x = lambda x: ''.join(x)	# Beautiful function to make hypotheses pretty

		hypotheses = []
		args = []

		for i in range(1,depth+1):
			args = itertools.chain(args, map(x,itertools.combinations('ABCDE',i)))

		args = [[i] for i in args]

		args = list(args)

		y = lambda y: self.Or(*y)

		for i in range(1, depth+1):
			hypotheses = itertools.chain(hypotheses, map(y,
												itertools.combinations('ABCDE',i)))

		hypotheses = args + list(hypotheses)

		if uniform:
			prior = list()
			prior = [1.0/len(hypotheses) for i in hypotheses]

		else:
			prior = list()
			for h in hypotheses:
				prior.append(1.0/self.priorHelp(h))
			normal = sum(prior)
			prior = [i/normal for i in prior]

		return [hypotheses, prior, [''.join(i) for i in self.unorderedArgs]]

	def depthSampler(self, depth,uniform=True):
		"""
			Samples AND, OR hypotheses at several depths.

		"""
		x = lambda x: ''.join(x)	# Beautiful function to make hypotheses pretty

		hypotheses = []
		args = []

		for i in range(1,depth+1):
			args = itertools.chain(args, itertools.imap(x,itertools.combinations('ABCDE',i)))

		args = list(args)

		y = lambda y: self.Or(*y)

		for i in range(1, depth+1):
			hypotheses = itertools.chain(hypotheses, itertools.imap(y,
											itertools.combinations(args,i)))

		hypotheses = list(hypotheses)
		if uniform:
			prior = list()
			prior = [1.0/len(hypotheses) for i in hypotheses]

		else:
			prior = list()
			for h in hypotheses:
				prior.append(1.0/self.priorHelp(h))
			normal = sum(prior)
			prior = [i/(5+normal) for i in prior]

		return [hypotheses, prior, [''.join(i) for i in self.unorderedArgs]]

	"""
		Same thing as depthSampler, except you first specify how many samples
		you want from the hypothesis space depthSampler gives.
	"""
	def random_depth_sampler(self, samples, depth, uniform=True, th = ['BE']):

		temp = self.depthSampler(depth,uniform)
		hyps = temp[0]
		arg = temp[2]

		if len(hyps) < samples:
			print 'Desired sample size is larger than total hypothesis space, choose larger depth'
			return None

		final_hyps = list()
		final_hyps.append(th)

		for i in range(samples-1):
			ind = np.random.choice(len(hyps))
			while hyps[ind] == th:
				ind = np.random.choice(len(hyps))

			temp = hyps.pop(ind)
			final_hyps.append(temp)

		if uniform:
			prior = list()
			prior = [1.0/len(final_hyps) for i in final_hyps]

		np.random.shuffle(final_hyps)

		return [final_hyps, prior, arg]

	def random_teacher(self, num_examples):

		temp = self.depthSampler(1,uniform=True)
		examples = temp[2]

		return list(np.random.choice(examples,size=num_examples,replace=False))

	def permute_teacher(self, teacherData):

		final_teacher_data = list()

		if len(teacherData) < 8:
			final_teacher_data = itertools.permutations(teacherData)
			final_teacher_data = [list(i) for i in final_teacher_data]

		else:
			temp = teacherData
			perms = set()

			while len(perms) < 10000:
				np.random.shuffle(temp)
				perms.add(tuple(temp))

			final_teacher_data = list(perms)
			final_teacher_data = [list(i) for i in final_teacher_data]

		return final_teacher_data

	def priorHelp(self, hypothesis):
		total = 0
		for h in hypothesis:
			total += len(h)

		return total

	def unorderedArgs(self, blockList):
		"""
			Generates a list of arguments for the unordered set of hypothesis
			generators. Takes a blockList, and generates every combination.

			Param:
				blockList - a list of characters
				uniform - boolean, if true, will set prior to uniform distribution
		"""

		# Initialize empty action space
		args = list()
		args += blockList

		# Generate every possible combination of arguments from blockList
		for i in range(2, len(blockList)+1):
			for arg in itertools.combinations(blockList, i):
				args.append(list(arg))

		return args

	def orderedArgs(self, blockList):
		"""
			Generates a list of arguments for the unordered set of hypothesis
			generators. Takes a blockList, and generates every combination.

			Param:
				blockList - a list of characters
		"""

		# Initialize empty action space
		args = list()
		args += blockList

		# Generate every possible combination of arguments from blockList
		for i in range(2, len(blockList)+1):
			for arg in itertools.permutations(blockList, i):
				args.append(list(arg))

		return args

	def unorderedOr(self, uniform=True):
		"""
			Hypothesis Space #1:
			Generates a list of hypotheses, including all combinations of
			Or logic rules.

			Param:
				uniform - if true, prior for H is uniform, else t.b.d

		"""

		# Initializes hypothesis space and prior
		hypothesisSpace = list()
		hypothesisSpacePrior = list()

		# Add the single-block hypotheses to hypothesis space
		hypothesisSpace += [[i] for i in self.unorderedArgs[0:len(self.blockList)]]

		# Remove the single-block hypotheses from arguments
		args = self.unorderedArgs[len(self.blockList):]

		# Use args as arguments for Or(), add to hyp. space
		for arg in args:
			hypothesisSpace.append(self.Or(*arg))

		if uniform:
			# Calculate prior distribution of hypothesis space
			hypothesisSpacePrior = [1.0/len(self.unorderedArgs) for i in self.unorderedArgs]

		return [hypothesisSpace, hypothesisSpacePrior, [''.join(i) for i in self.unorderedArgs]]

	def unorderedAnd(self, uniform=True):
		"""
			Hypothesis Space #1:
			Generates a list of hypotheses, including all combinations of
			And logic rules.

			Param:
				uniform - if true, prior for H is uniform, else t.b.d

		"""

		# Initializes hypothesis space and prior
		hypothesisSpace = list()
		hypothesisSpacePrior = list()

		# Add the single-block hypotheses to hypothesis space
		hypothesisSpace += [[i] for i in self.unorderedArgs[0:len(self.blockList)]]

		# Remove the single-block hypotheses from arguments
		args = self.unorderedArgs[len(self.blockList):]

		# Use args as arguments for Or(), add to hyp. space
		for arg in args:
			hypothesisSpace.append(self.And(*arg))

		if uniform:
			# Calculate prior distribution of hypothesis space
			hypothesisSpacePrior = [1.0/len(self.unorderedArgs) for i in self.unorderedArgs]

		return [hypothesisSpace, hypothesisSpacePrior, [''.join(i) for i in self.unorderedArgs]]

	def unorderedAndDepth(self, depth, uniform=True):
		"""
			Hypothesis Space #1:
			Generates a list of hypotheses, including all combinations of
			And logic rules.
			Param:
				uniform - if true, prior for H is uniform, else t.b.d
		"""

		# Initializes hypothesis space and prior
		hypothesisSpace = list()
		hypothesisSpacePrior = list()

		# Add the single-block hypotheses to hypothesis space
		hypothesisSpace += [[i] for i in self.unorderedArgs[0:len(self.blockList)]]

		# Remove the single-block hypotheses from arguments
		args = self.unorderedArgs[len(self.blockList):]
		args = [i for i in args if len(i) <= depth]

		# Use args as arguments for Or(), add to hyp. space
		for arg in args:
			hypothesisSpace.append(self.And(*arg))

		if uniform:
			# Calculate prior distribution of hypothesis space
			hypothesisSpacePrior = [1.0/len(hypothesisSpace) for i in hypothesisSpace]

		else:
			for h in hypothesisSpace:
				hypothesisSpacePrior.append(1.0/self.priorHelp(h))
				normal = sum(hypothesisSpacePrior)
				hypothesisSpacePrior = [i/normal for i in hypothesisSpacePrior]

		return [hypothesisSpace, hypothesisSpacePrior, [''.join(i) for i in self.unorderedArgs]]

	def unorderedAndOr(self, uniform = True):
		"""
			Hypothesis Space # 3:
			Generates a list of hypotheses, including all combinations of
			And and Or logic rules.

			Param:
				uniform - if ture, prior for H is uniform, else t.b.d.
		"""

		# Initializes hypothesis space and prior
		hypothesisSpace = list()
		hypothesisSpacePrior = list()

		# Add hypotheses to the hypothesis space
		hypothesisSpace += [[i] for i in self.unorderedArgs[0:len(self.blockList)]]

		# Remove the single-block hypotheses from arguments so they don't get
		# needlessly duplicated.
		args = self.unorderedArgs[len(self.blockList):]

		# Use args as arguments for And() as well as Or(), add to hypothesis space
		for arg in args:
			hypothesisSpace.append(self.And(*arg))
			hypothesisSpace.append(self.Or(*arg))

		if uniform:
			# Calculate prior distribution of hypothesis space
			hypothesisSpacePrior = [1.0/len(hypothesisSpace) for i in hypothesisSpace]

		# hypothesisSpace = all possible And & Or hypotheses
		# hypothesisSpacePrior = either uniform, or simplicity biased (tba)
		# the last statement makes up the actionSpace, by taking all the combinations
		# we made in unorderedArgs and simply taking away all the spaces etc.
		return [hypothesisSpace, hypothesisSpacePrior, [''.join(i) for i in self.unorderedArgs]]

	def orderedAnd(self, uniform = True):

		"""
			Hypothesis space #4
			Generates a list of ordered hypotheses, including all combinations
			of the And logical operator.
		"""
		# Initializes the hypothesis space and prior
		hypothesisSpace, hypothesisSpacePrior = list(), list()

		# Add hypotheses to the hypothesis space:
		hypothesisSpace += [[i] for i in self.orderedArgs[0:len(self.blockList)]]

		# Remove the single-block hypotheses from the list of arguments
		args = self.orderedArgs[len(self.blockList):]

		# use the args as arguments for the And() function & add to hypothesisSpace
		for arg in args:
			hypothesisSpace.append(self.And(*arg))

		if uniform:
			# Calculate prior
			hypothesisSpacePrior = [1.0/len(self.orderedArgs) for i in self.orderedArgs]

		return [hypothesisSpace, hypothesisSpacePrior, [''.join(i) for i in self.unorderedArgs]]

	def orderedAndOr(self, uniform = True):

		"""
			Hypothesis space # 5:
			Generates a lsit of ordered hypotheses, including all combinations of
			the And && Or logical operators
		"""

		# Initializes the hypothesis space & prior
		hypothesisSpace, hypothesisSpacePrior = list(), list()

		# Add hypotheses to the hypothesis space:
		hypothesisSpace += [[i] for i in self.orderedArgs[0:len(self.blockList)]]

		# remove the single-block hypotheses from the list of arguments
		args = self.orderedArgs[len(self.blockList):]
		args2 = self.unorderedArgs[len(self.blockList):]

		# use the args as arguments for the And() and Or() functions and add to hyopthesisSpace
		for arg in args:
			hypothesisSpace.append(self.And(*arg))
		for arg in args2:
			hypothesisSpace.append(self.Or(*arg))

		if uniform:
			# calculate prior
			hypothesisSpacePrior = [1.0/len(hypothesisSpace) for i in hypothesisSpace]

		return [hypothesisSpace, hypothesisSpacePrior, [''.join(i) for i in self.unorderedArgs]]

	def Or(self, *args):
		"""
			Logical Or, e.g. Or('A','B') = ['A','B']
			Can handle 2 or more arguments

			Param:
				*args - May accept any argument, but
				for the model, block characters generally used

		"""

		# Our return list
		temp = list()

		# Sift through arguments to add to final list
		for arg in args:

			# If argument is a tuple, convert to list then add
			if type(arg) is tuple:
				temp.append(list(arg))

			# Standard character, add as usual
			else:
				temp.append(arg)

		return temp

	# def And(self, *args):

	# 	"""
	# 		Logical And, e.g. And('A','B') -> ['AB','BA']
	# 		Can handle 2 or more arguments.

	# 		Param:
	# 			*args - May accept any argument, but
	# 			for the model, block characters generally used.

	# 	"""

	# 	args = list(args)

	# 	# Convert arguments to list if not already
	# 	for i in range(len(args)):
 # 			if type(args[i]) is not list:
 # 				args[i] = list([args[i]])

 # 		# Initialize final list
	# 	final = list()

	# 	# Generate all permutations of arguments
	# 	temp = list(itertools.permutations(args))

	# 	# Compute all products within each permutation
	# 	for arg in temp:
	# 		final.append(list([''.join(s) for s in list(itertools.product(*arg))]))

	# 	return [''.join(i) for i in final]

   	def And(self,*args):

 		args = list(args)

 		for i in range(len(args)):

 			if type(args[i]) is not list:
 				args[i] = list([args[i]])

		return [''.join(s) for s in list(itertools.product(*args))]<|endoftext|>


<commit_before>from hyperadmin.resources.storages.views import BoundFile
from hyperadmin.hyperobjects import Link
from hyperadmin.resources.crud.changelist import ChangeList

class StoragePaginator(object):
    #count, num_pages, object_list
    def __init__(self, index, storage, state):
        self.state = state
        self.path = state.params.get('path', '')
        self.dirs, self.files = index
        self.storage = storage

        self.count = len(self.files)
        self.num_pages = 1

        self.links, self.instances = self.get_links_and_instances()
        self.object_list = self.instances

    def get_links_and_instances(self):
        items = list()
        for file_name in self.files:
            items.append(BoundFile(self.storage, file_name))

        links = list()
        if self.path:
            url = './%s' % self.state.get_query_string({}, ['path'])
            print url
            link = Link(url=url, resource=self, prompt=u"Directory: /", classes=['filter', 'directory'], rel="filter")
            links.append(link)
        for directory in self.dirs:
            url = './%s' % self.state.get_query_string({'path':directory})
            print url
            link = Link(url=url, resource=self, prompt=u"Directory: %s" % directory, classes=['filter', 'directory'], rel="filter")
            links.append(link)
        if '/' in self.path:
            url = './%s' % self.state.get_query_string({'path':self.path[:self.path.rfind('/')]})
            print url
            link = Link(url=url, resource=self, prompt=u"Directory: ../", classes=['filter', 'directory'], rel="filter")
            links.append(link)
        return links, items

class StorageChangeList(ChangeList):
    def populate_state(self, state):
        for section in self.sections.itervalues():
            section.populate_state(state)
        index = self.get_instances(state)
        paginator = self.get_paginator(index, storage=self.resource.storage, state=state)
        state['paginator'] = paginator
        state['page'] = paginator
        state['links'] = paginator.links

<commit_msg>removed unused print statements<commit_after>from hyperadmin.resources.storages.views import BoundFile
from hyperadmin.hyperobjects import Link
from hyperadmin.resources.crud.changelist import ChangeList

class StoragePaginator(object):
    #count, num_pages, object_list
    def __init__(self, index, storage, state):
        self.state = state
        self.path = state.params.get('path', '')
        self.dirs, self.files = index
        self.storage = storage

        self.count = len(self.files)
        self.num_pages = 1

        self.links, self.instances = self.get_links_and_instances()
        self.object_list = self.instances

    def get_links_and_instances(self):
        items = list()
        for file_name in self.files:
            items.append(BoundFile(self.storage, file_name))

        links = list()
        if self.path:
            url = './%s' % self.state.get_query_string({}, ['path'])
            link = Link(url=url, resource=self, prompt=u"Directory: /", classes=['filter', 'directory'], rel="filter")
            links.append(link)
        for directory in self.dirs:
            url = './%s' % self.state.get_query_string({'path':directory})
            link = Link(url=url, resource=self, prompt=u"Directory: %s" % directory, classes=['filter', 'directory'], rel="filter")
            links.append(link)
        if '/' in self.path:
            url = './%s' % self.state.get_query_string({'path':self.path[:self.path.rfind('/')]})
            link = Link(url=url, resource=self, prompt=u"Directory: ../", classes=['filter', 'directory'], rel="filter")
            links.append(link)
        return links, items

class StorageChangeList(ChangeList):
    def populate_state(self, state):
        for section in self.sections.itervalues():
            section.populate_state(state)
        index = self.get_instances(state)
        paginator = self.get_paginator(index, storage=self.resource.storage, state=state)
        state['paginator'] = paginator
        state['page'] = paginator
        state['links'] = paginator.links

<|endoftext|>


<commit_before>from __future__ import absolute_import, division, print_function

import datashape
from datashape import Record, DataShape, dshape, TimeDelta, Decimal, Option
from datashape import coretypes as ct
from datashape.predicates import iscollection, isboolean, isnumeric, isdatelike
from numpy import inf
from odo.utils import copydoc
import toolz

from .core import common_subexpression
from .expressions import Expr, ndim
from .strings import isstring
from .expressions import dshape_method_list, method_properties

class Reduction(Expr):

    """ A column-wise reduction

    Blaze supports the same class of reductions as NumPy and Pandas.

        sum, min, max, any, all, mean, var, std, count, nunique

    Examples
    --------

    >>> from blaze import symbol
    >>> t = symbol('t', 'var * {name: string, amount: int, id: int}')
    >>> e = t['amount'].sum()

    >>> data = [['Alice', 100, 1],
    ...         ['Bob', 200, 2],
    ...         ['Alice', 50, 3]]

    >>> from blaze.compute.python import compute
    >>> compute(e, data)
    350
    """
    __slots__ = '_hash', '_child', 'axis', 'keepdims'

    def __init__(self, _child, axis=None, keepdims=False):
        self._child = _child
        if axis is None:
            axis = tuple(range(_child.ndim))
        if isinstance(axis, (set, list)):
            axis = tuple(axis)
        if not isinstance(axis, tuple):
            axis = (axis,)
        axis = tuple(sorted(axis))
        self.axis = axis
        self.keepdims = keepdims
        self._hash = None

    def _dshape(self):
        axis = self.axis
        if self.keepdims:
            shape = tuple(1 if i in axis else d
                          for i, d in enumerate(self._child.shape))
        else:
            shape = tuple(d
                          for i, d in enumerate(self._child.shape)
                          if i not in axis)
        return DataShape(*(shape + (self.schema,)))

    def _schema(self):
        schema = self._child.schema[0]
        if isinstance(schema, Record) and len(schema.types) == 1:
            result = toolz.first(schema.types)
        else:
            result = schema
        return DataShape(result)

    @property
    def symbol(self):
        return type(self).__name__

    @property
    def _name(self):
        child_name = self._child._name
        if child_name is None or child_name == '_':
            return type(self).__name__
        else:
            return '%s_%s' % (child_name, type(self).__name__)

    def __str__(self):
        kwargs = list()
        if self.keepdims:
            kwargs.append('keepdims=True')
        if self.axis != tuple(range(self._child.ndim)):
            kwargs.append('axis=' + str(self.axis))
        other = sorted(
            set(self.__slots__[1:]) - set(['_child', 'axis', 'keepdims']))
        for slot in other:
            kwargs.append('%s=%s' % (slot, getattr(self, slot)))
        name = type(self).__name__
        if kwargs:
            return '%s(%s, %s)' % (name, self._child, ', '.join(kwargs))
        else:
            return '%s(%s)' % (name, self._child)

class any(Reduction):
    schema = dshape(ct.bool_)

class all(Reduction):
    schema = dshape(ct.bool_)

class sum(Reduction):
    def _schema(self):
        return DataShape(datashape.maxtype(super(sum, self)._schema()))

class max(Reduction):
    pass

class min(Reduction):
    pass

class mean(Reduction):
    def _schema(self):
        measure = self._child.schema.measure
        base = getattr(measure, 'ty', measure)
        return_type = Option if isinstance(measure, Option) else toolz.identity
        return DataShape(return_type(
            base if isinstance(base, Decimal) else ct.float64
        ))

class var(Reduction):

    """Variance

    Parameters
    ----------
    child : Expr
        An expression
    unbiased : bool, optional
        Compute an unbiased estimate of the population variance if this is
        ``True``. In NumPy and pandas, this parameter is called ``ddof`` (delta
        degrees of freedom) and is equal to 1 for unbiased and 0 for biased.
    """
    __slots__ = '_hash', '_child', 'unbiased', 'axis', 'keepdims'

    schema = dshape(ct.real)

    def __init__(self, child, unbiased=False, *args, **kwargs):
        self.unbiased = unbiased
        super(var, self).__init__(child, *args, **kwargs)

class std(Reduction):

    """Standard Deviation

    Parameters
    ----------
    child : Expr
        An expression
    unbiased : bool, optional
        Compute the square root of an unbiased estimate of the population
        variance if this is ``True``.

        .. warning::

            This does *not* return an unbiased estimate of the population
            standard deviation.

    See Also
    --------
    var
    """
    __slots__ = '_hash', '_child', 'unbiased', 'axis', 'keepdims'

    schema = dshape(ct.real)

    def __init__(self, child, unbiased=False, *args, **kwargs):
        self.unbiased = unbiased
        super(std, self).__init__(child, *args, **kwargs)

class count(Reduction):

    """ The number of non-null elements """
    schema = dshape(ct.int32)

class nunique(Reduction):
    schema = dshape(ct.int32)

class nelements(Reduction):

    """Compute the number of elements in a collection, including missing values.

    See Also
    ---------
    blaze.expr.reductions.count: compute the number of non-null elements

    Examples
    --------
    >>> from blaze import symbol
    >>> t = symbol('t', 'var * {name: string, amount: float64}')
    >>> t[t.amount < 1].nelements()
    nelements(t[t.amount < 1])
    """
    schema = dshape(ct.int32)

def nrows(expr):
    return nelements(expr, axis=(0,))

class Summary(Expr):

    """ A collection of named reductions

    Examples
    --------

    >>> from blaze import symbol
    >>> t = symbol('t', 'var * {name: string, amount: int, id: int}')
    >>> expr = summary(number=t.id.nunique(), sum=t.amount.sum())

    >>> data = [['Alice', 100, 1],
    ...         ['Bob', 200, 2],
    ...         ['Alice', 50, 1]]

    >>> from blaze import compute
    >>> compute(expr, data)
    (2, 350)
    """
    __slots__ = '_hash', '_child', 'names', 'values', 'axis', 'keepdims'

    def __init__(self, _child, names, values, axis=None, keepdims=False):
        self._child = _child
        self.names = names
        self.values = values
        self.keepdims = keepdims
        self.axis = axis
        self._hash = None

    def _dshape(self):
        axis = self.axis
        if self.keepdims:
            shape = tuple(1 if i in axis else d
                          for i, d in enumerate(self._child.shape))
        else:
            shape = tuple(d
                          for i, d in enumerate(self._child.shape)
                          if i not in axis)
        measure = Record(list(zip(self.names,
                                  [v.schema for v in self.values])))
        return DataShape(*(shape + (measure,)))

    def __str__(self):
        s = 'summary('
        s += ', '.join('%s=%s' % (name, str(val))
                       for name, val in zip(self.fields, self.values))
        if self.keepdims:
            s += ', keepdims=True'
        s += ')'
        return s

@copydoc(Summary)
def summary(keepdims=False, axis=None, **kwargs):
    items = sorted(kwargs.items(), key=toolz.first)
    names = tuple(map(toolz.first, items))
    values = tuple(map(toolz.second, items))
    child = common_subexpression(*values)

    if len(kwargs) == 1 and not iscollection(child.dshape):
        while not iscollection(child.dshape):
            children = [i for i in child._inputs if isinstance(i, Expr)]
            if len(children) == 1:
                child = children[0]
            else:
                child = common_subexpression(*children)

    if axis is None:
        axis = tuple(range(ndim(child)))
    if isinstance(axis, (set, list)):
        axis = tuple(axis)
    if not isinstance(axis, tuple):
        axis = (axis,)
    return Summary(child, names, values, keepdims=keepdims, axis=axis)

def vnorm(expr, ord=None, axis=None, keepdims=False):
    """ Vector norm

    See np.linalg.norm
    """
    if ord is None or ord == 'fro':
        ord = 2
    if ord == inf:
        return max(abs(expr), axis=axis, keepdims=keepdims)
    elif ord == -inf:
        return min(abs(expr), axis=axis, keepdims=keepdims)
    elif ord == 1:
        return sum(abs(expr), axis=axis, keepdims=keepdims)
    elif ord % 2 == 0:
        return sum(expr ** ord, axis=axis, keepdims=keepdims) ** (1.0 / ord)
    return sum(abs(expr) ** ord, axis=axis, keepdims=keepdims) ** (1.0 / ord)

dshape_method_list.extend([
    (iscollection, set([count, nelements])),
    (lambda ds: (iscollection(ds) and
                 (isstring(ds) or isnumeric(ds) or isboolean(ds) or
                  isdatelike(ds) or isinstance(ds, TimeDelta))),
     set([min, max])),
    (lambda ds: len(ds.shape) == 1,
     set([nrows, nunique])),
    (lambda ds: iscollection(ds) and isboolean(ds),
     set([any, all])),
    (lambda ds: iscollection(ds) and (isnumeric(ds) or isboolean(ds)),
     set([mean, sum, std, var, vnorm])),
])

method_properties.update([nrows])
<commit_msg>Factor out decimal-y _schema into a tiny class<commit_after>from __future__ import absolute_import, division, print_function

import datashape
from datashape import Record, DataShape, dshape, TimeDelta, Decimal, Option
from datashape import coretypes as ct
from datashape.predicates import iscollection, isboolean, isnumeric, isdatelike
from numpy import inf
from odo.utils import copydoc
import toolz

from .core import common_subexpression
from .expressions import Expr, ndim
from .strings import isstring
from .expressions import dshape_method_list, method_properties

class Reduction(Expr):

    """ A column-wise reduction

    Blaze supports the same class of reductions as NumPy and Pandas.

        sum, min, max, any, all, mean, var, std, count, nunique

    Examples
    --------

    >>> from blaze import symbol
    >>> t = symbol('t', 'var * {name: string, amount: int, id: int}')
    >>> e = t['amount'].sum()

    >>> data = [['Alice', 100, 1],
    ...         ['Bob', 200, 2],
    ...         ['Alice', 50, 3]]

    >>> from blaze.compute.python import compute
    >>> compute(e, data)
    350
    """
    __slots__ = '_hash', '_child', 'axis', 'keepdims'

    def __init__(self, _child, axis=None, keepdims=False):
        self._child = _child
        if axis is None:
            axis = tuple(range(_child.ndim))
        if isinstance(axis, (set, list)):
            axis = tuple(axis)
        if not isinstance(axis, tuple):
            axis = (axis,)
        axis = tuple(sorted(axis))
        self.axis = axis
        self.keepdims = keepdims
        self._hash = None

    def _dshape(self):
        axis = self.axis
        if self.keepdims:
            shape = tuple(1 if i in axis else d
                          for i, d in enumerate(self._child.shape))
        else:
            shape = tuple(d
                          for i, d in enumerate(self._child.shape)
                          if i not in axis)
        return DataShape(*(shape + (self.schema,)))

    def _schema(self):
        schema = self._child.schema[0]
        if isinstance(schema, Record) and len(schema.types) == 1:
            result = toolz.first(schema.types)
        else:
            result = schema
        return DataShape(result)

    @property
    def symbol(self):
        return type(self).__name__

    @property
    def _name(self):
        child_name = self._child._name
        if child_name is None or child_name == '_':
            return type(self).__name__
        else:
            return '%s_%s' % (child_name, type(self).__name__)

    def __str__(self):
        kwargs = list()
        if self.keepdims:
            kwargs.append('keepdims=True')
        if self.axis != tuple(range(self._child.ndim)):
            kwargs.append('axis=' + str(self.axis))
        other = sorted(
            set(self.__slots__[1:]) - set(['_child', 'axis', 'keepdims']))
        for slot in other:
            kwargs.append('%s=%s' % (slot, getattr(self, slot)))
        name = type(self).__name__
        if kwargs:
            return '%s(%s, %s)' % (name, self._child, ', '.join(kwargs))
        else:
            return '%s(%s)' % (name, self._child)

class any(Reduction):
    schema = dshape(ct.bool_)

class all(Reduction):
    schema = dshape(ct.bool_)

class sum(Reduction):
    def _schema(self):
        return DataShape(datashape.maxtype(super(sum, self)._schema()))

class max(Reduction):
    pass

class min(Reduction):
    pass

class FloatingReduction(Reduction):
    def _schema(self):
        measure = self._child.schema.measure
        base = getattr(measure, 'ty', measure)
        return_type = Option if isinstance(measure, Option) else toolz.identity
        return DataShape(return_type(
            base if isinstance(base, Decimal) else ct.float64
        ))

class mean(FloatingReduction):
    pass

class var(FloatingReduction):

    """Variance

    Parameters
    ----------
    child : Expr
        An expression
    unbiased : bool, optional
        Compute an unbiased estimate of the population variance if this is
        ``True``. In NumPy and pandas, this parameter is called ``ddof`` (delta
        degrees of freedom) and is equal to 1 for unbiased and 0 for biased.
    """
    __slots__ = '_hash', '_child', 'unbiased', 'axis', 'keepdims'

    def __init__(self, child, unbiased=False, *args, **kwargs):
        self.unbiased = unbiased
        super(var, self).__init__(child, *args, **kwargs)

class std(FloatingReduction):

    """Standard Deviation

    Parameters
    ----------
    child : Expr
        An expression
    unbiased : bool, optional
        Compute the square root of an unbiased estimate of the population
        variance if this is ``True``.

        .. warning::

            This does *not* return an unbiased estimate of the population
            standard deviation.

    See Also
    --------
    var
    """
    __slots__ = '_hash', '_child', 'unbiased', 'axis', 'keepdims'

    def __init__(self, child, unbiased=False, *args, **kwargs):
        self.unbiased = unbiased
        super(std, self).__init__(child, *args, **kwargs)

class count(Reduction):

    """ The number of non-null elements """
    schema = dshape(ct.int32)

class nunique(Reduction):
    schema = dshape(ct.int32)

class nelements(Reduction):

    """Compute the number of elements in a collection, including missing values.

    See Also
    ---------
    blaze.expr.reductions.count: compute the number of non-null elements

    Examples
    --------
    >>> from blaze import symbol
    >>> t = symbol('t', 'var * {name: string, amount: float64}')
    >>> t[t.amount < 1].nelements()
    nelements(t[t.amount < 1])
    """
    schema = dshape(ct.int32)

def nrows(expr):
    return nelements(expr, axis=(0,))

class Summary(Expr):

    """ A collection of named reductions

    Examples
    --------

    >>> from blaze import symbol
    >>> t = symbol('t', 'var * {name: string, amount: int, id: int}')
    >>> expr = summary(number=t.id.nunique(), sum=t.amount.sum())

    >>> data = [['Alice', 100, 1],
    ...         ['Bob', 200, 2],
    ...         ['Alice', 50, 1]]

    >>> from blaze import compute
    >>> compute(expr, data)
    (2, 350)
    """
    __slots__ = '_hash', '_child', 'names', 'values', 'axis', 'keepdims'

    def __init__(self, _child, names, values, axis=None, keepdims=False):
        self._child = _child
        self.names = names
        self.values = values
        self.keepdims = keepdims
        self.axis = axis
        self._hash = None

    def _dshape(self):
        axis = self.axis
        if self.keepdims:
            shape = tuple(1 if i in axis else d
                          for i, d in enumerate(self._child.shape))
        else:
            shape = tuple(d
                          for i, d in enumerate(self._child.shape)
                          if i not in axis)
        measure = Record(list(zip(self.names,
                                  [v.schema for v in self.values])))
        return DataShape(*(shape + (measure,)))

    def __str__(self):
        s = 'summary('
        s += ', '.join('%s=%s' % (name, str(val))
                       for name, val in zip(self.fields, self.values))
        if self.keepdims:
            s += ', keepdims=True'
        s += ')'
        return s

@copydoc(Summary)
def summary(keepdims=False, axis=None, **kwargs):
    items = sorted(kwargs.items(), key=toolz.first)
    names = tuple(map(toolz.first, items))
    values = tuple(map(toolz.second, items))
    child = common_subexpression(*values)

    if len(kwargs) == 1 and not iscollection(child.dshape):
        while not iscollection(child.dshape):
            children = [i for i in child._inputs if isinstance(i, Expr)]
            if len(children) == 1:
                child = children[0]
            else:
                child = common_subexpression(*children)

    if axis is None:
        axis = tuple(range(ndim(child)))
    if isinstance(axis, (set, list)):
        axis = tuple(axis)
    if not isinstance(axis, tuple):
        axis = (axis,)
    return Summary(child, names, values, keepdims=keepdims, axis=axis)

def vnorm(expr, ord=None, axis=None, keepdims=False):
    """ Vector norm

    See np.linalg.norm
    """
    if ord is None or ord == 'fro':
        ord = 2
    if ord == inf:
        return max(abs(expr), axis=axis, keepdims=keepdims)
    elif ord == -inf:
        return min(abs(expr), axis=axis, keepdims=keepdims)
    elif ord == 1:
        return sum(abs(expr), axis=axis, keepdims=keepdims)
    elif ord % 2 == 0:
        return sum(expr ** ord, axis=axis, keepdims=keepdims) ** (1.0 / ord)
    return sum(abs(expr) ** ord, axis=axis, keepdims=keepdims) ** (1.0 / ord)

dshape_method_list.extend([
    (iscollection, set([count, nelements])),
    (lambda ds: (iscollection(ds) and
                 (isstring(ds) or isnumeric(ds) or isboolean(ds) or
                  isdatelike(ds) or isinstance(ds, TimeDelta))),
     set([min, max])),
    (lambda ds: len(ds.shape) == 1,
     set([nrows, nunique])),
    (lambda ds: iscollection(ds) and isboolean(ds),
     set([any, all])),
    (lambda ds: iscollection(ds) and (isnumeric(ds) or isboolean(ds)),
     set([mean, sum, std, var, vnorm])),
])

method_properties.update([nrows])
<|endoftext|>