Exemple #1
0
def interface(inpath, outpath, cap):

    signature = digest_paths(inpath)
    dst = osp.join(outpath, signature)
    os.makedirs(dst, exist_ok=True)

    raw = imread(inpath)
    processed = prep(raw).astype(np.uint32)
    data = watershed(processed, cap, np.zeros_like(processed, dtype='bool'))

    print(np.unique(data[-1]))
    for i, regions in enumerate(data):
        result = np.copy(raw)
        result = label2rgb(regions, image=result, bg_label=0)
        imsave(osp.join(dst, 'img-{}.png'.format(str.zfill(str(i), 3))),
               result)

    np.save(osp.join(dst, 'raw.npy'), raw)
    np.save(osp.join(dst, 'reg.npy'), data[-1])
Exemple #2
0
def year():

    if request.method == "POST":
        ## Pull data from database for players
        rows = db.execute(
            "SELECT * FROM playerStats WHERE year = :year ORDER BY player",
            year=request.form.get('years'))
        if not rows:
            return apology("Unable to load data", 400)

        rows = prep(rows)

        rows = sorted(rows, key=itemgetter('avg'), reverse=True)

        erasorts = sorted(rows, key=itemgetter('era'), reverse=False)
        for erasort in erasorts:
            if erasort['era'] == 100000000:
                erasort['era'] = 'N/A'
            else:
                erasort['era'] = "{:.2f}".format(float(erasort['era']))

        totals = db.execute(
            "SELECT SUM(ab), SUM(r), SUM(h), SUM(rbi), SUM(twob), SUM(threeb), SUM(hr), SUM(sac), SUM(sb), SUM(hbp), SUM(bb), SUM(k), SUM(ip), SUM(hp), SUM(rp), SUM(er), SUM(bbp), SUM(kp), SUM(w), SUM(l), SUM(s), SUM(so), AVG(er), AVG(ip), AVG(h), AVG(hbp), AVG(bb), AVG(ab), AVG(twob), AVG(threeb), AVG(hr) FROM playerStats WHERE year = :year",
            year=request.form.get('years'))
        if not totals:
            return apology("Unable to load data", 400)

        for x in totals:
            oneb = int(x['SUM(h)']) - (int(x['SUM(twob)']) + int(
                x['SUM(threeb)']) + int(x['SUM(hr)']))
            x['oneb'] = oneb
            x['tb'] = int(x['oneb'] + ((int(x['SUM(twob)']) * 2) +
                                       (int(x['SUM(threeb)']) * 3) +
                                       (int(x['SUM(hr)']) * 4)))
            x['ip'] = float(x['SUM(ip)'])
            x['kp'] = int(x['SUM(kp)'])
        if int(x['SUM(ab)']) > 0:
            avg = truncate(x['AVG(h)'] / x['AVG(ab)'], 3)
            x['avg'] = avg
            slg = truncate((x['tb'] / int(x['SUM(ab)'])), 3)
            x['slg'] = slg
        else:
            x['avg'] = 'N/A'
            x['slg'] = 'N/A'
        if int(x['SUM(ab)']) + x['SUM(hbp)'] + int(x['SUM(bb)']) > 0:
            obp = truncate((x['AVG(h)'] + x['AVG(hbp)'] + x['AVG(bb)']) /
                           (x['AVG(ab)'] + x['AVG(hbp)'] + x['AVG(bb)']), 3)
            x['obp'] = obp
        else:
            x['obp'] = 'N/A'
        if x['ip'] > 0:
            era = truncate((9 * x['AVG(er)']) / (x['AVG(ip)']), 2)
            x['era'] = era
        else:
            x['era'] = 'N/A'

        if x['ip'] > 0:
            oppsba = truncate(
                int(x['SUM(hp)']) / ((x['ip'] * 3) + int(x['SUM(hp)'])), 3)
            whip = truncate(
                (int(x['SUM(bbp)']) + int(x['SUM(hp)'])) / (x['ip']), 3)
        else:
            oppsba = "N/A"
            whip = "N/A"
        x['ip'] = "{:.2f}".format(float(x['SUM(ip)']))

        return render_template("selectedyear.html",
                               rows=rows,
                               totals=totals,
                               oppsba=oppsba,
                               whip=whip,
                               year=request.form.get('years'),
                               erasorts=erasorts)

    if request.method == "GET":
        ## Pull data from database for players
        rows = db.execute(
            "SELECT DISTINCT year FROM playerStats ORDER BY year")
        if not rows:
            return apology("Unable to load data", 400)

        return render_template("year.html", rows=rows)
