Project: s41467-025-56623-1 | Function: get_cell_partition_Linear_Regression_Model_2_new (python)

system: You are an expert Python programmer implementing algorithms from scientific papers.
Complete the masked function based on the algorithm description.
Follow Python best practices and maintain consistent code style.
Return **only** the complete Python function code.

user: Complete this Python function:

**DESCRIPTION:** Probability-based deconvolution model that addresses Type-III issues by incorporating expression variance information to maximize joint probability for cell type fraction estimation in bulk RNA-seq samples

**KEY TERMS:** {
  "L_min_comb": "Minimum combination (maximum probability)",
  "L_var_value": "Variance values (σ_it²)",
  "L_exp_mean": "Expression means (μ_it)",
  "L_sig_V": "Signature variance calculations"
}

**CONTEXT CODE:**     Y_list = []
    X_list = []
    L_gene_list = []
    for gene in p_expression_RNAseq:
        L_gene_list.append(gene)
    for gene in L_gene_list:
        t_gene_std = p_std_gene_old[gene] + 0.00000001
        Y_list.append(p_expression_RNAseq[gene] / t_gene_std)
        t_list = []
        for grp in L_grp_order:
            t_list.append(p_mean_std[gene][grp][0] / t_gene_std)
        X_list.append(t_list)
    x_train = numpy.array(X_list, dtype=numpy.float32)
    y_train = numpy.array(Y_list, dtype=numpy.float32)

    x_train_t = x_train.transpose()
    x_train_mul = numpy.dot(x_train_t, x_train)
    x_train_inv = inv(x_train_mul)
    x_result_ma = numpy.dot(x_train_inv, x_train_t)
    L_para = numpy.dot(x_result_ma, y_train)

    # print('LR:',L_para)

    L_min_comb = {};
    L_min_comb2 = {}
    t_total = 0.0
    for idx in range(len(L_grp_order)):
        gid = L_grp_order[idx]
        va = L_para[idx]
        t_total += va
        L_min_comb[gid] = va

    for idx in range(len(L_grp_order)):
        gid = L_grp_order[idx]
        va = L_para[idx]
        # L_min_comb2[gid] = va/t_total*100
        L_min_comb2[gid] = va / t_total

    L_std_gene_new_dic = {}
    for gene in L_gene_list:
        t_va = 0.0
        for grp in L_min_comb:
            if L_min_comb[grp] > 0:
                t_va += L_min_comb[grp] * p_mean_std[gene][grp][1] * p_mean_std[gene][grp][1]
        t_std = t_va ** 0.5
        L_std_gene_new_dic[gene] = t_std

    return L_min_comb2, L_min_comb, L_std_gene_new_dic


def get_cell_partition_Linear_Regression_Model_2_new(p_expression_RNAseq, p_mean_std, p_std_gene_old):
    """
    Probability-based deconvolution model that addresses Type-III issues.

    This function incorporates expression variance information to maximize joint
    probability for cell type fraction estimation in bulk RNA-seq samples. It
    addresses Type-III issues by integrating expression variance information into
    the deconvolution modeling, selecting only signature genes whose expression
    is stable within each type of cells for scRNA-seq reference construction.

    Args:
        p_expression_RNAseq: Dictionary of gene expression values from bulk RNA-seq
        p_mean_std: Dictionary containing mean and standard deviation for each gene
                   and cell type combination
        p_std_gene_old: Dictionary of gene-wise standard deviations from previous iteration

    Returns:
        tuple: (L_min_comb2, L_min_comb, L_std_gene_new_dic)
               - L_min_comb2: Normalized cell type fractions (sum to 1)
               - L_min_comb: Raw cell type fractions from linear regression
               - L_std_gene_new_dic: Updated gene-wise standard deviations
    """
    raise NotImplementedError("This function has been masked for testing purposes")


def get_cell_partition_Linear_Regression_Model_nnls(p_expression_RNAseq, p_mean_std, p_std_gene_old):
    L_min_comb = {}

    L_grp_order = []
    for gene in p_mean_std:
        for grp in p_mean_std[gene]:
            L_grp_order.append(grp)
        break
    Y_list = []
    X_list = []
    L_gene_list = []
    for gene in p_expression_RNAseq:
        L_gene_list.append(gene)
    for gene in L_gene_list:
        t_gene_std = p_std_gene_old[gene] + 0.00000001
        Y_list.append(p_expression_RNAseq[gene] / t_gene_std)
        t_list = []
        for grp in L_grp_order:
            t_list.append(p_mean_std[gene][grp][0] / t_gene_std)
        X_list.append(t_list)
    x_train = numpy.array(X_list, dtype=numpy.float32)
    y_train = numpy.array(Y_list, dtype=numpy.float32)

    L_para, this_Res = nnls(x_train, y_train)

    #   paraTp = ''
    #   for item in L_para:
    #      paraTp += '\t'+str(item)
    #   print('---LR:',paraTp)

    L_min_comb = {};
    L_min_comb2 = {}
    t_total = 0.0
    for idx in range(len(L_grp_order)):
        gid = L_grp_order[idx]
        va = L_para[idx]
        t_total += va
        L_min_comb[gid] = va

    for idx in range(len(L_grp_order)):
        gid = L_grp_order[idx]
        va = L_para[idx]
        # L_min_comb2[gid] = va/t_total*100
        L_min_comb2[gid] = va / t_total

    L_std_gene_new_dic = {}
    for gene in L_gene_list:
        t_va = 0.0
        for grp in L_min_comb:
            if L_min_comb[grp] > 0:
                t_va += L_min_comb[grp] * p_mean_std[gene][grp][1] * p_mean_std[gene][grp][1]
        t_std = t_va ** 0.5
        L_std_gene_new_dic[gene] = t_std

    return L_min_comb2, L_min_comb, L_std_gene_new_dic


def Percentage_Fraction_nor(p_per, p_fra):
    L_percentage = {};
    L_fraction = {}
    L_total = 0.0
    for item in p_per:
        item2 = p_per[item]
        if item2 > 0:
            L_total += item2
    for item in p_per:
        item2 = p_per[item]
        L_percentage[item] = max(0, item2 / L_total)

    for item in p_fra:
        item2 = p_fra[item]
        L_fraction[item] = max(0, item2)
    return L_percentage, L_fraction




Complete the function implementation replacing "raise NotImplementedError".