Exemple #1
0
def calculate_aic(eigenworms_matrix_path, shapes_file, coiled_modes_file,
                  num_modes):
    eigenworms_matrix = np.loadtxt(eigenworms_matrix_path,
                                   delimiter=",").astype(np.float32)

    # Load angle library
    f = scipy.io.loadmat(shapes_file)
    thetas_w = ma.array(f["theta_ensemble"])
    thetas_w[thetas_w == 0] = ma.masked
    thetas_library_raw = ma.compress_rows(ma.vstack(thetas_w))
    raw_samples = thetas_library_raw[::2]

    # Load coiled modes library
    with h5py.File(coiled_modes_file, "r") as mat:
        refs = list(mat["#refs#"].keys())[1:]
        tseries_w = [
            ma.masked_invalid(np.array(mat["#refs#"][ref]).T)[:, :num_modes]
            for ref in refs
        ]

    modes_library = ma.compress_rows(ma.vstack(tseries_w))
    # find indices with larger curvature (on the tail of the distribution of angles that can be solved)
    indices_curved = np.abs(modes_library[:, 2]) > np.percentile(
        raw_samples.dot(eigenworms_matrix[:, 2]), 95)
    curved_samples = modes_library[indices_curved].dot(
        eigenworms_matrix[:, :num_modes].T)

    # combine samples
    thetas_library_combined = np.vstack((curved_samples, raw_samples))

    # sample uniformly from various degrees of curvature
    indices = _uniform_samples(
        thetas_library_combined.dot(eigenworms_matrix[:, 2]))

    training_data = thetas_library_combined[indices]

    aic = []
    n_components_range = np.arange(150, 350, 10)
    for n_components in n_components_range:
        # Fit a Gaussian mixture with EM
        try:
            gmm = GaussianMixture(n_components=n_components)
            gmm.fit(training_data)
            aic.append(gmm.aic(training_data))
        except:
            aic.append(np.nan)

    return np.vstack((n_components_range, aic)).T
Exemple #2
0
def _calculate_values(function, points, dimension, cell_mask=None):
    """Calculate function values at given reference points

    :arg function: function to be sampled
    :arg points: points to be sampled in reference space
    :arg cell_mask: Masks for cell node list
    """
    import numpy.ma as ma
    function_space = function.function_space()
    keys = {1: (0,),
            2: (0, 0)}
    fiat_element = create_element(function_space.ufl_element(), vector_is_mixed=False)
    elem = fiat_element.tabulate(0, points)[keys[dimension]]
    cell_node_list = function_space.cell_node_list
    if cell_mask is not None:
        cell_mask = np.tile(cell_mask.reshape(-1, 1), cell_node_list.shape[1])
        cell_node_list = ma.compress_rows(ma.masked_array(cell_node_list,
                                                          mask=cell_mask))
    data = function.dat.data_ro[cell_node_list]
    if function.ufl_shape == ():
        vec_length = 1
    else:
        vec_length = function.ufl_shape[0]
    if vec_length == 1:
        data = np.reshape(data, data.shape+(1, ))
    return np.einsum("ijk,jl->ilk", data, elem)
 def norm_smooth3(self, x, smorder=False):
     # A version of smooth3x that is designed to simply return a smoothed
     # version of the passed array without, altering any execution state
     # variables or objects. Was made to solve a problem in whole_spec_plot
     # where an array seprate from self.sm was needed.
     if smorder == False: order = self.order
     else: order = smorder
     x = ma.compress_rows(x)
     nelm = np.shape(x)[0]
     sm = np.zeros((int(math.floor(nelm / 3)), 2))
     for i in range(int(math.floor(nelm / 3))):
         n = 3 * i
         sm[i, 0] = (x[n, 0] + x[n + 1, 0] + x[n + 2, 0]) / 3
         sm[i, 1] = (x[n, 1] + x[n + 1, 1] + x[n + 2, 1]) / 3
     return sm
     if self.state['trimmed'][order] == True:
         masked = np.empty(np.shape(self.sm[order]))
         masked.fill(False)
         for r in range(np.shape(self.spec_trim_points[order])[0]):
             start = self.spec_trim_points[order][r, 0]
             end = self.spec_trim_points[order][r, 1]
             mask = np.empty(np.shape(self.sm[order]))
             mask[:, 0] = np.logical_and(self.sm[order][:, 0] >= start,
                                         self.sm[order][:, 0] <= end)
             mask[:, 1] = np.logical_and(self.sm[order][:, 0] >= start,
                                         self.sm[order][:, 0] <= end)
             maskednew1 = np.where(mask[:, 0] == True)
             maskednew2 = np.where(mask[:, 1] == True)
             masked[maskednew1, 0] = True
             masked[maskednew2, 1] = True
         #self.sm[order] = ma.masked_array(self.sm[order],
         #                          mask=masked)
         sm = ma.masked_array(self.sm[order], mask=masked)
         return sm
 def smooth3(self, x, smorder=False):
     # the order keyword and acompanying if statement allow the function be
     # called on a specific order (as in plotting) or on the current order
     # specified by state (as during interacvie normalization).
     if smorder == False: order = self.order
     else: order = smorder
     x = ma.compress_rows(x)
     nelm = np.shape(x)[0]
     self.sm[order] = np.zeros((int(math.floor(nelm / 3)), 2))
     sm = self.sm[order]
     for i in range(int(math.floor(nelm / 3))):
         n = 3 * i
         sm[i, 0] = (x[n, 0] + x[n + 1, 0] + x[n + 2, 0]) / 3
         sm[i, 1] = (x[n, 1] + x[n + 1, 1] + x[n + 2, 1]) / 3
     #self.sm[order] = sm
     self.state['w_smoothed'][order] = True
     if self.state['trimmed'][order] == True:
         masked = np.empty(np.shape(self.sm[order]))
         masked.fill(False)
         for r in range(np.shape(self.spec_trim_points[order])[0]):
             start = self.spec_trim_points[order][r, 0]
             end = self.spec_trim_points[order][r, 1]
             mask = np.empty(np.shape(self.sm[order]))
             mask[:, 0] = np.logical_and(self.sm[order][:, 0] >= start,
                                         self.sm[order][:, 0] <= end)
             mask[:, 1] = np.logical_and(self.sm[order][:, 0] >= start,
                                         self.sm[order][:, 0] <= end)
             maskednew1 = np.where(mask[:, 0] == True)
             maskednew2 = np.where(mask[:, 1] == True)
             masked[maskednew1, 0] = True
             masked[maskednew2, 1] = True
         self.sm[order] = ma.masked_array(self.sm[order], mask=masked)
