예제 #1
0
 def test_new_gen(self): #OK
     nb_ped = 1
     nb_people = [50]
     nb_Gen_Max = [4]
     nb_Gen_Min = [math.ceil(x/2) for x in nb_Gen_Max]
     cl = 3
     ped1 = Pedigree()
     ped1.load("../data/ped/senegal2013.ped")
     bn_sen = pview.ped_to_bn_compact(ped1,0.05)
     print('clique sen',pview.max_clique_size(bn_sen))
     print('uho', ped1.all_consanguineous_ped(cl))
     for p, g_max, g_min in zip(nb_people, nb_Gen_Max, nb_Gen_Min):
         for nb in range(nb_ped):
             nbChild = random.randint(4, 8)
             g = random.randint(g_min, g_max)
             ped = Pedigree()
             ped.gen_ped(nb, p, g, nbChild, cl)
             # print('-------------------------')
             # print(ped)
             print('-------------------------')
             print('Consanguin',ped.all_consanguineous_ped(cl))
             bn = pview.ped_to_bn_compact(ped,0.05)
             print('clique',pview.max_clique_size(bn))
             print('-------------------------')
             pview.save(ped, f'../pedigree_{p}_{g}_{nbChild}_{cl-1}_G{nb}')
             pview.graph(ped, f'../generate_graph_{p}_{g}_{nbChild}_{cl-1}_G{nb}', False)
예제 #2
0
파일: test_bn.py 프로젝트: phwuil/bped2
 def test_bn_compact(self):
     ped = Pedigree()
     ped.load('../cplex/samples/pedigree_50_7_4_4_G8.ped')
     bn_no = pview.ped_to_bn(ped, 0.05)
     bn_compact = pview.ped_to_bn_compact()
     ie_no = pview.gum.LazyPropagation(bn_no)
     ie_compact = pview.gum.LazyPropagation(bn_compact)
예제 #3
0
파일: test_bn.py 프로젝트: phwuil/bped2
    def test_clique_csg(self):
        nb_ped = 50
        nb_people = [1900]
        nb_Gen_Max = [5]
        nb_Gen_Min = [5]
        cl = 3
        min_clique = 100
        max_clique = 0
        mean_clique = 0

        min_csg = 100
        max_csg = 0
        mean_csg = 0
        ped1 = Pedigree()
        ped1.load("../data/ped/senegal2013.ped")
        bn_sen = pview.ped_to_bn_compact(ped1, 0.05)
        print('clique sen', pview.max_clique_size(bn_sen))
        print('csg', len(ped1.all_consanguineous_ped(5)))
        for p, g_max, g_min in zip(nb_people, nb_Gen_Max, nb_Gen_Min):
            for nb in range(nb_ped):
                nbChild = random.randint(6, 12)
                g = random.randint(g_min, g_max)
                ped = Pedigree()
                ped.gen_ped(nb, p, g, nbChild, cl, 0.03)
                bn = pview.ped_to_bn_compact(ped, 0.05)
                clique = pview.max_clique_size(bn)
                csg = len(ped.all_consanguineous_ped(g))

                if clique > max_clique:
                    max_clique = clique
                if clique < min_clique:
                    min_clique = clique
                mean_clique += clique

                if csg > max_csg:
                    max_csg = csg
                if csg < min_csg:
                    min_csg = csg
                mean_csg += csg
            print(max_clique, min_clique, mean_clique / nb_ped)
            print(max_csg, min_csg, mean_csg / nb_ped)
예제 #4
0
mean_gen_is = []
errorValues_is = []
max_is = []
min_is = []

mean_gen_diff = []
errorValues_diff = []
max_diff = []
min_diff = []
w = 0

sen = Pedigree()
sen.load('../data/ped/senegal2013.ped')
len_sen = len(sen)
bn_sen = pview.ped_to_bn_compact(sen, f)
ie = pview.gum.ImportanceSampling(bn_sen)
ie.setMaxTime(200)
ie.setEpsilon(5e-4)
t1 = process_time()
ie.makeInference()
t2 = process_time()
time_sen_compact = t2 - t1

for p, g_max, g_min in zip(nb_people, nb_Gen_Max, nb_Gen_Min):
    tab_lis = np.zeros(nb_ped)
    tab_diff = np.zeros(nb_ped)
    for nb in range(nb_ped):
        nbChild = random.randint(6, 12)
        g = random.randint(g_min, g_max)
        ped = Pedigree()
