Ejemplo n.º 1
0
 def select(self, dataset):
     if dataset.shape != self.dataset_shape:
         raise ValueError("shape mismatch (wrong factory)")
     if not dataset.shape:
         sink = dataset[...]
     sink = np.empty(shape=self.sink_shape, dtype=dataset.dtype)
     if debug: sink[...] = -47119999
     for ssel, dsel in zip(iter_product(*self.sink_selectors), 
                           iter_product(*self.source_selectors)):
         sink[ssel] = dataset[dsel]
     return DatasetSelection(self.sink_axes, sink)
Ejemplo n.º 2
0
def unfold_settings(exp_config):
    '''
        Unfold a command and get all possible
        settings. Returned as an Iterable.
        The possible settings are generated by taking
        a Cartesian product over list fields of the command

        Note: for `ratio` command, the `memory_budget` is calculated here in order to
              avoid multiple runs of baseline trial
    '''
    setting_heading = list()
    list_fields = list()
    for (k, v) in exp_config.items():
        if isinstance(v, list):
            setting_heading.append(k)
            list_fields.append(v)

    if not list_fields:
        yield exp_config
    else:
        for combo in iter_product(*list_fields):
            # necessary to copy each time
            # since the old data might be used later
            result = exp_config.copy()
            for i in range(len(list_fields)):
                result[setting_heading[i]] = combo[i]
            if result.get('kind') == 'ratio':
                result['memory_budget'] *= result['ratio']
            yield result
Ejemplo n.º 3
0
def dump_image_batch(X, file_name, max_size = 10, figsize = (10, 10)):
    from itertools import product as iter_product
    nrows = max_size
    ncols = nrows
    if 1 == 1:
        if 1 == 1:
            if 1 == 1:
                shape = X.shape
                figs, axes = plt.subplots(nrows, ncols, figsize = figsize,
                                          squeeze = False)

                for ax in axes.flatten():
                    ax.set_xticks([])
                    ax.set_yticks([])
                    ax.axis('off')
                for i, (r, c) in enumerate(iter_product(range(nrows), range(ncols))):
                    if i >= shape[0]:
                        break
                    img = X[i].transpose((1, 2, 0))
                    axes[r, c].imshow(img, interpolation = 'none')

                plt.savefig(os.path.join('', file_name))
                #plt.cla()
                plt.clf()
                plt.close()
Ejemplo n.º 4
0
    def parse_options(self, response):
        product = response.meta['product']
        options_found = 0
        try:
            ajax_url = response.meta['options_url']
            data = json.loads(response.body)
            options = iter_product(
                *(map(lambda d: dict(attr_id=attr['id'], **d), attr['values'])
                  for attr in data.get('attributes', [])
                  if not attr['disabled']))
            for options_selected in options:
                new_product = Product(product)
                for option in options_selected:
                    options_found += 1
                    opt_id = 'attributes[%s]' % option['attr_id']
                    opt_value_id = option['value_id']
                    # new_product['identifier'] += ':' + opt_value_id
                    new_product['name'] += ' ' + option['value']
                    ajax_url = add_or_replace_parameter(
                        ajax_url, opt_id, opt_value_id)
                meta = response.meta.copy()
                meta['product'] = new_product

                yield Request(ajax_url,
                              callback=self.parse_options_prices,
                              meta=meta)
        except Exception, e:
            self.log('NO OPTIONS WARNING => %r' % e)
            yield product
