Example #1
0
def create_C_W_X():
    X = np.random.randn(5, 3)
    X = vstack((X, ones((1, 3))))
    C = array([[1, 0, 1], [0, 1, 0]])

    W = np.random.randn(6, 2)

    return C, W, X
Example #2
0
def get_column_from_matrix(m: mat, c: int) -> array:
    """
    取矩阵的某一列
    :param m:
    :param c:
    :return:
    """
    return array([m[0, c], m[1, c]])
Example #3
0
def plot_hist(column_data=None, output=None, sub_title="", path=None):
    """
    Plot a histogram
    obj = {"col_name":[{'lower': -87.36666870117188, 'upper': -70.51333465576172, 'value': 0},
    {'lower': -70.51333465576172, 'upper': -53.66000061035157, 'value': 22094},
    {'lower': -53.66000061035157, 'upper': -36.80666656494141, 'value': 2},
    ...
    ]}
    :param column_data: column data in json format
    :param output: image, base64 or plot. Image output a file, base64 output a base64 encoded image and plot output the
    image to the notebook
    :param sub_title: plot subtitle
    :param path:
    :return: plot, image or base64
    """

    for col_name, data in column_data.items():
        bins = []
        # print(data)
        # print("**********")
        for d in data:
            bins.append(d['lower'])

        last = data[len(data) - 1]["upper"]
        bins.append(last)

        # Transform hist Optimus format to matplot lib format
        hist = []
        for d in data:
            if d is not None:
                hist.append(d["count"])

        array_bins = array(bins)
        center = (array_bins[:-1] + array_bins[1:]) / 2
        width = 0.9 * (array_bins[1] - array_bins[0])

        hist = one_list_to_val(hist)

        # Plot
        fig = plt.figure(figsize=(12, 5))
        plt.bar(center, hist, width=width)
        plt.title("Histogram '" + col_name + "' " + sub_title)

        # fig.tight_layout()

        if output is "base64":
            return output_base64(fig)
        elif output is "image":
            # Save image
            output_image(plt, path)
            print_html("<img src='" + path + "'>")
            # Print in jupyter notebook

        elif output is "plot":
            plt.subplots_adjust(left=0.05, right=0.99, top=0.9, bottom=0.3)
Example #4
0
    def reduce(S, lb, ub, keep=(), block=(), absolute_bounds=False):
        lb, ub = list(map(array, [lb, ub]))
        to_keep, to_block = [], []
        irrev = (lb >= 0) | ((ub <= 0) & (lb <= 0))

        if block:
            to_block = array(block)

        if keep:
            to_keep = array(keep)

        rd, sub, irrev_reduced, rdind, irrv_subsets, kept_reactions, K, _ = subset_reduction(
            S, irrev, to_keep_single=to_keep, to_remove=to_block)

        mapping = SubsetReducer.get_transform_maps(sub)

        nlb = [0 if irrev_reduced[k] else None for k in range(rd.shape[1])]
        nub = [None] * rd.shape[1]

        if absolute_bounds:
            nlb = [
                0 if irrev_reduced[k] else -1000 for k in range(rd.shape[1])
            ]
            nub = [float('inf')] * rd.shape[1]
            alb, aub = list(
                zip(*[[
                    fx([x[k] for k in mapping.from_new(i)])
                    for x, fx in zip([lb, ub], [max, min])
                ] for i in range(rd.shape[1])]))

            for func, pair in zip([max, min], [[nlb, alb], [nub, aub]]):
                new, absolute = pair
                for i, v in enumerate(absolute):
                    new[i] = func(new[i], absolute[i])

        return rd, nlb, nub, mapping, rdind
Example #5
0
def res_2_df(complex_bus_voltages, complex_bus_powers, bus_types):
    """
    Create data frame to display the results nicely

    :param complex_bus_voltages: List of complex voltages
    :param complex_bus_powers: List of complex powers
    :param bus_types: List of bus types

    :return: Pandas DataFrame
    """
    vm = abs(complex_bus_voltages)
    va = angle(complex_bus_voltages)

    d = {1: 'PQ', 2: 'PV', 3: 'VD'}

    tpe_str = array([d[i] for i in bus_types], dtype=object)
    data = c_[tpe_str, complex_bus_powers.real, complex_bus_powers.imag, vm,
              va]
    cols = ['Type', 'P', 'Q', '|V|', 'angle']
    data_frame = pd.DataFrame(data=data, columns=cols)

    return data_frame
