<commit_before>import os
import sys
import os.path as op
import argparse
import numpy as np
import pandas as pd

from flask import Flask, request, render_template

from lyman import gather_project_info, gather_experiment_info

project = gather_project_info()
default_exp = project["default_exp"]

parser = argparse.ArgumentParser()
parser.add_argument("-experiment", default=default_exp)
parser.add_argument("-port", type=int, default=5000)
parser.add_argument("-external", action="store_true")
parser.add_argument("-debug", action="store_true")
args = parser.parse_args(sys.argv[1:])

exp = gather_experiment_info(args.experiment)

app = Flask(__name__)


def basic_info():
    """Basic information needed before any report options are set."""
    lyman_dir = os.environ["LYMAN_DIR"]

    subjects = np.loadtxt(op.join(lyman_dir, "subjects.txt"), str).tolist()
    selector_size = min(len(subjects), 10)

    runs = range(1, exp["n_runs"] + 1)

    contrasts = [c[0] for c in exp["contrasts"]]
    contrast_size = len(contrasts)

    return dict(all_subjects=subjects,
                selector_size=selector_size,
                experiment=args.experiment,
                runs=runs,
                all_contrasts=contrasts,
                contrast_size=contrast_size)


@app.route("/")
def home():
    """Build the selector column."""
    info = basic_info()
    return render_template("layout.html", **info)


@app.route("/report", methods=["GET", "POST"])
@app.route("/report/<arg1>", methods=["GET", "POST"])
@app.route("/report/<arg1>/<arg2>", methods=["GET", "POST"])
def generate_report(arg1=None, arg2=None):
    """Build the main report."""
    info = basic_info()

    if request.method == "POST":

        # Possibly generate a url for the report
        form = request.form
        if form.get("btn", "") == "Generate With Link":
            url = request.url + "?"
            args = ["=".join([k, v]) for k, v in form.iteritems()
                    if k != "btn"
                    and not (k == "space"
                             and not (form.getlist("ffx")
                                      or form.getlist("group")))
                    and not (k == "groupname" and not form.getlist("group"))]

            url += "&".join(args)
            info["report_url"] = unicode(url)

        # Populate info off the form values
        info = request_to_info(request.form, info)

    else:

        # Populate info for a group report
        if arg1 == "group":
            info["group"] = ["mask", "zstat", "peaks", "boxplot"]
            info["space"] = "mni"
            if arg2 is None:
                info["contrasts"] = info["all_contrasts"]
            else:
                info["contrasts"] = [arg2]

        # Populate info for a subject report
        elif arg1 is not None:<commit_msg>Fixing bug in URL generation<commit_after>import os
import sys
import os.path as op
import argparse
import numpy as np
import pandas as pd

from flask import Flask, request, render_template

from lyman import gather_project_info, gather_experiment_info

project = gather_project_info()
default_exp = project["default_exp"]

parser = argparse.ArgumentParser()
parser.add_argument("-experiment", default=default_exp)
parser.add_argument("-port", type=int, default=5000)
parser.add_argument("-external", action="store_true")
parser.add_argument("-debug", action="store_true")
args = parser.parse_args(sys.argv[1:])

exp = gather_experiment_info(args.experiment)

app = Flask(__name__)


def basic_info():
    """Basic information needed before any report options are set."""
    lyman_dir = os.environ["LYMAN_DIR"]

    subjects = np.loadtxt(op.join(lyman_dir, "subjects.txt"), str).tolist()
    selector_size = min(len(subjects), 10)

    runs = range(1, exp["n_runs"] + 1)

    contrasts = [c[0] for c in exp["contrasts"]]
    contrast_size = len(contrasts)

    return dict(all_subjects=subjects,
                selector_size=selector_size,
                experiment=args.experiment,
                runs=runs,
                all_contrasts=contrasts,
                contrast_size=contrast_size)


@app.route("/")
def home():
    """Build the selector column."""
    info = basic_info()
    return render_template("layout.html", **info)


@app.route("/report", methods=["GET", "POST"])
@app.route("/report/<arg1>", methods=["GET", "POST"])
@app.route("/report/<arg1>/<arg2>", methods=["GET", "POST"])
def generate_report(arg1=None, arg2=None):
    """Build the main report."""
    info = basic_info()

    if request.method == "POST":

        # Possibly generate a url for the report
        form = request.form
        if form.get("btn", "") == "Generate With Link":
            args = ""
            for key in form:
                if key == "btn":
                    continue
                elif key == "space" and not (form.getlist("ffx")
                                             or form.getlist("group")):
                    continue
                elif key == "groupname" and not form.getlist("group"):
                    continue
                for value in form.getlist(key):
                    args += "%s=%s&" % (key, value)
            url = request.url + "?" + args
            info["report_url"] = unicode(url)

        # Populate info off the form values
        info = request_to_info(request.form, info)

    else:

        # Populate info for a group report
        if arg1 == "group":
            info["group"] = ["mask", "zstat", "peaks", "boxplot"]
            info["space"] = "mni"
            if arg2 is None:
                info["contrasts"] = info["all_contrasts"]
            else:
                info["contrasts"] = [arg2]

        # Populate info for a subject report
        elif arg1 is not None:
{'fraction_non_alphanumeric': 0.13638729828214471, 'fraction_numerical': 0.00659378795766094, 'mean_word_length': 2.5018226002430133, 'pattern_counts': {'":': 7, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 5, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2515500', 'n_tokens_mistral': 1742, 'n_tokens_neox': 1686, 'n_words': 438}