# if summary is not yet set: prepare it
            if len(summary) == 0:
                for m in methodkeys:
                    summary[m] = defaultdict(list)

            # fill results into summary dictionary
            for m in methodkeys:
                for i, k in enumerate(fpkeys):
                    j = i * 2
                    summary[m][k].append(
                        [target, results[m][j], results[m][j + 1]])

    # write out
    outdir = outpath + '/method_summary/'
    if not os.path.exists(outdir): os.makedirs(outdir)
    # loop over methods
    for m in methodkeys:
        outfile = open(outdir + 'summary_' + m + '.txt', 'w')
        ana_func.writeHeader(outfile, fpkeys)
        # loop over targets
        for t in range(len(summary[m][fpkeys[0]])):
            curr_id = summary[m][fpkeys[0]][t][0]
            outfile.write("%s " % curr_id)
            # loop over fingerprints
            for k in fpkeys:
                element = summary[m][k][t]
                outfile.write("%.3f %.3f " % (element[1], element[2]))
            outfile.write("\r\n")  # for Windows
        outfile.close()
            if len(ranks) == 0:
                for m in methodkeys:
                    ranks[m] = defaultdict(list)

            # prepare writing out
            # if header is not yet written to csv file
            if not header:
                for m in methodkeys:
                    outfile_csv = open(outdir_csv+'/fp_ranking_'+m+'.csv', 'w')
                    outfile_csv.write("\"fp\",\"target\",\"rank\",\"repeat\"\n")
                    outfile_csv.close()
                header = True

            # header of average score per target
            outfile = open(outdir+'/target_'+str(target)+'.txt', 'w')
            ana_func.writeHeader(outfile, fpkeys)

            # calculate average score
            # loop over methods
            for m in methodkeys:
                outfile.write("%s\t" % m) # average score file
                outfile_csv = open(outdir_csv+'/fp_ranking_'+m+'.csv', 'a') # csv file: append!
                # loop over fingerprints
                for k in fpkeys:
                    # csv rank file
                    for i,v in enumerate(validation[m][k]):
                        outfile_csv.write("\"%s\",\"%s\",\"%i\",\"%i\"\n" % (k, str(target), v[1], i+1))
                    # average score file
                    tmp = []
                    for i in validation[m][k]:
                        ranks[m][k].append(i[1]) # store ranks
            methodkeys = results.keys()

            # if summary is not yet set: prepare it
            if len(summary) == 0:
                for m in methodkeys:
                    summary[m] = defaultdict(list)

            # fill results into summary dictionary
            for m in methodkeys:
                for i,k in enumerate(fpkeys):
                    j = i*2
                    summary[m][k].append([target, results[m][j], results[m][j+1]])

    # write out
    outdir = outpath+'/fp_summary/'
    if not os.path.exists(outdir): os.makedirs(outdir)
    # loop over fingerprints
    for k in fpkeys:
        outfile = open(outdir+'summary_'+k+'.txt', 'w')
        ana_func.writeHeader(outfile, methodkeys)
        # loop over targets
        for t in range(len(summary[methodkeys[0]][k])):
            curr_id = summary[methodkeys[0]][k][t][0]
            outfile.write("%s " % curr_id)
            # loop over methods
            for m in methodkeys:
                element = summary[m][k][t]
                outfile.write("%.3f %.3f " % (element[1], element[2]))
            outfile.write("\r\n") # for Windows
        outfile.close()
        # if summary is not yet set: prepare it
        if len(summary) == 0:
            for m in methodkeys:
                summary[m] = defaultdict(list)

        # fill results into summary dictionary
        for m in methodkeys:
            for i, k in enumerate(fpkeys):
                j = i * 2
                summary[m][k].append(
                    [target, results[m][j], results[m][j + 1]])

    # write out
    outdir = outpath + '/fp_summary/'
    if not os.path.exists(outdir): os.makedirs(outdir)
    # loop over fingerprints
    for k in fpkeys:
        outfile = open(outdir + 'summary_' + k + '.txt', 'w')
        ana_func.writeHeader(outfile, methodkeys)
        # loop over targets
        for t in range(len(summary[methodkeys[0]][k])):
            curr_id = summary[methodkeys[0]][k][t][0]
            outfile.write("%s " % curr_id)
            # loop over methods
            for m in methodkeys:
                element = summary[m][k][t]
                outfile.write("%.3f %.3f " % (element[1], element[2]))
            outfile.write("\r\n")  # for Windows
        outfile.close()