Example #6
0
    def plot_transit_fit(self, ax=None, full_phase: bool = False, mode='all', nbins: int = 20, alpha=0.2):
        zero_epoch, period, duration = self.parameters[['tc', 'p', 't14']].iloc[0].copy()
        hdur = duration * array([-0.5, 0.5])

        phase = self.phase
        sids = argsort(phase)
        phase = phase[sids]
        pmask = ones(phase.size, bool) if full_phase else abs(phase) < 1.5 * duration

        if pmask.sum() < 100:
            alpha = 1

        if self.mode == 'all':
            fmod = self.ftra[sids]
            fobs = self.fobs[sids] / self.fbase[sids]
        else:
            fmod = self.fmod[sids]
            fobs = self.fobs[sids]

        ax.plot(24 * phase[pmask], fobs[pmask], '.', alpha=alpha)
        ax.plot(24 * phase[pmask], fmod[pmask], 'w', lw=5, alpha=0.5, zorder=99)
        ax.plot(24 * phase[pmask], fmod[pmask], 'k', zorder=100)

        # if duration > 1 / 24:
        pb, fb, eb = downsample_time(phase[pmask], fobs[pmask], phase[pmask].ptp() / nbins)
        mask = isfinite(pb)
        pb, fb, eb = pb[mask], fb[mask], eb[mask]
        ax.errorbar(24 * pb, fb, eb, fmt='k.')
        ylim = fb.min() - 2 * eb.max(), fb.max() + 2 * eb.max()
        # else:
        #    ylim = fobs[pmask].min(), fobs[pmask].max()

        ax.get_yaxis().get_major_formatter().set_useOffset(False)
        ax.axvline(0, alpha=0.25, ls='--', lw=1)
        [ax.axvline(24 * hd, alpha=0.25, ls='-', lw=1) for hd in hdur]

        ax.autoscale(axis='x', tight='true')
        setp(ax, ylim=ylim, xlabel='Phase [h]', ylabel='Normalised flux')
Example #7
0
def subset_reduction(S, irrev, to_remove=[], to_keep_single=[]):
    """
	Reduces a stoichiometric matrix using nullspace analysis by identifying linearly dependent (enzyme) subsets.
	These reactions are then compressed.

	Parameters
	----------
		S: Stoichiometric matrix as an ndarray.

		irrev: A boolean array with size equal to the number of columns in the S matrix.

		to_remove: A list of indices specifying columns of the S matrix to remove before the compression (usually blocked
		reactions)

		to_keep_single: A list of indices specifying columns of the S matrix not to compress.

	Returns	rd, sub, irrev_reduced, rdind, irrv_subsets, kept_reactions, kernel, correlation_matrix

		rd : compressed stoichiometric matrix -> numpy.array

		sub : subset matrix, n-subsets by n-reactions -> numpy.array

		irrev_reduced : subset reversibilities -> numpy.array of type bool

		rdind : metabolite indices -> numpy.array of type int

		irrv_subsets : same as sub, but list if empty

		kept_reactions : indexes for reactions used in the network compression

		kernel : numpy.array with the right nullspace of S

		correlation_matrix : numpy.array with reaction correlation matrix
	-------

	"""
    m, n = S.shape

    keep_single = array([False] * n)
    keep_single[to_keep_single] = True

    kept_reactions = array([True] * n)
    kept_reactions[to_remove] = False
    kept_reactions = where(kept_reactions)[0]

    ktol = EPSILON * sum(kept_reactions)

    kernel = compute_nullspace(S[:, kept_reactions], ktol, False)
    kernel_blocked = nullspace_blocked_reactions(kernel, ktol)

    if kernel_blocked.shape[0] > 0:
        kept_reactions = kept_reactions[setdiff1d(kept_reactions,
                                                  kernel_blocked)]
        kernel = compute_nullspace(S[:, kept_reactions], ktol, False)

    correlation_matrix = subset_candidates(kernel)
    S_scm = S[:, kept_reactions]
    irrev_scm = irrev[kept_reactions]
    scm_kp_ids = where([keep_single[kept_reactions]])[1]

    sub, irrev_reduced, irrv_subsets = subset_correlation_matrix(
        S_scm, kernel, irrev_scm, correlation_matrix, scm_kp_ids)
    if len(kept_reactions) < n:
        temp = zeros([sub.shape[0], n])
        temp[:, kept_reactions] = sub
        sub = temp
        if len(irrv_subsets) > 0:
            temp = zeros([len(irrv_subsets), n])
            temp[:, kept_reactions] = irrv_subsets
            irrv_subsets = temp
    rd, rdind, irrev_reduced_final, sub = reduce(S, sub, irrev_reduced)

    return rd, sub, irrev_reduced_final, rdind, irrv_subsets, kept_reactions, kernel, correlation_matrix