Exemple #5
0
def _calculate_values(function, points, dimension, cell_mask=None):
    """Calculate function values at given reference points

    :arg function: function to be sampled
    :arg points: points to be sampled in reference space
    :arg cell_mask: Masks for cell node list
    """
    import numpy.ma as ma
    function_space = function.function_space()
    keys = {1: (0, ), 2: (0, 0)}
    # Such tabulation could be done with FInAT, but that would be more
    # complicated, and there is no clear benefit to changing.
    fiat_element = create_base_element(
        function_space.ufl_element()).fiat_equivalent
    elem = fiat_element.tabulate(0, points)[keys[dimension]]
    cell_node_list = function_space.cell_node_list
    if cell_mask is not None:
        cell_mask = np.tile(cell_mask.reshape(-1, 1), cell_node_list.shape[1])
        cell_node_list = ma.compress_rows(
            ma.masked_array(cell_node_list, mask=cell_mask))
    data = function.dat.data_ro[cell_node_list]
    if function.ufl_shape == ():
        vec_length = 1
    else:
        vec_length = function.ufl_shape[0]
    if vec_length == 1:
        data = np.reshape(data, data.shape + (1, ))
    return np.einsum("ijk,jl->ilk", data, elem)
Exemple #6
0
def _calculate_values(function, points, dimension, cell_mask=None):
    """Calculate function values at given reference points

    :arg function: function to be sampled
    :arg points: points to be sampled in reference space
    :arg cell_mask: Masks for cell node list
    """
    import numpy.ma as ma
    function_space = function.function_space()
    keys = {1: (0,),
            2: (0, 0)}
    elem = function_space.fiat_element.tabulate(0, points)[keys[dimension]]
    cell_node_list = function_space.cell_node_list
    if cell_mask is not None:
        cell_mask = np.tile(cell_mask.reshape(-1, 1), cell_node_list.shape[1])
        cell_node_list = ma.compress_rows(ma.masked_array(cell_node_list,
                                                          mask=cell_mask))
    data = function.dat.data_ro[cell_node_list]
    if function.ufl_shape == ():
        vec_length = 1
    else:
        vec_length = function.ufl_shape[0]
    if vec_length == 1:
        data = np.reshape(data, data.shape+(1, ))
    return np.einsum("ijk,jl->ilk", data, elem)
 def norm_smooth3(self, x, smorder=False):
     # A version of smooth3x that is designed to simply return a smoothed
     # version of the passed array without, altering any execution state
     # variables or objects. Was made to solve a problem in whole_spec_plot
     # where an array seprate from self.sm was needed.
     if smorder == False: order=self.order
     else: order = smorder
     x = ma.compress_rows(x)
     nelm = np.shape(x)[0]
     sm = np.zeros( (int(math.floor(nelm/3)),2) )
     for i in range(int(math.floor(nelm/3))):
       n=3*i
       sm[i,0] = ( x[n,0] + x[n+1,0] + x[n+2,0] ) / 3
       sm[i,1] = ( x[n,1] + x[n+1,1] + x[n+2,1] ) / 3
     return sm
     if self.state['trimmed'][order] == True:
         masked = np.empty(np.shape(self.sm[order]))
         masked.fill(False)
         for r in range(np.shape(self.spec_trim_points[order])[0]):
             start = self.spec_trim_points[order][r,0]
             end = self.spec_trim_points[order][r,1]
             mask = np.empty(np.shape(self.sm[order]))
             mask[:,0] = np.logical_and(self.sm[order][:,0] >= start,
                                        self.sm[order][:,0] <= end)
             mask[:,1] = np.logical_and(self.sm[order][:,0] >= start,
                                        self.sm[order][:,0] <= end)
             maskednew1 = np.where(mask[:,0] == True)
             maskednew2 = np.where(mask[:,1] == True)
             masked[maskednew1,0] = True
             masked[maskednew2,1] = True
         #self.sm[order] = ma.masked_array(self.sm[order],
                         #                          mask=masked)
         sm = ma.masked_array(self.sm[order],mask=masked)
         return sm
 def smooth3(self, x, smorder=False):
     # the order keyword and acompanying if statement allow the function be
     # called on a specific order (as in plotting) or on the current order
     # specified by state (as during interacvie normalization).
     if smorder == False: order=self.order
     else: order = smorder
     x = ma.compress_rows(x)
     nelm = np.shape(x)[0]
     self.sm[order] = np.zeros( (int(math.floor(nelm/3)),2) )
     sm = self.sm[order]
     for i in range(int(math.floor(nelm/3))):
       n=3*i
       sm[i,0] = ( x[n,0] + x[n+1,0] + x[n+2,0] ) / 3
       sm[i,1] = ( x[n,1] + x[n+1,1] + x[n+2,1] ) / 3
     #self.sm[order] = sm
     self.state['w_smoothed'][order] = True
     if self.state['trimmed'][order] == True:
         masked = np.empty(np.shape(self.sm[order]))
         masked.fill(False)
         for r in range(np.shape(self.spec_trim_points[order])[0]):
             start = self.spec_trim_points[order][r,0]
             end = self.spec_trim_points[order][r,1]
             mask = np.empty(np.shape(self.sm[order]))
             mask[:,0] = np.logical_and(self.sm[order][:,0] >= start,
                                        self.sm[order][:,0] <= end)
             mask[:,1] = np.logical_and(self.sm[order][:,0] >= start,
                                        self.sm[order][:,0] <= end)
             maskednew1 = np.where(mask[:,0] == True)
             maskednew2 = np.where(mask[:,1] == True)
             masked[maskednew1,0] = True
             masked[maskednew2,1] = True
         self.sm[order] = ma.masked_array(self.sm[order],
                                                   mask=masked)
