Пример #1
0
def simulate(axis, shape, nsims, maxcats, xlim, ylim):
    c_space = commcost.Space(shape)
    r_space = rectlang.Space(shape)
    for n_cats in range(2, maxcats + 1):
        color = str(((maxcats + 2) - (n_cats - 1)) / (maxcats + 2))
        X, Y = [], []
        for i in range(nsims):
            _, part = commcost.random_partition(shape, n_cats, True)
            comp = r_space.complexity(part)
            cost = c_space.cost(part)
            if comp < xlim[1] and cost > ylim[0]:
                X.append(comp)
                Y.append(cost)
            _, part = commcost.random_partition(shape, n_cats, False)
            comp = r_space.complexity(part)
            cost = c_space.cost(part)
            if comp < xlim[1] and cost > ylim[0]:
                X.append(comp)
                Y.append(cost)
        axis.scatter(X, Y, color=color)
    return X, Y
Пример #2
0
def transform_paper3_data():
    r_space = rectlang.Space((6, 8))
    c_space = commcost.Space((6, 8))
    old_new = {
        0: (1, 3),
        1: (0, 2),
        2: (0, 6),
        3: (2, 5),
        4: (5, 1),
        5: (3, 7),
        6: (0, 5),
        7: (5, 7),
        8: (4, 2),
        9: (5, 2),
        10: (4, 0),
        11: (0, 0),
        12: (5, 6),
        13: (4, 3),
        14: (1, 4),
        15: (4, 4),
        16: (1, 1),
        17: (4, 1),
        18: (2, 4),
        19: (5, 4),
        20: (4, 5),
        21: (3, 0),
        22: (5, 5),
        23: (3, 3),
        24: (2, 6),
        25: (3, 5),
        26: (0, 4),
        27: (0, 7),
        28: (2, 1),
        29: (3, 1),
        30: (3, 4),
        31: (3, 2),
        32: (4, 6),
        33: (1, 2),
        34: (0, 1),
        35: (3, 6),
        36: (1, 7),
        37: (1, 0),
        38: (4, 7),
        39: (2, 2),
        40: (2, 7),
        41: (2, 0),
        42: (1, 5),
        43: (1, 6),
        44: (5, 0),
        45: (5, 3),
        46: (2, 3),
        47: (0, 3)
    }
    data = {'chains': []}
    for chain in ['I', 'J', 'K', 'L']:
        chain_data = {'chain_id': chain, 'generations': []}
        for generation in range(11):
            lang = np.full((6, 8), -1, dtype=int)
            words = {}
            with open('/Users/jon/Code/flatlanders/data/experiment_3/%s/%is' %
                      (chain, generation)) as file:
                for i, line in enumerate(file):
                    word = line.split('\t')[0]
                    if word in words:
                        words[word].append(i)
                    else:
                        words[word] = [i]
            for cat, (key, values) in enumerate(words.items(), 0):
                for value in values:
                    coord = old_new[value]
                    lang[coord] = cat
            comp = r_space.complexity(lang)
            cost = c_space.cost(lang)
            chain_data['generations'].append({
                'prod_cost': cost,
                'prod_complexity': comp
            })
        data['chains'].append(chain_data)
    print(data)