Example #8
0
def subset_correlation_matrix(S, kernel, irrev, cr, keepSingle=None):
    """

	Parameters
	----------
		S: Stoichiometric matrix as ndarray

		kernel: The nullspace of S

		irrev: List of booleans representing irreversible reactions (when True)

		cr: The subset candidate matrix, computed using <subset_candidates>

		keepSingle: List of reaction indices that will not be compressed.

	Returns sub, irrev_sub, irrev_violating_subsets

		sub : subset matrix, n-subsets by n-reactions -> numpy.array

		irrev_sub : subset reversibilities -> numpy.array of type bool

		irrev_violating_subsets : same as sub, but list if empty. Contains subsets discarded due to irreversibility faults
	-------

	"""
    m, n = S.shape
    in_subset = array([False] * n)
    irrev_sub = array([False] * cr.shape[0])
    sub = zeros([cr.shape[0], n])
    irrev_violating_subsets = []
    sub_count = 0
    if (keepSingle is not None) and (len(keepSingle) > 0):
        # keepSingle = array([])
        irrev_violating_subsets = []
        sub[:len(keepSingle), keepSingle] = eye(len(keepSingle))
        irrev_sub[:len(keepSingle)] = irrev[keepSingle]
        in_subset[keepSingle] = True
        sub_count = len(keepSingle)
    for i in range(cr.shape[0] - 1, -1, -1):
        reactions = where(cr[:, i] != 0)[0]
        in_subset_indexes = where(in_subset)[0]
        in_subset_reactions = isin(reactions, in_subset_indexes)
        reactions = reactions[logical_not(in_subset_reactions)]
        if len(reactions) > 0:
            in_subset[reactions] = True
            irrev_sub[sub_count] = (irrev[reactions]).any()

            if len(reactions) == 1:
                sub[sub_count, reactions] = 1
            else:
                lengths = norm(kernel[reactions, :], axis=1)
                min_ind = argmin(abs(lengths - mean(lengths)))
                lengths /= lengths[min_ind]
                sub[sub_count, reactions] = lengths * cr[reactions, i]
            sub_count += 1

    sub = sub[:sub_count, :]
    irrev_sub = irrev_sub[:sub_count]

    ind = unique(where(sub[:, irrev] < 0)[0])
    if len(ind) > 0:
        sub[ind, :] = -sub[ind, :]
        ind = unique(where(sub[:, irrev] < 0)[0])
        if len(ind) > 0:
            irrev_violating_subsets = sub[ind, :]
            sub = delete(sub, ind, 0)
            irrv_to_keep = delete(array(range(len(irrev_sub))), ind, 0)
            irrev_sub = irrev_sub[irrv_to_keep]

    return sub, irrev_sub, irrev_violating_subsets
Example #9
0
 def transform_single(self, image: Image) -> ndarray:
     return array([
         calculate_normalized_channel_histogram(bloc, channel, self.bins)
         for channel in range(3)
         for bloc in _split_images_in_blocs(image, self.rows, self.cols)
     ]).ravel()
Example #10
0
 def transform_single(self, image: Image) -> ndarray:
     return array([
         calculate_normalized_channel_histogram(image, channel, self.bins)
         for channel in range(3)
     ]).ravel()