Exemple #3
0
def legacy():

    ## When the form is completed
    if request.method == 'POST':

        rows = db.execute(
            "SELECT * FROM playerStats WHERE player = :player ORDER BY year",
            player=request.form.get("legplayers"))
        player = request.form.get('legplayers')
        if not rows:
            return apology("Unable to load data", 400)

        rows = prep(rows)
        for row in rows:
            if row['era'] == 100000000:
                row['era'] = 'N/A'
            else:
                row['era'] = "{:.2f}".format(row['era'])

        ## Find total for all player data
        totals = db.execute(
            "SELECT SUM(ab), SUM(r), SUM(h), SUM(rbi), SUM(twob), SUM(threeb), SUM(hr), SUM(sac), SUM(sb), SUM(hbp), SUM(bb), SUM(k), SUM(ip), SUM(hp), SUM(rp), SUM(er), SUM(bbp), SUM(kp), SUM(w), SUM(l), SUM(s), SUM(so), AVG(er), AVG(ip), AVG(h), AVG(hbp), AVG(bb), AVG(ab), AVG(twob), AVG(threeb), AVG(hr) FROM playerStats WHERE player = :play",
            play=request.form.get('legplayers'))
        if not totals:
            return apology("Unable to load data", 400)

        for x in totals:
            oneb = int(x['SUM(h)']) - (int(x['SUM(twob)']) + int(
                x['SUM(threeb)']) + int(x['SUM(hr)']))
            x['oneb'] = oneb
            x['tb'] = int(x['oneb'] + ((int(x['SUM(twob)']) * 2) +
                                       (int(x['SUM(threeb)']) * 3) +
                                       (int(x['SUM(hr)']) * 4)))
            x['ip'] = float(x['SUM(ip)'])
            x['kp'] = int(x['SUM(kp)'])
        if int(x['SUM(ab)']) > 0:
            avg = truncate(x['AVG(h)'] / x['AVG(ab)'], 3)
            x['avg'] = avg
            slg = truncate((x['tb'] / int(x['SUM(ab)'])), 3)
            x['slg'] = slg
        else:
            x['avg'] = 'N/A'
            x['slg'] = 'N/A'
        if int(x['SUM(ab)']) + x['SUM(hbp)'] + int(x['SUM(bb)']) > 0:
            obp = truncate((x['AVG(h)'] + x['AVG(hbp)'] + x['AVG(bb)']) /
                           (x['AVG(ab)'] + x['AVG(hbp)'] + x['AVG(bb)']), 3)
            x['obp'] = obp
        else:
            x['obp'] = 'N/A'
        if x['ip'] > 0:
            era = truncate((9 * x['AVG(er)']) / (x['AVG(ip)']), 2)
            x['era'] = era
        else:
            x['era'] = 'N/A'

        if x['ip'] > 0:
            oppsba = truncate(
                int(x['SUM(hp)']) / ((x['ip'] * 3) + int(x['SUM(hp)'])), 3)
            whip = truncate(
                (int(x['SUM(bbp)']) + int(x['SUM(hp)'])) / (x['ip']), 3)
        else:
            oppsba = "N/A"
            whip = "N/A"
        x['ip'] = "{:.2f}".format(float(x['SUM(ip)']))

        return render_template("selectedlegacy.html",
                               rows=rows,
                               totals=totals,
                               oppsba=oppsba,
                               whip=whip,
                               player=player)

    else:
        ## Pull data from database for players
        rows = db.execute(
            "SELECT name FROM players WHERE status = 'legacy' ORDER BY name")
        if not rows:
            return apology("Unable to load data", 400)

        return render_template("legacy.html", rows=rows)