Ejemplo n.º 5
0
    def solve_vibration_modes(self, num=20):
        """
        Find the 'num' lowest generalized eigenvalues, along with eigenvectors. \n
            Aₕu = ω²Mₕu 
        """
        if self.A_h is None:
            self.generate_A_h()
        if self.M_h is None:
            self.generate_M_h()

        eigvals_small, eigvecs_small = spla.eigsh(A=self.A_h, M=self.M_h, 
                                                  k=num, which="LM", sigma=0.0)
        self.num_eigenpairs = num
        self.vibration_frequencies = eigvals_small

        # List of the displacement eigenvectors for each eigenvalue: 
        self.vibration_eigenvectors = []
        num_dof = int(self.num_nodes - len(self.dirichlet_BC_basis_functions)//2)
        for k in range(num):
            displacement_vec = np.zeros((num_dof, 2))
    
            # Eigenvectors stored column-wise:
            vibration_eigenvec = eigvecs_small[:, k]
            
            for n, d in iter_product(range(num_dof), (0, 1)):
                displacement_vec[n, d] = vibration_eigenvec[2*n + d]
            
            self.vibration_eigenvectors.append(displacement_vec)
 
        print("Done solving for eigenmodes!")
Ejemplo n.º 6
0
    def generate_F_h(self):
        """
        Generate the source vector. Sum over elements and add contributions from each basis function.
        """
        # Source function f([x, y]) must be callable.
        assert(callable(self.f))
        # Making the full Source vector:
        self.F_h = np.zeros(self.num_basis_functions)

        # Reference triangle nodes:
        eta1, eta2, eta3 = self.reference_triangle_nodes

        T = self.reference_to_global_transformation

        for k, element in enumerate(self.triang):
            J_k = self.generate_jacobian(k)
            det_J_k = la.det(J_k)

            # Loop through nodes in each element and two components per node:
            for (i, node), d in iter_product(enumerate(element), (0, 1)):
                # print(f"i: {i}, d: {d}\tnode: {node}")
                
                global_index = 2*node + d

                # Integrand. ϕ_i_d^T⋅f([x, y]) = [(1-d)*ϕ_i, d*ϕ_i]^T⋅[f_1(x, y), f_2(x, y)] 
                #                              = ((1 - d)*f_1(x, y) + d*f_2(x, y))*ϕ_i(x, y)
                integrand = lambda eta: self.f(T(eta, k, J_k))[d]*self.basis_functions[i](eta)

                # Add contribution from basis function component d. Integrate over reference triangle.
                self.F_h[global_index] += det_J_k*quadrature2D(integrand, eta1, eta2, eta3, self.quad_points)
        
        # Reshape F_h to a column vector:
        self.F_h = self.F_h.reshape(len(self.F_h), 1)
Ejemplo n.º 7
0
def state_to_array(dimensions: Tuple[int, ...], state: int):
    arr = np.ndarray(dimensions, np.bool)
    for coords in iter_product(*map(range, dimensions)):
        arr[(*coords, )] = bool(state >> sum(c * product(*dimensions[:j])
                                             for j, c in enumerate(coords))
                                & 1)
    return arr
Ejemplo n.º 8
0
def plot_weights(W, plot_name, file_path = '.',
                    max_subplots = 100, max_figures = 64,
                    figsize = (28, 28)):
    try:
   	W = W.get_value(borrow = True)
    except:
	W=W
	''' W=np.reshape(W,(2,5,5,32))
	W=np.swapaxes(W,0,3)
	W=np.swapaxes(W,3,1)
	W=np.swapaxes(W,2,3)'''
    #W = W/4
    shape = W.shape
    assert((len(shape) == 2) or (len(shape) == 4))
    max_val = np.max(W)
    min_val = np.min(W)

    if len(shape) == 2:
        plt.figure(figsize = figsize)
        plt.imshow(W, cmap = 'gray',#'jet',
                   vmax = max_val, vmin = min_val,
                   interpolation = 'none')
        plt.axis('off')
        plt.colorbar()
        file_name = plot_name + '.png'
        plt.savefig(os.path.join(file_path, file_name))
        plt.clf()
        plt.close()
        return

    nrows = min(np.ceil(np.sqrt(shape[1])).astype(int),
                np.floor(np.sqrt(max_subplots)).astype(int))
    ncols = nrows
    '''
    max_val = -np.inf
    min_val = np.inf
    for i in range(shape[0]):
        tmp = np.mean(W[i], axis = 0)
        max_val = max(max_val, np.max(tmp))
        min_val = min(min_val, np.min(tmp))
    '''
    for j in range(min(shape[0], max_figures)):
        figs, axes = plt.subplots(nrows, ncols, figsize = figsize,
                                  squeeze = False)
        for ax in axes.flatten():
            ax.set_xticks([])
            ax.set_yticks([])
            ax.axis('off')
        for i, (r, c) in enumerate(iter_product(range(nrows), range(ncols))):
            if i >= shape[1]:
                break
            im = axes[r, c].imshow(W[j, i], cmap = 'Greens',#'jet',
                                   vmax = max_val, vmin = min_val,
                                   interpolation = 'none')
        figs.colorbar(im, ax = axes.ravel().tolist())
        file_name = plot_name + '_fmap' + str(j) + '.png'
        plt.savefig(os.path.join(file_path, file_name))
        plt.clf()
        plt.close()
    return
Ejemplo n.º 9
0
    def ksgrid(self, ksgrid: np.ndarray):
        self._ksgrid = ksgrid

        p_range = self.params['kwargs']['npartitions_range']

        kga_inertia = np.zeros(self.ksgrid.shape, dtype=np.float64)
        for ij in iter_product(range(self.ksgrid.shape[0]),
                               range(self.ksgrid.shape[1])):
            if self.n_clusters_out[ij] < self.ksgrid[ij].n_clusters:
                kga_inertia[ij] = np.nan
            else:
                kga_inertia[ij] = self.ksgrid[ij].inertia_

        self.kga_inertia_heatmap.set(kga_inertia,
                                     ylabels=list(range(*p_range)),
                                     cmap=self.kga_inertia_heatmap_cmap,
                                     annot=False)

        ixs_lowest = np.dstack(
            np.unravel_index(np.argsort(kga_inertia.ravel()),
                             kga_inertia.shape))[0]
        ixs_lowest[:, 0] += self.params['kwargs']['npartitions_range'][0]

        self.inertia_sorted = pd.DataFrame.from_dict({
            'n_clusters':
            ixs_lowest[:, 0],
            'trial':
            ixs_lowest[:, 1]
        })

        self.kga_inertia_heatmap.show()
Ejemplo n.º 10
0
def gif(
    iterations: int,
    dimensions: Tuple[int, ...],
    scaling: int,
    path: str,
    simulation: Iterator[int],
    tpf: int = 50,
    loop: bool = True,
    **kwargs,
):
    imgs = []
    width, height = dimensions
    for state in simulation:
        img = Image.new("RGB", dimensions, (255, 255, 255))
        data = img.load()
        for x, y in iter_product(range(width), range(height)):
            if state >> y * width + x & 1:
                data[x, y] = (0, 0, 0)
        imgs.append(
            img.resize((scaling * width, scaling * height), Image.NEAREST))
    imgs[0].save(
        path,
        format="GIF",
        save_all=True,
        append_images=imgs[1:],
        duration=tpf,
        loop=int(not loop),
    )
def expand_parameters(arg_vals):
    '''
    Given a list of args and values
    return a list of tuples
    containing all possible sequences
    of length n.
    '''
    arg_tuples = dict_to_tuples(arg_vals)
    return [dict(args) for args in iter_product(*arg_tuples)]
Ejemplo n.º 12
0
def cosine_similarity_overlap(v1, v2):
    intersect = len(v1 & v2)
    for w1, w2 in iter_product(list(v1), list(v2)):
        if (w1 in w2 or w2 in w1) and w1 != w2:
            intersect += 0.5
    if intersect > 0:
        return intersect * 1. / np.sqrt(len(v1) * len(v2))
    else:
        return 0.
def get_gram_ratio(w2v, text1, text2, n_grams_1=1, n_grams_2=1, n_jobs=1):
    t1 = list(ngrams(text1.split(), n_grams_1))
    t2 = list(ngrams(text2.split(), n_grams_2))
    pairs = list(iter_product(t1, t2, repeat=1))
    res = list(map(lambda x: similarity(w2v, x), pairs))
    if len(res) == 0:
        return 0
    else:
        return np.mean(res)
Ejemplo n.º 14
0
def normalize(ring, f, kappa=None):
    "If kappa is not set, normalize by the sum of values in the domain."
    if kappa == None:
        kappa = ring.zero
        for args in iter_product(*[var.var_domain for var in f.domain]):
            kappa = ring.add(kappa, f(*args))
    def normalized(*args):
        return ring.invmul(f(*args), kappa)
    return Factor(normalized, f.domain)
def expand_parameters(arg_vals):
    '''
    Given a list of args and values
    return a list of tuples
    containing all possible sequences
    of length n.
    '''
    arg_tuples = dict_to_tuples(arg_vals)
    return [dict(args) for args in iter_product(*arg_tuples)]
Ejemplo n.º 16
0
def count_worker(dimensions, slice_sizes, neighborhood_radius, b, s, state, i):
    return (b * (~state >> i & 1) + s * (state >> i & 1) >> sum(
        state >> sum(
            (i // slice_sizes[j] + offset) % dimensions[j] * slice_sizes[j]
            for j, offset in enumerate(offsets))
        & any(offset != 0 for offset in offsets) for offsets in iter_product(
            *(range(-neighborhood_radius, neighborhood_radius + 1)
              for _ in dimensions)))
            & 1) << i
Ejemplo n.º 17
0
def pattern_worker(dimensions, slice_sizes, neighborhood_radius, patterns,
                   state, i):
    return (patterns[sum(
        (state >> sum(
            (i // slice_sizes[k] + offset) % dimensions[k] * slice_sizes[k]
            for k, offset in enumerate(offsets))
         & 1) << j
        for j, offsets in enumerate(
            iter_product(*(range(-neighborhood_radius, neighborhood_radius + 1)
                           for _ in dimensions))))] << i)
Ejemplo n.º 18
0
def make_sample_index_iterator_maker(n, steps, r):
    """
    Make a generator of iterators that iterate over n-uples of integers
    that parametrize certain complex numbers on the unit circle with a
    given number of steps on a semicircle.
    """

    indr1 = range(1, steps + 1)
    indr2 = range(-steps + 1, 0)

    if r is None:
        return lambda: iter_product(
            indr1,
            *[iter_chain(indr1, indr2) for _ in range(1, n)]
        )

    else:
        if n == 1:
            return lambda: iter(indr1)

        else:
            if r % n:
                rr = [r, -r]
            else:
                rr = [r]

            def make_2ind_iterator():
                for i1 in indr1:
                    for m in rr:
                        i2 = (m - i1 + n - 1) % (2*n) - n + 1
                        yield (i1, i2)

            if n == 2:
                return make_2ind_iterator

            elif n >= 3:
                return lambda: map(
                    _splice_first,
                    iter_product( make_2ind_iterator(),
                                  *[ iter_chain(indr1, indr2)
                                     for _ in range(2, n) ] )
                )
Ejemplo n.º 19
0
 def __repr__(self):
     table = [( ("(" + ", ".join([x.name for x in self.domain]) + ")", "value") )]
     for args in iter_product(*[var.var_domain for var in self.domain]):
         table.append( (str(args), str(self(*args))) )
     arg_len = max([len(x[0]) for x in table])
     val_len = max([len(x[1]) for x in table])
     s = [table[0][0].ljust(arg_len) + " " + table[0][1]]
     s.append(("-" * arg_len) + " " + ("-" * val_len))
     for i in range(1,len(table)):
         s.append(table[i][0].ljust(arg_len) + " " + table[i][1])
     return '\n'.join(s)
Ejemplo n.º 20
0
    def _compute_for_index_combinations(self, set_one, set_two):
        """
        Computes value of kernel matrix for all combinations of given set of indices
        """

        return np.array([
            self._eval_kernel(idx_one, idx_two)
            for idx_one, idx_two in iter_product(set_one, set_two)
        ],
                        dtype=self._sample.dtype).reshape(
                            len(set_one), len(set_two))
Ejemplo n.º 21
0
def affine_tryout(invM, pts_src, pts_dst, triangle_dst):
    '''
        Brute force for finding correct affine mapping
        Biggest triangle maps to biggest triangle (what a Sherlock)
    '''
    n_pts = pts_src.shape[0]
    pts_src_ext = np.hstack((pts_src, np.ones((n_pts, 1))))
    pts_dst_ext = np.hstack((pts_dst, np.ones((n_pts, 1))))
    # trying all combinations (some of them are invalid)
    for (p0, p1), p2 in iter_product(iter_product(triangle_dst, triangle_dst),
                                     triangle_dst):
        N = find_affine(np.vstack((p0, p1, p2)))
        # determinant of affine mapping has to be regular
        if np.linalg.matrix_rank(N) == 3:
            P = np.matmul(N, invM)
            pts_src_mapped = np.matmul(P, pts_src_ext.T).T
            valid, mapping = is_affine_valid(pts_src_mapped, pts_dst_ext)
            if valid:
                return P, mapping

    return None, None
Ejemplo n.º 22
0
    def generate_M_ref(self):
        # 36 interactions.
        M_ref = np.zeros((6, 6), dtype=float)
        local_indices = (0, 1, 2)
        d_pairs = ((0, 0), (0, 1), (1, 0), (1, 1))
        
        for i, j, (d_i, d_j) in iter_product(local_indices, local_indices, d_pairs):
            row, col = 2*i + d_i, 2*j + d_j
            # Calculate the mass matrix on the reference element: det_J_k = 1.0
            M_ref[row, col] = self.M_i_j(i, j, d_i, d_j, det_J_k=1.0)

        return M_ref
Ejemplo n.º 23
0
def build_iterator(**kwargs):
    parsers = {
        'modes': parse_mode_pattern,
        'days': parse_day_pattern,
        'intervals': parse_time_intervals
    }

    iterables = []

    for category, pattern in kwargs.items():
        iterables.append(parsers[category](pattern))

    return iter_product(*iterables)
Ejemplo n.º 24
0
def marginalize(ring, f, retain):
    "Marginalize according to the addition operator of the ring."
    bitmask = [var.name in retain for var in f.domain]
    results = {}
    for args in iter_product(*[var.var_domain for var in f.domain]):
        reduced_args = tuple(compress(args,bitmask))
        if reduced_args in results:
            results[reduced_args] = ring.add(results[reduced_args], f(*args))
        else:
            results[reduced_args] = f(*args)
    def marginalized(*args):
        return results[args]
    return Factor(marginalized, [var for var in f.domain if var.name in retain])
Ejemplo n.º 25
0
def MultiIndex_from_product_indices(indices):
    """Create MultiIndex from cartesian product of (Multi)Indices

    Parameters
    ----------
    indices: subclass of pandas.Index

    Returns
    -------
    idx: pandas.MultiIndex
    """
    names = np.concatenate(idx.names for idx in indices)
    values = iter_product(indices)
    return pd.MultiIndex.from_tuples(values, names=names)
Ejemplo n.º 26
0
def join(ring, f1, f2):
    "Join two factors using the ring's multiplication operator."
    f1_names = [var.name for var in f1.domain]
    new_domain = f1.domain + [var for var in f2.domain if var.name not in f1_names]
    new_names = [var.name for var in new_domain]
    arg_pos2 = [new_names.index(var.name) for var in f2.domain]
    results = {}
    for args in iter_product(*[var.var_domain for var in new_domain]):
        args1 = args[:len(f1_names)]
        args2 = [args[x] for x in arg_pos2]
        results[args] = ring.mul(f1(*args1), f2(*args2))
    def merged(*args):
        return results[args]
    return Factor(merged, new_domain)
Ejemplo n.º 27
0
    def join(self, args: List[Any]) -> Union[List[str], str]:
        # expand everything that isn't a string to a list
        args = [[item] if isinstance(item, str) else list(item)
                for item in args]
        # combine items into list corresponding to cartesian product
        res = [''.join(flat_args) for flat_args in iter_product(*args)]

        if len(res) > 1:
            return res

        if res:
            return res[0]

        return ''
Ejemplo n.º 28
0
 def default_get(self, var_fields):
     template = self.env['product.template'].browse(
         self.env.context['active_id'])
     values = iter_product(
         *map(lambda x: x.value_ids.ids, template.attribute_line_ids))
     val_list = list(values)
     products = template.product_variant_ids
     lines = []
     for vals in val_list:
         line_vals = {
             'attributes': [(6, 0, list(vals))],
             'product': self._product_by_variants(vals, products),
         }
         line_vals['old_product'] = line_vals['product']
         lines.append((0, 0, line_vals))
     return {'products': lines}
Ejemplo n.º 29
0
 def default_get(self, var_fields):
     template = self.env['product.template'].browse(
         self.env.context['active_id'])
     values = iter_product(*map(lambda x: x.value_ids.ids,
                                template.attribute_line_ids))
     val_list = list(values)
     products = template.product_variant_ids
     lines = []
     for vals in val_list:
         line_vals = {
             'values': [(6, 0, list(vals))],
             'product': self._product_by_variants(vals, products),
         }
         line_vals['old_product'] = line_vals['product']
         lines.append((0, 0, line_vals))
     return {'products': lines}
Ejemplo n.º 30
0
def generate_axes(axes, count):
    """ Returns an array of strings ['XX', 'XZ',...]
    It splits up the individual characters of axes and permutates count times
    So ('XY', 2) returns XX, XY, YX, YY.

    Keyword arguments:
    axis -- a string of any combination of X, Y, Z -- 'XY', 'XYZ'
    count -- the number to permutate over.

    Returns a string count characters long.
    """
    array_axes = []
    all_axes = iter_product(axes, repeat=count)
    for b in all_axes:
        array_axes.append(''.join(str(i) for i in b))
    return array_axes
Ejemplo n.º 31
0
    def set_plots(self, tuning_curves: Dict[str, np.ndarray],
                  y_units: List[str]):
        """
        Set the subplots

        :param input_arrays: padded input arrays (2D),  shape is [num_samples, padded_peak_curve_length]
        :param n_clusters: number of clusters
        :param y_pred: cluster predictions (labels)
        :param xzero_pos: set the zero position as the 'zero' position of the input array or the 'maxima' of the input array
        :param error_band: Type of error band to show, one of either 'ci' or 'std'
        """
        self.clear()

        stim_types = list(tuning_curves.keys())

        if len(stim_types) == 1:
            self.ncols = 1
        elif len(stim_types) < 5:
            self.ncols = 2
        else:
            self.ncols = 3

        self.nrows = ceil(len(tuning_curves.keys()) / self.ncols)

        self.axs = self.fig.subplots(self.nrows, self.ncols)
        self.fig.tight_layout()

        for i, plot_ix in enumerate(
                iter_product(range(self.nrows), range(self.ncols))):
            if not i < len(stim_types):
                break

            stim_type = stim_types[i]
            data = tuning_curves[stim_type]

            if len(stim_types) == 1:
                plot = self.axs
            else:
                plot = self.axs[plot_ix]

            plot.plot(data[0], data[1], c='k')
            plot.set_xlabel(stim_type)
            plot.set_ylabel(f"{y_units} response")

        self.draw()
Ejemplo n.º 32
0
    def generate_nodes(self):
        print("Generating nodes.")

        # X-direction
        print("Searching toward x-direction.")
        Nx = self.find_node_candidates(self.T_x, self.x_bounds,
                                       self.source.llx, self.source.urx)

        for n in Nx:
            print(n)
        print("")

        # Y-direction
        print("Searching toward y-direction.")
        Ny = self.find_node_candidates(self.T_y, self.y_bounds,
                                       self.source.lly, self.source.ury)
        for n in Ny:
            print(n)
        print("")

        # Final node set
        self.nodes = NodeSet()

        remaining = set(self.sinks)

        for nx, ny in iter_product(Nx, Ny):
            sinks_nx = nx.sink_set
            sinks_ny = ny.sink_set

            sinks_new = sinks_nx.intersection(sinks_ny)
            # if sinks_new in [sinks_nx, sinks_ny]:
            if len(sinks_new) > 0:
                node_new = Node(sinks_new)
                self.nodes.add(node_new)

                remaining = remaining - sinks_new

        # Update movable region for each node
        [n.update_movable_region(self.source) for n in self.nodes]
        print("%d nodes are found (N^2=%d).\n" \
              % (len(self.nodes), len(self.sinks)**2))

        # if __debug__:
        print("All the generated nodes:")
        [print("\t%d : %s" % (i + 1, n)) for i, n in enumerate(self.nodes)]
Ejemplo n.º 33
0
    def import_from_imagej(self, path: str):
        """
        Uses read-roi package created by Hadrien Mary.
        https://pypi.org/project/read-roi/

        :param path: Full path to the ImageJ ROIs zip file
        """
        ij_roi = read_imagej(path)
        for k in ij_roi.keys():
            if ij_roi[k]['type'] in ('oval', 'rectangle'):
                width = ij_roi[k]['width']
                height = ij_roi[k]['height']
                left = ij_roi[k]['left']
                top = ij_roi[k]['top']
                if ij_roi[k]['type'] == 'oval':
                    x_bottom = left
                    y_bottom = top + height
                    ps = [x_bottom, y_bottom, width, height]
                    roi = ManualROI.from_ellipse(
                        positions=ps,
                        curve_plot_item=self.get_plot_item(),
                        view_box=self.vi.viewer.getView())
                else:
                    ps = list(
                        iter_product((left, left + width),
                                     (top, top + height)))
                    ps[2], ps[3] = ps[3], ps[
                        2]  #swap coordinates so it draws coordinates in sequence
            elif (ij_roi[k]['type'] == 'freehand'):
                #freehand ROIs have large number of datapoints, so reducing it by a fifth
                xs = ij_roi[k]['x'][::5]
                ys = ij_roi[k]['y'][::5]
                ps = list(zip(xs, ys))
            else:
                xs = ij_roi[k]['x']
                ys = ij_roi[k]['y']
                ps = list(zip(xs, ys))
            if ij_roi[k]['type'] != 'oval':
                roi = ManualROI.from_positions(
                    positions=ps,
                    curve_plot_item=self.get_plot_item(),
                    view_box=self.vi.viewer.getView())
            self.roi_list.append(roi)
        self.roi_list.reindex_colormap()
Ejemplo n.º 34
0
def compute_likelihoods(catalog_file,
                        out_file,
                        cosmo_dict,
                        nmodes,
                        dtheta,
                        n_z,
                        sigma=0.3,
                        RAmin = None, DECmin = None,
                        NRA = None, NDEC = None,
                        remove_z_problem=True,
                        fiducial_cosmology = None,
                        **kwargs):
    """
    compute the likelihoods for a range of cosmologies

    Parameters
    ----------
    catalog_file : string or list of strings
        location of the COSMOS catalog(s) to use
    out_file : string
        location to save likelihood output
        Output will be a text file, with columns labeled
    cosmo_dict : Dictionary
        keys are arguments for cosmology object
        values are the corresponding range
    nmodes : 
        number of modes to use.  This should be less than NRA*NDEC
        alternatively, a list of integers can be supplied
    dtheta : float
        size of (square) pixels in arcmin
    sigma : float (default = 0.3)
        intrinsic ellipticity of galaxies
    fiducial_cosmology : Cosmology object
        the fiducial cosmology used for determination of KL vectors
        if unspecified, it will be initialized from remaining kwargs

    Other Parameters
    ----------------
    n_z : `shear_KL_source.zdist.zdist` object
        n_z(z) returns the galaxy distribution
            
    If the following are unspecified, they will be determined from the data
    RAmin/DECmin : float
        minimum of RA/DEC bins (degrees).
    NRA/NDEC : int
        number of RA/DEC bins
    """
    gamma, Ngal, noise, RArange, DECrange = get_gamma_vector(
        catalog_file, dtheta, RAmin, DECmin, NRA, NDEC, N_bootstraps=10,
        remove_z_problem=True)
    
    if fiducial_cosmology is None:
        fiducial_cosmology = Cosmology(**kwargs)

    # use results of bootstrap resampling to compute
    # the noise on observed shear
    gamma = gamma.reshape(gamma.size)
    Ngal = Ngal.reshape(Ngal.size)
    noise = noise.reshape(noise.size)

    i_sigma = np.where(Ngal > 1)
    sigma_estimate = np.sqrt(np.mean(noise[i_sigma] * Ngal[i_sigma]))
    print "average sigma: %.2g" % sigma_estimate

    izero = np.where(Ngal <= 1)
    noise[izero] = sigma_estimate ** 2
    N_m_onehalf = noise ** -0.5

    # noise = sigma^2 / Ngal
    # correlation matrix takes a constant sigma and a variable
    # ngal. So we'll encode the noise as an "effective Ngal"
    # using the user-defined sigma.
    Ngal_eff = sigma**2 / noise
    Ngal_eff[np.where(Ngal == 0)] = 0

    # construct fiducial correlation matrix
    print ">> fiducial correlation matrix"
    R_fid = shear_correlation_matrix(sigma, RArange, DECrange, Ngal_eff,
                                     n_z,
                                     whiten=True,
                                     cosmo=fiducial_cosmology)

    evals, evecs = np.linalg.eigh(R_fid)
    isort = np.argsort(evals)[::-1]
    evals = evals[isort]
    evecs = evecs[:,isort]

    #compute KL transform of data
    a_data = np.dot(evecs.T, N_m_onehalf * gamma)

    #iterate through all nmodes requested
    if not hasattr(nmodes, '__iter__'):
        nmodes = [nmodes]

    cosmo_keys = cosmo_dict.keys()
    cosmo_vals = [cosmo_dict[k] for k in cosmo_keys]
    cosmo_kwargs = fiducial_cosmology.get_dict()
    
    log2pi = np.log(2*np.pi)

    OF = open(out_file, 'w')
    OF.write('# fiducial cosmology: %s\n' % str(cosmo_kwargs))
    OF.write('# ncut ')
    OF.write(' '.join(cosmo_keys))
    OF.write(' chi2 log|det(C)| log(Likelihood)\n')

    for cosmo_tup in iter_product(*cosmo_vals):
        cosmo_kwargs.update(dict(zip(cosmo_keys,cosmo_tup)))

        #flat universe prior
        cosmo_kwargs['Ol'] = 1. - cosmo_kwargs['Om']
        
        print ">>", cosmo_keys, ['%.2g' % v for v in cosmo_tup]
        R = shear_correlation_matrix(sigma, RArange, DECrange, Ngal_eff,
                                     n_z,
                                     whiten=True,
                                     **cosmo_kwargs)
        cosmo_args = (len(cosmo_keys) * " %.6g") % cosmo_tup
        for ncut in nmodes:
            evecs_n = evecs[:,:ncut]
            a_n = a_data[:ncut]
            C_n = np.dot(evecs_n.T, np.dot(R, evecs_n))

            # compute chi2 = (a_n-<a_n>)^T C_n^-1 (a_n-<a_n>)
            # model predicts <a> = 0 so this simplifies:
            chi2_raw = np.dot(a_n.conj(), np.linalg.solve(C_n, a_n))

            #chi2_raw is complex because a_n is complex.  The imaginary
            # part of chi2 should be zero (within machine precision), because
            # C_n is Hermitian.  We'll skip checking that this is the case.
            chi2 = chi2_raw.real
            s, logdetC = np.linalg.slogdet(C_n)
            
            X0 = -0.5 * ncut * log2pi
            X1 = -0.5 * logdetC
            X2 = -0.5 * chi2
            print chi2, logdetC, X0, X1, X2
            OF.write("%i %s %.6g %.6g %.6g\n" % (ncut, cosmo_args, chi2,
                                                 logdetC, X0+X1+X2))
        ###
    ###
    OF.close()