Exemple #9
0
def generate(shapes_file, coiled_modes_file, eigenworms_matrix_path, out_file, num_gaussians):
    # Load angle library from Greg
    f = scipy.io.loadmat(shapes_file)
    thetas_w = ma.array(f["theta_ensemble"])
    thetas_w[thetas_w == 0] = ma.masked
    thetas_library_raw = ma.compress_rows(ma.vstack(thetas_w))

    # Load library from Onno
    mat = h5py.File(coiled_modes_file, "r")
    refs = list(mat["#refs#"].keys())[1:]
    tseries_w = [ma.masked_invalid(np.array(mat["#refs#"][ref]).T)[:, :5] for ref in refs]
    mat.close()
    modes_library = ma.compress_rows(ma.vstack(tseries_w))

    eigenworms_matrix = np.loadtxt(eigenworms_matrix_path, delimiter=",").astype(np.float32)

    # same number of samples from full theta
    # raw_samples = thetas_library_raw[np.random.choice(np.arange(len(thetas_library_raw)),np.sum(indices_curved),replace=False)]
    raw_samples = thetas_library_raw[::2]

    # find indices with larger curvature
    indices_curved = np.abs(modes_library[:, 2]) > np.percentile(raw_samples.dot(eigenworms_matrix[:, 2]), 97.5)
    # get same number of samples from raw angles and projected modes
    curved_samples = modes_library[indices_curved].dot(eigenworms_matrix[:, :5].T)

    thetas_library_combined = np.vstack((curved_samples, raw_samples))

    indices = uniform_samples(thetas_library_combined.dot(eigenworms_matrix[:, 2]))
    training_data = thetas_library_combined[indices]

    # fit gaussian mixture model
    gmm = GaussianMixture(n_components=num_gaussians)
    gmm.fit(training_data)

    # sort according to curvature
    sorting_indices = np.argsort(np.sum(np.abs(np.diff(gmm.means_, axis=1)), axis=1))
    means = gmm.means_[sorting_indices]
    covariances = gmm.covariances_[sorting_indices]
    weights = gmm.weights_[sorting_indices]

    with gzip.open(out_file, "wt") as f:
        json.dump({"means": means.tolist(), "covariances": covariances.tolist(), "weights": weights.tolist()}, f)
Exemple #10
0
    def __ReturnArrayWithDetectedPixels(self, HSV_Image):

        BinaryArrayOfDetectedPixels = self.__ReturnBinaryArrayOfDetectedPixels(
            HSV_Image)
        ArrayOfAllDetecedPlasticPixels = ma.masked_array(
            (self.HSV_Image), mask=BinaryArrayOfDetectedPixels)

        height, width, channels = HSV_Image.shape
        ArrayOfAllDetecedPlasticPixels = np.reshape(
            ArrayOfAllDetecedPlasticPixels, ((height * width), 3))
        ArrayOfAllDetecedPlasticPixels = ma.compress_rows(
            ArrayOfAllDetecedPlasticPixels)

        return ArrayOfAllDetecedPlasticPixels
Exemple #11
0
def _filter(varname1, varname2, column, value, operator):
    """Generates a numpy mask, masks input array, returns compressed
        output to variables dict"""
    # To do: generate masks programatically given operator input
    # Operators are reversed in mask because true values are masked, not false
    if operator == '==':
        mask = np.repeat(VARIABLES[varname1][:, column] == value,
                         VARIABLES[varname1].shape[1])
    elif operator == '>':
        mask = np.repeat(VARIABLES[varname1][:, column] <= value,
                         VARIABLES[varname1].shape[1])
    elif operator == '>=':
        mask = np.repeat(VARIABLES[varname1][:, column] < value,
                         VARIABLES[varname1].shape[1])
    elif operator == '<':
        mask = np.repeat(VARIABLES[varname1][:, column] >= value,
                         VARIABLES[varname1].shape[1])
    elif operator == '<=':
        mask = np.repeat(VARIABLES[varname1][:, column] > value,
                         VARIABLES[varname1].shape[1])

    VARIABLES[varname2] = ma.compress_rows(
        ma.array(VARIABLES[varname1], mask=mask))
