Last active
August 29, 2015 14:20
-
-
Save hokkun-dayo/b0d04c1ea96e8cf69474 to your computer and use it in GitHub Desktop.
MayFes.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
import pandas as pd | |
import datetime | |
import sys | |
from collections import defaultdict | |
import matplotlib.pyplot as plt | |
import argparse | |
# define constants | |
MAX_MEMBER = 6 # excluding band master | |
AVAILABLE = 0 # available is 0, unavailable is 1 | |
# For analysis | |
def compute_num_of_band(): | |
hist_y = [] | |
hist_x = [] | |
for day in range(0, final_day-initial_day+1): | |
for hour in range(first_h, last_h): | |
count = 0 | |
for i in range(len(data)): | |
count += data.ix[i]["{}-{}".format(day+1, hour+1)] | |
print "{}-{}".format(day+1, hour+1), "AVAILABLE: {}".format(total_band_num-count) | |
hist_x.append("{}-{}".format(day+1, hour+1)) | |
hist_y.append(total_band_num-count) | |
plt.barh([i for i in range(len(hist_y))], hist_y) | |
plt.yticks([i for i in range(len(hist_y))], hist_x) | |
plt.title("The number of band that can perform") | |
plt.show() | |
# For analysis | |
def compute_band_hist(): | |
plt.hist(data.total) | |
plt.show() | |
# sys.exit() | |
def compute_mean_age(data): | |
mean_age = [] | |
import re | |
r = re.compile(r"\d+") | |
for i in range(len(data)): | |
n_member = 0 | |
age = 0.0 | |
for j in range(MAX_MEMBER): | |
age_str = data.ix[i]["member{}".format(j+1)] | |
if pd.isnull(age_str): | |
continue | |
res = r.search(age_str) | |
if res == None: | |
continue | |
age += float(int(res.group(0))) | |
n_member += 1 | |
age /= float(n_member) | |
mean_age.append(age) | |
return mean_age | |
def main(): | |
parser = argparse.ArgumentParser() | |
parser.add_argument("input_csv", | |
help="Input csv file.") | |
# parse options | |
parser.add_argument("--initial_day", | |
type=int, | |
help="initial day.") | |
parser.add_argument("--final_day", | |
type=int, | |
help="final day.") | |
parser.add_argument("--initial_hour", | |
type=int, | |
help="initial hour.") | |
parser.add_argument("--last_hour", | |
type=int, | |
help="last hour.") | |
parser.add_argument("--rest_time", | |
default=15, | |
type=int, | |
help="last hour.") | |
args = parser.parse_args() | |
data = pd.read_csv(args.input_csv) | |
initial_day = args.initial_day | |
final_day = args.final_day | |
rest_time = args.rest_time | |
initial_hour = args.initial_hour | |
last_hour = args.last_hour | |
# results = [] | |
total_band_num = data.shape[0] | |
# add mean of ki | |
mean_age = compute_mean_age(data) | |
data["mean_age"] = pd.Series(mean_age, index=data.index) | |
# print data | |
now_time = datetime.datetime(2015, 5, initial_day, initial_hour, 0, 0) | |
while True: | |
# Error check | |
if now_time > datetime.datetime(now_time.year, now_time.month, now_time.day, last_hour-1, 59, 0): | |
now_time = datetime.datetime(now_time.year, now_time.month, now_time.day+1, 9, 0, 0) | |
if now_time.day > final_day: | |
break | |
# Check whether to insert break. | |
if now_time.hour == 12 and now_time.minute > 15: | |
print "Break!", now_time | |
now_time += datetime.timedelta(seconds=45*60) | |
continue | |
if now_time.hour % 2 == 0 and now_time.minute > 50: | |
print "Break!", now_time | |
now_time += datetime.timedelta(seconds=rest_time*60) | |
continue | |
# results.append(defaultdict(str)) | |
# select candidates of band | |
initial_candidate = data[data["{}-{}".format(now_time.day - initial_day + 1, now_time.hour + 1)]==AVAILABLE] | |
if len(initial_candidate) == 0: | |
print "Break!", now_time | |
now_time += datetime.timedelta(seconds=rest_time*60) | |
continue | |
# Filter by available time slot | |
max_total = initial_candidate.total.max() | |
candidate_by_total = initial_candidate[initial_candidate.total==max_total] | |
# Filter by mean age | |
max_mean_age = candidate_by_total.mean_age.max() | |
candidate_by_mean_age = candidate_by_total[candidate_by_total.mean_age==max_mean_age] | |
band = candidate_by_total.iloc[0] | |
band_index = candidate_by_total.index[0] | |
data = data.drop(band_index) | |
print now_time, band.band_name | |
now_time += datetime.timedelta(seconds=band.required_time*60) | |
print "Band not listed:" | |
for band in data.band_name.values: | |
print band | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment