def redo_quality(block_path, dense_gt_path, minMemb, minSeg, sigMin, sigWeights, sigSmooth, edgeLengths=None,
                      nodeFeatures=None, nodeSizes=None, nodeLabels=None, nodeNumStop=None, beta=0, metric='l1',
                      wardness=0.2, out=None):

    #find all relevant folders in block path
    folder_list = [f for f in os.listdir(block_path) if not os.path.isfile(os.path.join(block_path,f))
                   and ("figure" not in f) and "redone" not in f]

    #create folder for redone quality
    outpath = block_path + "/redone_quality"
    if not os.path.exists(outpath):
        os.mkdir(outpath)

    dense_gt_data = read_h5(dense_gt_path)
    nodes_in_dgt = len(np.unique(dense_gt_data))

    #use prob file in every folder to create new quality in data in corresponding new folder
    for f in folder_list:


        #find prob file paths
        folder_path = os.path.join(block_path, f)
        prob_file = [h for h in os.listdir(folder_path) if "probs" in h]
        prob_file_path = os.path.join(folder_path,prob_file[0])

        #create new folders for new quality data
        config_name = prob_file_path.split("/")[-2]
        config_path = outpath +"/"+ config_name
        if not os.path.exists(config_path):
            os.mkdir(config_path)
        quality_data_path = config_path + "/" + config_name+".h5"

        #save new quality data in new folders
        save_quality_values(prob_file_path,gt_path, dense_gt_path, quality_data_path,slices,minMemb, minSeg, sigMin,
                            sigWeights, sigSmooth, edgeLengths, nodeFeatures, nodeSizes, nodeLabels, nodeNumStop, beta,
                            metric, wardness, out)
def test(ilp, files, gt_path, dense_gt_path, labels="", loops=3, weights="", repeats=1, outpath= "",
         t_cache = "", p_cache = ""):
    """ Run test
    :param: ilp             : path to ilp project to use for training
    :param: files           : path to file to do batch prediction on
    :param: gt_path         : path to trimap groundtruth
    :param: dense_gt_path   : path to dense groundtruth
    :param: labels          : amount of labeled pixels to use in training
    :param: loops           : amount of autocontext loops
    :param: weights         : weighting of the labels over the autocontext loops
    :param: repeats         : amount of repeat runs of the test
    :param: outpath         : outpath for test outputs
    :param: t_cache         : path to training data cache
    :param: p_cache         : path to prediction data cache
    """

    hostname = socket.gethostname()
    print "test information:"
    print
    print "hostname:", hostname
    print
    print "ilp_file:", ilp
    print
    print "files to predict:", files
    print
    print "groundtruth:", gt_path
    print
    print "dense groundtruth:", dense_gt_path
    print
    print "labels:", labels
    print
    print "loops:", loops
    print
    print "weights:", weights
    print
    print "repeats:", repeats

    # Collect the test specifications
    ilp_split = ilp.split(".")[-2]
    training_file = "training file: "+ilp_split.split("/")[-1]

    gt_split = gt_path.split(".")[-2]
    trimap_file = "trimap file: " + gt_split.split("/")[-1]


    # Assign paths
    filesplit = files.split(".")[-2]
    filename = filesplit.split("/")[-1]
    prediction_file = "prediction file: "+ filename

    test_folder_path = assign_path(hostname)[5]

    if t_cache == "":
        t_cache = test_folder_path + "/t_cache"

    if p_cache == "":
        p_cache = test_folder_path + "/p_cache/" + filename
        if not os.path.exists(p_cache):
            os.mkdir(p_cache)

    if outpath == "":
        output = test_folder_path + "/q_data"
    else:
        output = outpath

    print
    print "outpath:", outpath
    print
    print "t_cache:", t_cache
    print
    print "p_cache:", p_cache

    # Create file tags
    if labels == "":
        label_tag = "all"
    else:
        # assert labels == check_ilp_labels(ilp)
        true_labels = check_ilp_labels(ilp)
        label_tag = true_labels
    if weights == "":
        weight_tag = "none"
    else:
        weight_tag = str(weights)

    # Make folder for quality data

    if "manual" in ilp:
        filename += "_manual"
    if "hand_drawn" in ilp:
        filename += "_hand_drawn"
    if "clever" in ilp:
        filename += "_clever"
    if "less_feat" in ilp:
        filename += "_less_feat"


    #change labels for every test
    # if labels != "":
    #     print
    #     print "reducing labels to " + str(labels)
    #     ilp = reduce_labels_in_ilp(ilp, labels)

    file_dir = output + "/" + filename
    if not os.path.exists(file_dir):
        os.mkdir(file_dir)

    # Overwrite folder directory
    # file_dir = "/mnt/CLAWS1/stamilev/delme"

    # Check if file directory exists, if not make such directory
    if not os.path.exists(file_dir):
        print
        print "Output folder did not exist."
        os.mkdir(file_dir)
        print
        print "New one named " + file_dir + " was created."

    q_outpath = file_dir + "/n_" + str(loops) + "_l_" + str(label_tag) + "_w_" + weight_tag
    prob_folder = q_outpath + "/prob_files"
    q_data_outpath = q_outpath + "/n_" + str(loops) + "_l_" + str(label_tag) + "_w_" + weight_tag + ".h5"


    # Check if test directory exists, if not make such directory
    if not os.path.exists(q_outpath):
        print
        print "Output h5 file did not exist"
        os.mkdir(q_outpath)
        os.mkdir(prob_folder)
        print
        print "New one named " + q_outpath + " was created."
    if not os.path.exists(prob_folder):
        os.mkdir(prob_folder)

    # Run the test
    for i in range(repeats):
        print
        print "round of repeats %d of %d" % (i+1, repeats)

        #train on ilp project
        ac_train(ilp, labels, loops, weights, t_cache, outpath)
        print
        print "training completed"

        #batch predict files
        ac_batch_predict(files, t_cache, p_cache, overwrite = "")
        print
        print "batch prediction completed"

        #save prob files and quality data
        prob_file = [x for x in os.listdir(p_cache) if ("probs" in x)]
        predict_path = p_cache + "/" + prob_file[0]
        predict_data = adjust_predict(read_h5(predict_path))
        prob_path = prob_folder + "/prob_"+ str(i+1)+ ".h5"
        # if os.path.exists(prob_path):
        #     # TODO:not working yet
        #     prob_path = prob_path.split("/")[-2] + "/new_" + prob_path.split("/")[-1]
        #     print prob_path
        save_h5(predict_data, prob_path, "data")
        save_quality_values(predict_path, gt_path, dense_gt_path, q_data_outpath, (0,49,99))

        #save test specification data
        save_h5([training_file, prediction_file, trimap_file], q_data_outpath, "used files", None)
        save_h5([labels],q_data_outpath, "autocontext_parameters/#labels")
        save_h5([loops], q_data_outpath, "autocontext_parameters/#loops")
        save_h5([str(weight_tag)], q_data_outpath, "autocontext_parameters/weights")
        print
        print "quality data saved"

    #save configuration data
    # call(["cp", predict_path, q_outpath])
    save_h5(["pmin", "minMemb", "minSeg", "sigMin", "sigWeights", "sigSmooth", "cleanCloseSeeds", "returnSeedsOnly"],
            q_data_outpath, "segmentation/wsDt parameters", None)
    save_h5(["edge_weights", "edgeLengths", "nodeFeatures", "nodeSizes", "nodeLabels", "nodeNumStop", "beta", "metric",
             "wardness", "out"], q_data_outpath, "segmentation/agglCl parameters", None)
    print
    print "quality data saved"