Exemple #12
0
def plot_x(data, outfile, outformat, args, normalize=False):
    """Limit plot as a function of the source flavor ratio for each operator
    texture."""
    print('Making X sensitivity plot')
    dim = args.dimension
    if dim < 5: normalize = False
    srcs = args.source_ratios
    x_arr = np.array([i[0] for i in srcs])
    if args.texture is Texture.NONE:
        textures = [Texture.OEU, Texture.OET, Texture.OUT]
    else:
        textures = [args.texture]

    # Rearrange data structure
    r_data = np.full(
        (data.shape[1], data.shape[0], data.shape[2], data.shape[3]), np.nan)

    for isrc in range(data.shape[0]):
        for itex in range(data.shape[1]):
            r_data[itex][isrc] = data[isrc][itex]
    r_data = ma.masked_invalid(r_data)
    print(r_data.shape, 'r_data.shape')

    fig = plt.figure(figsize=(7, 6))
    ax = fig.add_subplot(111)

    ylims = SCALE_BOUNDARIES[dim]
    if normalize:
        if dim == 5: ylims = (-24, -8)
        if dim == 6: ylims = (-12, 8)
        if dim == 7: ylims = (0, 20)
        if dim == 8: ylims = (12, 36)
    else:
        if dim == 3: ylims = (-28, -22)
        if dim == 4: ylims = (-35, -26)
        if dim == 5: SCALE_BOUNDARIES[5]
    xlims = (0, 1)

    colour = {0: 'red', 2: 'blue', 1: 'green'}
    rgb_co = {0: [1, 0, 0], 2: [0, 0, 1], 1: [0.0, 0.5019607843137255, 0.0]}

    legend_log = []
    legend_elements = []
    labelsize = 13
    largesize = 17

    ax.set_xlim(xlims)
    ax.set_ylim(ylims)
    xticks = [0, 1 / 3., 0.5, 2 / 3., 1]
    # xlabels = [r'$0$', r'$\frac{1}{3}$', r'$\frac{1}{2}$', r'$\frac{2}{3}$', r'$1$']
    xlabels = [r'$0$', r'$1 / 3$', r'$1/2$', r'$2/3$', r'$1$']
    ax.set_xticks([], minor=True)
    ax.set_xticks(xticks, minor=False)
    ax.set_xticklabels(xlabels, fontsize=largesize)
    if dim != 4 or dim != 3:
        yticks = range(ylims[0], ylims[1], 2) + [ylims[1]]
        ax.set_yticks(yticks, minor=False)
    if dim == 3 or dim == 4:
        yticks = range(ylims[0], ylims[1], 1) + [ylims[1]]
        ax.set_yticks(yticks, minor=False)
    # for ymaj in ax.yaxis.get_majorticklocs():
    #     ax.axhline(y=ymaj, ls=':', color='gray', alpha=0.2, linewidth=1)
    for xmaj in xticks:
        if xmaj == 1 / 3.:
            ax.axvline(x=xmaj, ls='--', color='gray', alpha=0.5, linewidth=0.3)
        # else:
        #     ax.axvline(x=xmaj, ls=':', color='gray', alpha=0.2, linewidth=1)

    ax.text((1 / 3.) + 0.01,
            0.01,
            r'$(0.33:0.66:0)_\text{S}$',
            fontsize=labelsize,
            transform=ax.transAxes,
            rotation='vertical',
            va='bottom')
    ax.text(0.96,
            0.01,
            r'$(1:0:0)_\text{S}$',
            fontsize=labelsize,
            transform=ax.transAxes,
            rotation='vertical',
            va='bottom',
            ha='left')
    ax.text(0.01,
            0.01,
            r'$(0:1:0)_\text{S}$',
            fontsize=labelsize,
            transform=ax.transAxes,
            rotation='vertical',
            va='bottom')
    yl = 0.55
    if dim == 3: yl = 0.65
    ax.text(0.03,
            yl,
            r'${\rm \bf Excluded}$',
            fontsize=largesize,
            transform=ax.transAxes,
            color='g',
            rotation='vertical',
            zorder=10)
    ax.text(0.95,
            0.55,
            r'${\rm \bf Excluded}$',
            fontsize=largesize,
            transform=ax.transAxes,
            color='b',
            rotation='vertical',
            zorder=10)

    for itex, tex in enumerate(textures):
        print('|||| TEX = {0}'.format(tex))
        lims = np.full(len(srcs), np.nan)

        for isrc, src in enumerate(srcs):
            x = src[0]
            print('|||| X = {0}'.format(x))
            args.source_ratio = src
            d = r_data[itex][isrc]
            if np.sum(d.mask) > 2: continue
            scales, statistic = ma.compress_rows(d).T
            lim = get_limit(deepcopy(scales),
                            deepcopy(statistic),
                            args,
                            mask_initial=True)
            if lim is None: continue
            if normalize:
                lim -= np.log10(PLANCK_SCALE[dim])
            lims[isrc] = lim

        lims = ma.masked_invalid(lims)
        size = np.sum(~lims.mask)
        if size == 0: continue

        print('x_arr, lims', zip(x_arr, lims))
        if normalize:
            zeropoint = 100
        else:
            zeropoint = 0
        lims[lims.mask] = zeropoint

        l0 = np.argwhere(lims == zeropoint)[0]
        h0 = len(lims) - np.argwhere(np.flip(lims, 0) == zeropoint)[0]
        lims[int(l0):int(h0)] = zeropoint

        x_arr_a = [x_arr[0] - 0.1] + list(x_arr)
        x_arr_a = list(x_arr_a) + [x_arr_a[-1] + 0.1]
        lims = [lims[0]] + list(lims)
        lims = list(lims) + [lims[-1]]

        s = 0.2
        g = 2
        if dim == 3 and tex == Texture.OUT:
            s = 0.4
            g = 4
        if dim in (4, 5) and tex == Texture.OUT:
            s = 0.5
            g = 5
        if dim == 7 and tex == Texture.OET:
            s = 1.6
            g = 2
        if dim == 7 and tex == Texture.OUT:
            s = 2.0
            g = 20
        if dim == 8 and tex == Texture.OET:
            s = 0.8
            g = 6
        if dim == 8 and tex == Texture.OUT:
            s = 1.7
            g = 8

        # ax.scatter(x_arr_a, lims, color='black', s=1)
        tck, u = splprep([x_arr_a, lims], s=0, k=1)
        x, y = splev(np.linspace(0, 1, 200), tck)
        tck, u = splprep([x, y], s=s)
        x, y = splev(np.linspace(0, 1, 400), tck)
        y = gaussian_filter(y, sigma=g)
        ax.fill_between(x, y, zeropoint, color=rgb_co[itex] + [0.3])
        # ax.scatter(x, y, color='black', s=1)
        # ax.scatter(x_arr_a, lims, color=rgb_co[itex], s=8)

        if itex not in legend_log:
            legend_log.append(itex)
            # label = texture_label(tex, dim)[:-1] + r'\:{\rm\:texture}$'
            label = texture_label(tex, dim)[:-1] + r'\:({\rm this\:work})$'
            legend_elements.append(
                Patch(facecolor=rgb_co[itex] + [0.3],
                      edgecolor=rgb_co[itex] + [1],
                      label=label))

    LV_lim = np.log10(LV_ATMO_90PC_LIMITS[dim])
    if normalize:
        LV_lim -= np.log10(PLANCK_SCALE[dim])
    ax.add_patch(
        patches.Rectangle((xlims[0], LV_lim[1]),
                          np.diff(xlims),
                          LV_lim[0] - LV_lim[1],
                          fill=False,
                          hatch='\\\\'))

    if dim in PLANCK_SCALE:
        ps = np.log10(PLANCK_SCALE[dim])
        if normalize and dim == 6:
            ps -= np.log10(PLANCK_SCALE[dim])
            ax.add_patch(
                Arrow(0.24,
                      -0.009,
                      0,
                      -5,
                      width=0.12,
                      capstyle='butt',
                      facecolor='purple',
                      fill=True,
                      alpha=0.8,
                      edgecolor='darkmagenta'))
            ax.add_patch(
                Arrow(0.78,
                      -0.009,
                      0,
                      -5,
                      width=0.12,
                      capstyle='butt',
                      facecolor='purple',
                      fill=True,
                      alpha=0.8,
                      edgecolor='darkmagenta'))

            ax.text(0.26,
                    0.5,
                    r'${\rm \bf Quantum\:Gravity\:Frontier}$',
                    fontsize=largesize - 2,
                    transform=ax.transAxes,
                    va='top',
                    ha='left',
                    color='purple')
        if dim > 5:
            ax.axhline(y=ps, color='purple', alpha=1., linewidth=1.5)

    cpt = r'c' if dim % 2 == 0 else r'a'
    if normalize:
        ft = r'${\rm New\:Physics\:Scale}\:[\:{\rm log}_{10} \left (\mathring{'+cpt+r'}^{(' + \
                r'{0}'.format(args.dimension)+r')}\cdot{\rm E}_{\:\rm P}'
        if dim > 5: ft += r'^{\:' + r'{0}'.format(args.dimension - 4) + r'}'
        ft += r'\right )\: ]$'
        fig.text(0.01,
                 0.5,
                 ft,
                 ha='left',
                 va='center',
                 rotation='vertical',
                 fontsize=largesize)
    else:
        fig.text(
            0.01,
            0.5,
            r'${\rm New\:Physics\:Scale}\:[\:{\rm log}_{10} \left (\mathring{'
            + cpt + r'}^{(' + r'{0}'.format(args.dimension) + r')}\:' +
            get_units(args.dimension) + r'\right )\: ]$',
            ha='left',
            va='center',
            rotation='vertical',
            fontsize=largesize)

    ax.set_xlabel(
        r'${\rm Source\:Composition}\:[\:\left (\:x:1-x:0\:\right )_\text{S}\:]$',
        labelpad=10,
        fontsize=largesize)
    ax.tick_params(axis='x', labelsize=largesize - 1)

    purple = [0.5019607843137255, 0.0, 0.5019607843137255]
    # legend_elements.append(
    #     Patch(facecolor=purple+[0.7], edgecolor=purple+[1], label='Planck Scale Expectation')
    # )
    legend_elements.append(
        Patch(facecolor='none',
              hatch='\\\\',
              edgecolor='k',
              label='IceCube [TODO]'))
    legend = ax.legend(handles=legend_elements,
                       prop=dict(size=labelsize - 2),
                       loc='upper center',
                       title='Excluded regions',
                       framealpha=1.,
                       edgecolor='black',
                       frameon=True,
                       bbox_to_anchor=(0.5, 1))
    plt.setp(legend.get_title(), fontsize=labelsize)
    legend.get_frame().set_linestyle('-')

    # ybound = 0.14
    # if args.data is DataType.REAL:
    #     fig.text(0.7, ybound, r'\bf IceCube Preliminary', color='red', fontsize=13,
    #              ha='center', va='center', zorder=11)
    # elif args.data is DataType.REALISATION:
    #     fig.text(0.7, ybound-0.05, r'\bf IceCube Simulation', color='red', fontsize=13,
    #              ha='center', va='center', zorder=11)
    # else:
    #     fig.text(0.7, ybound, r'\bf IceCube Simulation', color='red', fontsize=13,
    #              ha='center', va='center', zorder=11)

    make_dir(outfile)
    for of in outformat:
        print('Saving plot as {0}'.format(outfile + '.' + of))
        fig.savefig(outfile + '.' + of, bbox_inches='tight', dpi=150)