Пример #3
0
languages. This was not included in the paper; refer to my thesis
instead.
'''

import pickle
import numpy as np
import matplotlib.pyplot as plt
import commcost
import rectlang
import tools
import colors

plt.rcParams['svg.fonttype'] = 'none'

rectlang_space = rectlang.Space((8,8))
commcost_space = commcost.Space((8,8))
commcost_space_discrete = commcost.Space((8,8), gamma=99999)

def save_results(array, filename):
	with open(filename, mode='wb') as file:
		pickle.dump(array, file)

def restore_results(filename):
	with open(filename, mode='rb') as file:
		array = pickle.load(file)
	return array

def sim_all_cats(n_sims, shape, convex=False):
	complexy_results = np.zeros((64, n_sims), dtype=float)
	commcost_results = np.zeros((64, n_sims), dtype=float)
	commcost_results_discrete = np.zeros((64, n_sims), dtype=float)
Пример #4
0
'''
Converts the raw results from chains.json into the same JSON format
used by the model. Produces similar output to model_process.py
'''

import json
import numpy as np
import rectlang
import commcost
import varofinf
import tools

shape = (8,8)

rectlang_space = rectlang.Space(shape, solutions_file='../data/8x8_solutions.json')
commcost_space = commcost.Space(shape)

def raw_model_results_to_json_files(input_file, output_file):
	results = {'bottleneck':2, 'exposures':4, 'chains':[]}
	data = tools.read_json_lines(input_file)
	for chain in data:
		chain_data = {'chain_id':chain['chain_id'], 'first_fixation':chain['first_fixation'], 'generations':[]}
		for gen_i, generation in enumerate(chain['generations']):
			generation_data = {'generation_number':gen_i, 'productions':generation['partition'], 'data_out':[]}
			productions = np.array(generation['partition'], dtype=int).reshape((8,8))
			for stim_i in generation['training_out']:
				meaning = (stim_i // shape[0], stim_i % shape[1])
				signal = int(productions[meaning])
				generation_data['data_out'].append((meaning, signal))
			generation_data['prod_expressivity'] = len(np.unique(productions))
			generation_data['prod_cost'] = commcost_space.cost(productions)
Пример #5
0
    def __init__(self,
                 shape=(8, 8),
                 maxcats=4,
                 prior='simplicity',
                 weight=1.0,
                 noise=0.05,
                 exposures=4,
                 mcmc_iterations=5000):

        if not isinstance(shape, tuple) or len(shape) != 2:
            raise ValueError(
                'shape should be a tuple containing the height and width')
        if not isinstance(shape[0], int) or shape[0] < 1:
            raise ValueError('height must be positive integer')
        if not isinstance(shape[1], int) or shape[1] < 1:
            raise ValueError('width must be postive integer')
        self._shape = shape
        self._size = np.product(shape)
        self._cells, self._rects = self._generate_cells_and_rects()

        if not isinstance(maxcats, int) or maxcats < 1 or maxcats > self._size:
            raise ValueError(
                'Invalid maxcats: Should be int between 1 and %i' % self._size)
        self._maxcats = maxcats

        if prior == 'simplicity':
            self._prior_grid = rectlang.Space(self._shape,
                                              rotational_invariance,
                                              max_exhaustive_size,
                                              max_beam_width)
            self._prior_func = self._prior_grid.complexity
        elif prior == 'informativeness':
            self._prior_grid = commcost.Space(self._shape, gamma=gamma, mu=mu)
            self._prior_func = self._prior_grid.cost
        elif not callable(prior):
            raise ValueError(
                'Invalid prior: Use \'simplicity\' or \'informativeness\', or pass a callable function'
            )
        else:
            self._prior_func = prior

        if not isinstance(weight, (float, int)):
            raise ValueError('Invalid weight: Should be float or int')
        self._weight = weight

        if not isinstance(noise, float) or noise < 0 or noise > 1:
            raise ValueError('Invalid noise: Should be float between 0 and 1')
        self._noise = noise

        if not isinstance(exposures, int) or exposures < 1:
            raise ValueError('Invalid exposures: Should be int greater than 0')
        self._exposures = exposures

        if not isinstance(mcmc_iterations, int) or mcmc_iterations < 1000:
            raise ValueError(
                'Invalid mcmc_iterations: Should be int greater than or equal to 1000'
            )
        self._mcmc_iterations = mcmc_iterations

        self._prob_correct = np.log2(1.0 - self._noise)
        self._prob_incorrect = np.log2(self._noise / (self._maxcats - 1))

        self.language = None
Пример #6
0
    def __init__(self,
                 generations=10,
                 shape=(8, 8),
                 mincats=1,
                 maxcats=4,
                 prior='simplicity',
                 weight=1.0,
                 noise=0.05,
                 bottleneck=2,
                 exposures=4,
                 mcmc_iterations=5000):
        if not isinstance(generations, int) or generations < 1:
            raise ValueError(
                'Invalid generations: Should be int greater than 0')
        self._generations = generations

        if not isinstance(shape, tuple) or len(shape) != 2:
            raise ValueError(
                'shape should be a tuple containing the height and width')
        if not isinstance(shape[0], int) or shape[0] < 1:
            raise ValueError('height must be positive integer')
        if not isinstance(shape[1], int) or shape[1] < 1:
            raise ValueError('width must be postive integer')
        self._shape = shape
        self._size = np.product(shape)

        if not isinstance(mincats, int) or mincats < 1 or mincats > self._size:
            raise ValueError(
                'Invalid mincats: Should be int between 1 and %i' % self._size)
        self._mincats = mincats

        if not isinstance(
                maxcats,
                int) or maxcats < self._mincats or maxcats > self._size:
            raise ValueError(
                'Invalid maxcats: Should be int between %i and %i' %
                (self._mincats, self._size))
        self._maxcats = maxcats

        self._rectlang_grid = rectlang.Space(self._shape,
                                             rotational_invariance,
                                             max_exhaustive_size,
                                             max_beam_width)
        self._commcost_grid = commcost.Space(self._shape, gamma=gamma, mu=mu)
        if prior == 'simplicity':
            self._prior_func = self._rectlang_grid.complexity
        elif prior == 'informativeness':
            self._prior_func = self._commcost_grid.cost
        elif not callable(prior):
            raise ValueError(
                'Invalid prior: Use \'simplicity\' or \'informativeness\', or pass a callable function'
            )
        else:
            self._prior_func = prior
        self._prior = str(prior)

        if not isinstance(weight, (float, int)):
            raise ValueError('Invalid weight: Must be float or int')
        self._weight = weight

        if not isinstance(noise, float) or noise < 0 or noise > 1:
            raise ValueError('Invalid noise: Must be float between 0 and 1')
        self._noise = noise

        if not isinstance(bottleneck, int) or bottleneck < 1 or bottleneck > 4:
            raise ValueError(
                'Invalid bottleneck: Should be int between 1 and 4')
        self._bottleneck = bottleneck
        self._segments = self._segment_space()

        if not isinstance(exposures, int) or exposures < 1:
            raise ValueError('Invalid exposures: Must be int greater than 0')
        self._exposures = exposures

        if not isinstance(mcmc_iterations, int) or mcmc_iterations < 1000:
            raise ValueError(
                'Invalid mcmc_iterations: Should be int greater than or equal to 1000'
            )
        self._mcmc_iterations = mcmc_iterations

        self._model_parameters = {
            'shape': self._shape,
            'mincats': self._mincats,
            'maxcats': self._maxcats,
            'prior': self._prior,
            'weight': self._weight,
            'noise': self._noise,
            'bottleneck': self._bottleneck,
            'exposures': self._exposures,
            'mcmc_iterations': self._mcmc_iterations
        }

        self.generations = []