예제 #5
0
w = 0
file = open('ttbn_1e4', 'w')
for p, g_max, g_min in zip(nb_people, nb_Gen_Max, nb_Gen_Min):

    tab_diff = np.zeros(nb_ped)
    for nb in range(nb_ped):
        y = 0
        nbChild = random.randint(6, 12)
        g = random.randint(g_min, g_max)
        ped = Pedigree()
        ped.gen_ped(nb, p, g, nbChild, cl, 0.03)
        pview.save(
            ped,
            f'../cplex/samples/TTBN/pedigree_{p}_{g}_{nbChild}_{cl}_G{nb}')

        bn_compact = pview.ped_to_bn_compact(ped, f)
        pview.save_bn(
            bn_compact,
            f'../cplex/bn/TTBN/bn_compact_{p}_{g}_{nbChild}_{cl}_G{nb}')

        # bn1 = pview.gum.BayesNet()
        # bn1.loadBIF(f'../cplex/bn/TTBN/bn_compact_{p}_{g}_{nbChild}_{cl}_G{nb}.bif')
        # ie1 = ttgum.ShaferShenoyTensorTrain(bn1, precision=1e-2, info=False)
        # marginalesSSTT, tempsSSTT, nb_param_SSTT = ie1.makeInference()
        marginalesSSTT = ttbn.ttbn_posterior(
            f'../cplex/bn/TTBN/bn_compact_{p}_{g}_{nbChild}_{cl}_G{nb}.bif')[0]

        # bn2 = pview.gum.BayesNet()
        # bn2.loadBIF(f'../cplex/bn/TTBN/bn_compact_{p}_{g}_{nbChild}_{cl}_G{nb}.bif')
        # ie2 = pview.gum.LazyPropagation(bn2)
        ie2 = laz.lazyPosterior(
예제 #6
0
파일: bped.py 프로젝트: phwuil/bped2
def main(args=None):
    """
    [--ped pedfile: format famid, id, patid (0 for founder), matid (0 for founder), gender (1 male, 2 female)
    [--ev evidencefile]: format famid, id, ev00, ev10 (1 paternal), ev01 (1 maternal), ev11
    [--famID Input the famID]
    [--freq minorallelefreq (default: freq=0.01)]
    [--targets list_of_ind (default: all)]
    [--size] size of the rendered graph
    [--bndot dotfile] export the BN into a dot file
    [--peddot dotfile] export the pedigree into a dot file
    [--audit textfile] introspect pedigree into a text file
    [--verbose] display error/warning messages in stderr
    [--mode] decide which version between normal, compact and multi version of a BN
    [--thetha] format [probability, probability, ...]
    [--centimorgans] format [distance, distance, ...]
    [--inference ] Decide which inference use, LazyPropagation or LoobyBeliefPropagation
    [--complete Bool] Decice which version between complete and compact for the audit file
    [--out outfile] export probabilities into a out file
    [--name_gen] format [str ,str ,str ...]
    """
    parser = OptionParser(version="%prog 0.1",
                          usage="usage: %prog [options] pedfile")
    parser.add_option("",
                      "--ev",
                      dest="evfile",
                      help="Specification of the profile",
                      metavar="FILE")
    parser.add_option("",
                      "--freq",
                      dest="f",
                      help="minor allel freq (gamma)",
                      type="float",
                      default=0.01)
    parser.add_option("", "--famID", dest='famID', help="Input the famID")
    parser.add_option(
        "",
        "--targets",
        dest="targets",
        help="Specification of a list of targets (comma separated)")
    parser.add_option("",
                      "--bndot",
                      dest="bndotfile",
                      help="Export the BN into a dot file",
                      metavar="FILE")
    parser.add_option("",
                      "--peddot",
                      dest="peddotfile",
                      help="Export the pedigree into a dot file",
                      metavar="FILE")
    parser.add_option("",
                      "--audit",
                      dest="auditfile",
                      help="Export some stats on pedigree in a text file",
                      metavar="FILE")
    parser.add_option("",
                      "--verbose",
                      dest="verbose",
                      help="messages while processing",
                      default=False,
                      action="store_true")
    parser.add_option("",
                      "--bn",
                      dest="bnfile",
                      help="Export the BN into a BIF file",
                      metavar="FILE")
    parser.add_option(
        "",
        "--mode",
        dest="mode",
        help="Choose the type of the generate bn, compact, no compact or multi",
        default='compact')
    parser.add_option(
        "",
        "--theta",
        dest="theta",
        type="string",
        help="Input a probability's distribution, shape: float;float; ...")
    parser.add_option(
        "",
        "--centimorgans",
        dest="centimorgans",
        type="string",
        help=
        "Input the distance between genes in centimorgans, shape: float;float; ..."
    )
    parser.add_option("",
                      "--inference",
                      dest="inference",
                      help="Choose between LP and LBP",
                      default='LP')
    parser.add_option(
        "",
        "--complete",
        dest="complete",
        help="Decide if the audit is a complete version or a compact version")
    parser.add_option("",
                      "--out",
                      dest="out",
                      help="Export the evidence into a out file",
                      metavar="FILE")
    parser.add_option("",
                      "--size",
                      dest="size",
                      help="Choose the size of the rendered graph",
                      type="int",
                      default=100)
    parser.add_option("",
                      "--name_gen",
                      dest="name_gen",
                      type="string",
                      help="Input the gene's name, shape: str;str; ...")

    if args is None:
        (options, arguments) = parser.parse_args()
    else:
        (options, arguments) = parser.parse_args(args)

    if len(arguments) != 2:  # si on n'a pas mis de pedfile ou on en a mis trop
        parser.parse_args(["--help"])
    else:
        # on retrouve toutes les valeurs dans options (principalement)
        # options.peddotfile contient le nom du fichier
        # options.verbose contient True or False
        # etc.
        print("=" * 40)
        print(f"arguments: {arguments}")
        print("=" * 40)
        print(f"options: {options}")
        print("=" * 40)
        print("\n\n")

        pedfile = arguments[1]
        print(f"working on file : {pedfile}")
        current_ped = ped.Pedigree()
        current_ped.load(str(pedfile))
        if options.famID:
            famID = options.famID
        else:
            list_famID = current_ped.get_domain()
            if len(list_famID) == 1:
                famID = list_famID.pop()
            else:
                return "missing famID"

        name_gen = None
        if options.name_gen:
            name_gen = options.name_gen.split(';')
            print(name_gen)
        if options.mode == 'compact':
            bn = pview.ped_to_bn_compact(current_ped, options.f)
            if options.verbose:
                pview.gnb.showBN(bn, options.size)
        elif options.mode == 'no_compact':
            bn = pview.ped_to_bn(current_ped, options.f)
            if options.verbose:
                pview.gnb.showBN(bn, options.size)
        elif options.mode == 'multi':
            if options.theta:
                distance = options.theta.split(';')
                nb_gen = len(distance) + 1
                for i in range(len(distance)):
                    distance[i] = float(distance[i])
                bn = pview.bn_multi_pb(current_ped, options.f, nb_gen,
                                       distance, name_gen)
                if options.verbose:
                    pview.gnb.showBN(bn, options.size)
            elif options.centimorgans:
                centimorgans = options.centimorgans.split(';')
                nb_gen = len(centimorgans)
                for i in range(len(centimorgans)):
                    centimorgans[i] = float(centimorgans[i])
                bn = pview.bn_multi_morgans(current_ped, options.f, nb_gen,
                                            centimorgans, name_gen)
                if options.verbose:
                    pview.gnb.showBN(bn, options.size)
            else:
                return "missing theta or centimorgan argument, cannot use the multi mode"

        if options.bnfile:
            bn.saveBIF(options.bndotfile)
            pview.gum.availableBNExts()
            pview.gum.saveBN(bn, options.bndotfile)
            if options.verbose:
                print('bndotfile saved in ' + options.bndotfile)

        if options.bndotfile:
            pview.save_dot(bn, options.bndotfile)
            if options.verbose:
                print('bn saved in ' + options.bndotfile)

        if options.peddotfile:
            if (len(current_ped)) > 1000:
                pview.graph(current_ped, options.peddotfile, True)
            else:
                pview.graph(current_ped, options.peddotfile, False)
            if options.verbose:
                print("graph save in " + options.peddotfile)

        if options.auditfile:
            if options.complete == str(False):
                print('false')
                current_ped.pedigree_overview_file(options.auditfile, False)
            else:
                current_ped.pedigree_overview_file(options.auditfile, True)
            if options.verbose:
                print('audit file ' + options.auditfile + ' created')

        if options.targets:
            if options.evfile:
                if options.mode == 'multi':
                    evidence = pview.load_evidence_multi(options.evfile, famID)
                else:
                    evidence = pview.load_evidence(options.evfile, famID)
                if options.verbose:
                    print(f"{options.evfile} loaded")

                if options.inference == 'LBP':
                    ie = pview.gum.LoopyBeliefPropagation(bn)
                else:
                    ie = pview.gum.LazyPropagation(bn)
                ie.setEvidence(evidence)
                ie.makeInference()
                for i in current_ped.get_pedigree().keys():
                    if i in options.targets:
                        print(ie.posterior(f"X{i}"))
                pview.create_out(options.out, current_ped, ie)
            else:
                if options.inference == 'LBP':
                    ie = pview.gum.LoopyBeliefPropagation(bn)
                else:
                    ie = pview.gum.LazyPropagation(bn)
                ie.makeInference()
                for i in current_ped.get_pedigree().keys():
                    if i in options.targets:
                        print(ie.posterior(f"X{i}"))
                pview.create_out(options.out, current_ped, ie)
        else:
            if options.evfile:
                if options.mode == 'multi':
                    evidence = pview.load_evidence_multi(options.evfile, famID)
                else:
                    evidence = pview.load_evidence(options.evfile, famID)
                if options.verbose:
                    print(f"{options.evfile} loaded")

                if options.inference == 'LBP':
                    ie = pview.gum.LoopyBeliefPropagation(bn)
                else:
                    ie = pview.gum.LazyPropagation(bn)
                ie.setEvidence(evidence)
                ie.makeInference()
                if options.mode == 'multi':
                    pview.create_out_multi(options.out, current_ped, ie,
                                           nb_gen, name_gen)
                else:
                    pview.create_out(options.out, current_ped, ie)
            else:
                if options.inference == 'LBP':
                    ie = pview.gum.LoopyBeliefPropagation(bn)
                else:
                    ie = pview.gum.LazyPropagation(bn)
                ie.makeInference()
                if options.mode == 'multi':
                    pview.create_out_multi(options.out, current_ped, ie,
                                           nb_gen, name_gen)
                else:
                    pview.create_out(options.out, current_ped, ie)