Exemple #13
0
def plot_table_sens(data, outfile, outformat, args, show_lvatmo=True):
    print('Making TABLE sensitivity plot')
    argsc = deepcopy(args)

    dims = args.dimensions
    srcs = args.source_ratios
    if args.texture is Texture.NONE:
        textures = [Texture.OET, Texture.OUT]
    else:
        textures = [args.texture]

    if len(srcs) > 3:
        raise NotImplementedError

    xlims = (-60, -20)
    ylims = (0.5, 1.5)

    colour = {2: 'red', 1: 'blue', 0: 'green'}
    rgb_co = {2: [1, 0, 0], 1: [0, 0, 1], 0: [0.0, 0.5019607843137255, 0.0]}

    fig = plt.figure(figsize=(8, 6))
    gs = gridspec.GridSpec(len(dims), 1)
    gs.update(hspace=0.15)

    first_ax = None
    legend_log = []
    legend_elements = []

    for idim, dim in enumerate(dims):
        print('|||| DIM = {0}'.format(dim))
        argsc.dimension = dim
        gs0 = gridspec.GridSpecFromSubplotSpec(len(textures),
                                               1,
                                               subplot_spec=gs[idim],
                                               hspace=0)

        for itex, tex in enumerate(textures):
            argsc.texture = tex
            ylabel = texture_label(tex, dim)
            # if angles == 2 and ian == 0: continue
            ax = fig.add_subplot(gs0[itex])

            if first_ax is None:
                first_ax = ax

            ax.set_xlim(xlims)
            ax.set_ylim(ylims)
            ax.set_yticks([], minor=True)
            ax.set_yticks([1.], minor=False)
            ax.set_yticklabels([ylabel], fontsize=13)
            ax.yaxis.tick_right()
            for xmaj in ax.xaxis.get_majorticklocs():
                ax.axvline(x=xmaj,
                           ls=':',
                           color='gray',
                           alpha=0.2,
                           linewidth=1)
            ax.get_xaxis().set_visible(False)
            if itex == len(textures) - 2:
                ax.spines['bottom'].set_alpha(0.6)
            elif itex == len(textures) - 1:
                ax.text(-0.04,
                        1.0,
                        '$d = {0}$'.format(dim),
                        fontsize=16,
                        rotation='90',
                        verticalalignment='center',
                        transform=ax.transAxes)
                # dim_label_flag = False
                ax.spines['top'].set_alpha(0.6)
                ax.spines['bottom'].set_alpha(0.6)

            for isrc, src in enumerate(srcs):
                print('== src', src)
                argsc.source_ratio = src

                if dim in PLANCK_SCALE.iterkeys():
                    ps = np.log10(PLANCK_SCALE[dim])
                    if ps < xlims[0]:
                        ax.annotate(s='',
                                    xy=(xlims[0], 1),
                                    xytext=(xlims[0] + 1, 1),
                                    arrowprops={
                                        'arrowstyle': '->, head_length=0.2',
                                        'lw': 1,
                                        'color': 'purple'
                                    })
                    elif ps > xlims[1]:
                        ax.annotate(s='',
                                    xy=(xlims[1] - 1, 1),
                                    xytext=(xlims[1], 1),
                                    arrowprops={
                                        'arrowstyle': '<-, head_length=0.2',
                                        'lw': 1,
                                        'color': 'purple'
                                    })
                    else:
                        ax.axvline(x=ps,
                                   color='purple',
                                   alpha=1.,
                                   linewidth=1.5)

                try:
                    scales, statistic = ma.compress_rows(
                        data[idim][isrc][itex]).T
                except:
                    continue
                lim = get_limit(deepcopy(scales),
                                deepcopy(statistic),
                                argsc,
                                mask_initial=True)
                if lim is None: continue

                ax.axvline(x=lim, color=colour[isrc], alpha=1., linewidth=1.5)
                ax.add_patch(
                    patches.Rectangle((lim, ylims[0]),
                                      100,
                                      np.diff(ylims),
                                      fill=True,
                                      facecolor=colour[isrc],
                                      alpha=0.3,
                                      linewidth=0))

                if isrc not in legend_log:
                    legend_log.append(isrc)
                    label = r'$\left('+r'{0}'.format(solve_ratio(src)).replace('_',':')+ \
                            r'\right)_{\text{S}}\:\text{at\:source}$'
                    legend_elements.append(
                        Patch(facecolor=rgb_co[isrc] + [0.3],
                              edgecolor=rgb_co[isrc] + [1],
                              label=label))

            if itex == len(textures) - 1 and show_lvatmo:
                LV_lim = np.log10(LV_ATMO_90PC_LIMITS[dim])
                ax.add_patch(
                    patches.Rectangle((LV_lim[1], ylims[0]),
                                      LV_lim[0] - LV_lim[1],
                                      np.diff(ylims),
                                      fill=False,
                                      hatch='\\\\'))

    ax.get_xaxis().set_visible(True)
    ax.set_xlabel(
        r'${\rm New\:Physics\:Scale}\:[\:{\rm log}_{10} (\Lambda^{-1}_{d}\:/\:{\rm GeV}^{-d+4})\: ]$',
        labelpad=5,
        fontsize=19)
    ax.tick_params(axis='x', labelsize=16)

    purple = [0.5019607843137255, 0.0, 0.5019607843137255]
    if show_lvatmo:
        legend_elements.append(
            Patch(facecolor='none',
                  hatch='\\\\',
                  edgecolor='k',
                  label='IceCube, Nature.Phy.14,961(2018)'))
    legend_elements.append(
        Patch(facecolor=purple + [0.7],
              edgecolor=purple + [1],
              label='Planck Scale Expectation'))
    legend = first_ax.legend(handles=legend_elements,
                             prop=dict(size=11),
                             loc='upper left',
                             title='Excluded regions',
                             framealpha=1.,
                             edgecolor='black',
                             frameon=True)
    first_ax.set_zorder(10)
    plt.setp(legend.get_title(), fontsize='11')
    legend.get_frame().set_linestyle('-')

    ybound = 0.595
    if args.data is DataType.REAL:
        # fig.text(0.295, 0.684, 'IceCube Preliminary', color='red', fontsize=13,
        fig.text(0.278,
                 ybound,
                 r'\bf IceCube Preliminary',
                 color='red',
                 fontsize=13,
                 ha='center',
                 va='center',
                 zorder=11)
    elif args.data is DataType.REALISATION:
        fig.text(0.278,
                 ybound - 0.05,
                 r'\bf IceCube Simulation',
                 color='red',
                 fontsize=13,
                 ha='center',
                 va='center',
                 zorder=11)
    else:
        fig.text(0.278,
                 ybound,
                 r'\bf IceCube Simulation',
                 color='red',
                 fontsize=13,
                 ha='center',
                 va='center',
                 zorder=11)

    make_dir(outfile)
    for of in outformat:
        print('Saving plot as {0}'.format(outfile + '.' + of))
        fig.savefig(outfile + '.' + of, bbox_inches='tight', dpi=150)