Exemple #4
0
def home():

    ## Figure out if it's baseball season yet, if not display last year's info
    now = datetime.now()
    if now.month < 5:
        year = int(now.year) - 1
    else:
        year = int(now.year)
    ranks = db.execute('SELECT * FROM playerStats where year = :year',
                       year=year)
    if not ranks:
        return apology("Unable to load data", 400)

    ranks = prep(ranks)

    ## Tabulate current highest ranking players
    rows = db.execute(
        'SELECT * FROM standings WHERE year = :year AND team = "Padres"',
        year=year)

    ## Determine number of games played
    for row in rows:
        games = (int(row['win']) + int(row['loss']) + int(row['tie'])) * 2.1

    ## Determine if min is hit to be included in current rankings
    avgs = db.execute(
        'SELECT player, h, ab FROM playerStats where year = :year AND ab > :games',
        year=year,
        games=games)
    for aver in avgs:
        if aver['ab'] > 0:
            avg = truncate(aver['h'] / aver['ab'], 3)
            aver['avg'] = avg

    ## Calculate current rankings
    avgsort = sorted(avgs, key=itemgetter('avg'), reverse=True)[:5]
    for x in avgsort:
        x['avg'] = "{:.3f}".format(float(x['avg']))

    hsort = sorted(ranks, key=itemgetter('h'), reverse=True)[:5]

    rsort = sorted(ranks, key=itemgetter('r'), reverse=True)[:5]

    rbisort = sorted(ranks, key=itemgetter('rbi'), reverse=True)[:5]

    sbsort = sorted(ranks, key=itemgetter('sb'), reverse=True)[:5]

    tbsort = sorted(ranks, key=itemgetter('tb'), reverse=True)[:5]

    wsort = sorted(ranks, key=itemgetter('w'), reverse=True)[:2]

    kpsort = sorted(ranks, key=itemgetter('kp'), reverse=True)[:2]

    ipsort = sorted(ranks, key=itemgetter('ip'), reverse=True)[:2]
    for x in ipsort:
        x['ip'] = "{:.2f}".format(float(x['ip']))

    ## Determine if min is hit to be included in current rankings
    eras = db.execute(
        'SELECT player, er, ip FROM playerStats where year = :year AND ip > 10',
        year=year)
    for eraa in eras:
        if eraa['ip'] > 0:
            era = (9 * eraa['er']) / (eraa['ip'])
            eraa['era'] = era
        else:
            eraa['era'] = " "

    erasort = sorted(eras, key=itemgetter('era'), reverse=False)[:2]
    for x in erasort:
        if x['era'] == 100000000:
            x['era'] = 'N/A'
        else:
            x['era'] = "{:.2f}".format(float(x['era']))

    ## Tabulate current standings across league
    rows = db.execute('SELECT * FROM standings WHERE year = :year', year=year)
    if not rows:
        return apology("Unable to load data", 400)

    ## Pull recap data
    res = db.execute('SELECT * FROM recap')
    recapsort = sorted(res, key=itemgetter('date'), reverse=True)[:5]

    return render_template("home.html",
                           recapsort=recapsort,
                           rows=rows,
                           ranks=ranks,
                           avgsort=avgsort,
                           hsort=hsort,
                           rsort=rsort,
                           rbisort=rbisort,
                           sbsort=sbsort,
                           tbsort=tbsort,
                           wsort=wsort,
                           kpsort=kpsort,
                           ipsort=ipsort,
                           erasort=erasort,
                           year=year)