Exemple #14
0
def plot_statistic(data, outfile, outformat, args, scale_param, label=None):
    """Make MultiNest factor or LLH value plot."""
    print('Making Statistic plot')
    fig_text = gen_figtext(args)
    if label is not None: fig_text += '\n' + label

    print('data', data)
    print('data.shape', data.shape)
    print('outfile', outfile)
    try:
        scales, statistic = ma.compress_rows(data).T
        lim = get_limit(deepcopy(scales),
                        deepcopy(statistic),
                        args,
                        mask_initial=True)
        tck, u = splprep([scales, statistic], s=0)
    except:
        return
    sc, st = splev(np.linspace(0, 1, 1000), tck)
    scales_rm = sc[sc >= scales[1]]
    statistic_rm = st[sc >= scales[1]]

    min_idx = np.argmin(scales)
    null = statistic[min_idx]
    # fig_text += '\nnull lnZ = {0:.2f}'.format(null)

    if args.stat_method is StatCateg.BAYESIAN:
        reduced_ev = -(statistic_rm - null)
    elif args.stat_method is StatCateg.FREQUENTIST:
        reduced_ev = -2 * (statistic_rm - null)

    fig = plt.figure(figsize=(7, 5))
    ax = fig.add_subplot(111)

    xlims = SCALE_BOUNDARIES[args.dimension]
    ax.set_xlim(xlims)
    ax.set_xlabel(r'${\rm log}_{10}\left[\Lambda^{-1}_{'+ \
                  r'{0}'.format(args.dimension)+r'}'+ \
                  get_units(args.dimension)+r'\right]$', fontsize=16)

    if args.stat_method is StatCateg.BAYESIAN:
        ax.set_ylabel(
            r'$\text{Bayes\:Factor}\:\left[\text{ln}\left(B_{0/1}\right)\right]$'
        )
    elif args.stat_method is StatCateg.FREQUENTIST:
        ax.set_ylabel(r'$-2\Delta {\rm LLH}$')

    # ymin = np.round(np.min(reduced_ev) - 1.5)
    # ymax = np.round(np.max(reduced_ev) + 1.5)
    # ax.set_ylim((ymin, ymax))

    ax.scatter(scales[1:], -(statistic[1:] - null), color='r')
    ax.plot(scales_rm, reduced_ev, color='k', linewidth=1, alpha=1, ls='-')

    if args.stat_method is StatCateg.BAYESIAN:
        ax.axhline(y=np.log(10**(BAYES_K)),
                   color='red',
                   alpha=1.,
                   linewidth=1.2,
                   ls='--')
        ax.axvline(x=lim, color='red', alpha=1., linewidth=1.2, ls='--')

    at = AnchoredText(fig_text, prop=dict(size=10), frameon=True, loc=4)
    at.patch.set_boxstyle("round,pad=0.1,rounding_size=0.5")
    ax.add_artist(at)
    make_dir(outfile)
    for of in outformat:
        print('Saving as {0}'.format(outfile + '.' + of))
        fig.savefig(outfile + '.' + of, bbox_inches='tight', dpi=150)
Exemple #15
0
data = [[ np.nan if cell is '?' else float(cell)
          for cell in row[DISCARD:]] for row in datareader]


# attributeNames: a Mx1 matrix
attributeNames = namereader.next()[DISCARD:] # only one row in file so pop from iterable

# X: data matrix, rows correspond to N data objects, each of which contains M attributes
X = ma.masked_invalid(data) # missing values are masked

X_mean = X.mean(0)[np.newaxis,:] # every value is the attributes mean

# preprocesses X
if INVALID_FIX is ELIM_COMM:
	# removes data objects with missing values
	X = ma.compress_rows(X)
	titlestr += "Rows with missings removed. "
	filestr += 'rows-with-missings-removed_'
elif INVALID_FIX is ELIM_ATTR:
	# removes attributes with missing values
	X = ma.compress_cols(X)
	titlestr += "Attributes with missings removed. "
	filestr += 'attr-with-missings-removed_'
elif INVALID_FIX is FILL_MEAN:
	# takes mean where masked value otherwise
	X = np.where(X.mask, X_mean, X)
	titlestr += "Missings filled with means. "
	filestr += "missings-filled-w-means_"

X_mean = X.mean(0)[np.newaxis,:] # recalculate mean
Exemple #16
0
def two_dim_findpeaks(image, peak_width=None, sigma=None, alpha=2, 
                      medfilt_radius=3, max_peak_number=50000, coords_list=[],
                      recursion_level=0, max_peak_width=100):
    """
    
    """
    from copy import deepcopy    
    # do a 2D median filter
    if medfilt_radius > 0:
        image = medfilt(image,medfilt_radius)    
    if peak_width is None:
        peak_width = estimate_peak_width(image,max_peak_width=max_peak_width)
        # break recursion if peak width estimation fails
        if peak_width == 0:
            print "peak width estimated as 0.  Skipping iteration."
            return
        print "Estimated peak width as %d pixels"%peak_width
    # draw a circular mask to be used around peaks
    coords = np.zeros((max_peak_number,2))
    image_temp = deepcopy(image)
    peak_ct=0
    # plus 3 is to black out a slightly larger area than the measured 
    #   size of the peak so that we don't get edges of peaks turning into 
    #   false peaks.
    #size=peak_width/2+3
    size=peak_width/2
    mask = draw_mask((size*2,size*2), size, [(size,size)])
    # invert the mask to chuck the peak, not the surrounding area.
    mask = mask==0
    if sigma is None:
        # peaks are some number of standard deviations from mean
        sigma=np.std(image_temp)
        # peaks are some set fraction of the max peak height
        #sigma = np.min(image)+0.2*(np.max(image)-np.min(image))
    while True:
        k = np.argmax(image_temp)
        j,i = np.unravel_index(k, image_temp.shape)
        if(image_temp[j,i] >= alpha*sigma):
            # store the coordinate
            coords[peak_ct]=[i,j]
            # set the neighborhood of the peak to zero so we go look elsewhere
            #  for other peaks
            x = np.arange(i-size, i+size)
            y = np.arange(j-size, j+size)
            xv,yv = np.meshgrid(x,y)
            image_temp[yv.clip(0,image_temp.shape[0]-1),
                                   xv.clip(0,image_temp.shape[1]-1) ] *= mask
            peak_ct+=1
        else:
            break
    
    coords = coords[:peak_ct]
    # add in the heights and peak width
    heights=np.array([[image[coords[i,1],coords[i,0]],peak_width] for i in xrange(coords.shape[0])]).reshape((-1,2))
    sigma = np.std(heights[:,0])
    mean = np.mean(heights[:,0])
    # filter out junk peaks - anything beyond 5 sigma.
    heights[:,0] = ma.masked_outside(heights[:,0], mean-(5*sigma), mean+(5*sigma))
    coords=np.hstack((coords,heights))
    coords=ma.compress_rows(coords)
    sigma=np.std(image_temp)
    # smaller fudge factor makes it more sensitive to junk.
    fudge_factor=1.5
    coords_list.append(coords)
    print "Iteration %d done.  %d peaks found." %(recursion_level,coords.shape[0])
    if image_temp.max()>sigma*alpha*fudge_factor:
        print "Recursing..."
        # we'll determine another peak width in this recursion, because less bright peaks might also be smaller.
        two_dim_findpeaks(image_temp,alpha=alpha*fudge_factor, 
                          recursion_level=recursion_level+1, 
                          max_peak_width=peak_width,
                          coords_list=coords_list)
    return coords_list