|
import pandas as pd |
|
import gradio as gr |
|
import os |
|
import threading |
|
import time |
|
import random |
|
import datetime |
|
import shutil |
|
import matplotlib.pyplot as plt |
|
import numpy as np |
|
|
|
|
|
PROJECT_ROOT_DIR='./' |
|
IMAGES_PATH = os.path.join(PROJECT_ROOT_DIR, "images") |
|
os.makedirs(IMAGES_PATH, exist_ok=True) |
|
|
|
|
|
def draw_classroom(occupy_list={}, center_list = {}, out_path = "layout.png"): |
|
|
|
def draw_trapezoid(ax, center, width, height, table_color, seat_color, tb_idx): |
|
|
|
bottom_width = width / 4 |
|
top_width = width / 2 |
|
coordinates = np.array([ |
|
[center[0] - bottom_width, center[1]], |
|
[center[0] - top_width, center[1] + height], |
|
[center[0] + top_width, center[1] + height], |
|
[center[0] + bottom_width, center[1]], |
|
[center[0] - bottom_width, center[1]] |
|
]) |
|
|
|
|
|
ax.plot(coordinates[:,0], coordinates[:,1], color=table_color) |
|
|
|
loc_x, loc_y = np.mean(coordinates, axis=0) |
|
if tb_idx < 10: |
|
ax.text(loc_x, loc_y, str(tb_idx), bbox=dict(facecolor='lightgray', edgecolor='none', boxstyle='round'), fontsize=12) |
|
else: |
|
ax.text(loc_x-0.1, loc_y+0.1, str(tb_idx), bbox=dict(facecolor='lightgray', edgecolor='none', boxstyle='round'), fontsize=12) |
|
|
|
|
|
for i in range(4): |
|
start, end = coordinates[i], coordinates[i+1] |
|
|
|
xs = np.linspace(start[0], end[0], 5)[2:3] |
|
ys = np.linspace(start[1], end[1], 5)[2:3] |
|
for x, y in zip(xs, ys): |
|
|
|
if str(tb_idx)+'_'+str(i+1) in occupy_list: |
|
if str(tb_idx)+'_'+str(i+1) in center_list: |
|
ax.plot(x, y, 's', color= center_list[str(tb_idx)+'_'+str(i+1)][1], markersize=20, markeredgecolor = 'black') |
|
else: |
|
ax.plot(x, y, 'o', color= occupy_list[str(tb_idx)+'_'+str(i+1)][1], markersize=20, markeredgecolor = 'black') |
|
else: |
|
ax.plot(x, y, 'o', color=seat_color, markersize=20, markeredgecolor = 'black') |
|
if i == 0: |
|
ax.text(x-0.4,y, str(i+1), fontsize=12) |
|
elif i == 1: |
|
ax.text(x,y+0.2, str(i+1), fontsize=12) |
|
elif i == 2: |
|
ax.text(x+0.2,y, str(i+1), fontsize=12) |
|
elif i == 3: |
|
ax.text(x,y-0.4, str(i+1), fontsize=12) |
|
|
|
|
|
|
|
fig, ax = plt.subplots(figsize=(12,10)) |
|
|
|
|
|
tables_in_row = 5 |
|
tables_in_column = 5 |
|
table_width = 1.5 |
|
table_height = 1.5 |
|
horizontal_spacing = 1.5 |
|
vertical_spacing = 1 |
|
|
|
|
|
tb_idx = 0 |
|
for i in range(tables_in_column): |
|
for j in range(tables_in_row): |
|
center_x = j * (table_width + horizontal_spacing) |
|
center_y = i * (table_height + vertical_spacing) |
|
draw_trapezoid(ax, (center_x, center_y), table_width, table_height, 'gray', 'white', tb_idx) |
|
tb_idx += 1 |
|
|
|
|
|
ax.set_aspect('equal') |
|
|
|
|
|
ax.axis('off') |
|
|
|
|
|
|
|
|
|
print("Saving figure to ",out_path) |
|
fig_extension = 'png' |
|
plt.tight_layout() |
|
plt.savefig(out_path, format=fig_extension, dpi=100) |
|
|
|
|
|
|
|
|
|
file_lock = threading.Lock() |
|
|
|
def data_process(layout, step, desk, seatid, color, instructor): |
|
file_path = "data_statistics.csv" |
|
|
|
color_map = {"Black (default)": 'black', |
|
"Red (Group 1)": 'red', |
|
"Green (Group 2)": 'green', |
|
"Yellow (Group 3)": 'yellow'} |
|
|
|
if color in color_map: |
|
label_color = color_map[color] |
|
else: |
|
label_color = 'black' |
|
|
|
|
|
if instructor == '4. Clean Data': |
|
img_path = os.path.join(IMAGES_PATH, "layout.png") |
|
if not os.path.exists(img_path): |
|
draw_classroom(occupy_list = [], out_path = img_path) |
|
print("Saving layout image to ",img_path) |
|
|
|
temp = {"Time":[] , "layout": [], "step": [], "desk": [], "seatid": [], "color": []} |
|
temp_stat = pd.DataFrame(temp) |
|
|
|
file_path = "data_statistics.csv" |
|
if os.path.exists(file_path): |
|
current_time = datetime.datetime.now().strftime("%Y%m%d_%H%M%S") |
|
backup_filename = f'data_statistics_{current_time}.csv' |
|
shutil.move(file_path, backup_filename) |
|
|
|
temp_stat.to_csv(file_path, index=False) |
|
print("Data removed") |
|
else: |
|
print("Emtpy data") |
|
temp_stat.to_csv(file_path, index=False) |
|
|
|
return "Result Updated", img_path, temp_stat |
|
|
|
elif instructor == '5. Remove step 2 data from table': |
|
img_path = os.path.join(IMAGES_PATH, "layout.png") |
|
if not os.path.exists(img_path): |
|
draw_classroom(occupy_list = [], out_path = img_path) |
|
print("Saving layout image to ",img_path) |
|
|
|
temp = {"Time":[] , "layout": [], "step": [], "desk": [], "seatid": [], "color": []} |
|
temp_stat = pd.DataFrame(temp) |
|
|
|
file_path = "data_statistics.csv" |
|
if os.path.exists(file_path): |
|
raw_data = pd.read_csv(file_path) |
|
current_time = datetime.datetime.now().strftime("%Y%m%d_%H%M%S") |
|
backup_filename = f'data_statistics_{current_time}.csv' |
|
shutil.move(file_path, backup_filename) |
|
|
|
temp_stat = raw_data[raw_data['step'] != 'Step 2: Update the locations of centroids (only for centroid student to select)'] |
|
temp_stat.to_csv(file_path, index=False) |
|
print("Data udpated") |
|
print("temp_stat: ",temp_stat) |
|
|
|
raw_data_step1 = temp_stat[temp_stat['step'] == 'Step 1: Check-in if you want to participate (provide desk_id, seat_id below)'] |
|
|
|
if len(raw_data_step1) == 0: |
|
img_path = os.path.join(IMAGES_PATH, "layout.png") |
|
if not os.path.exists(img_path): |
|
draw_classroom(occupy_list = [], out_path = img_path) |
|
print("Saving layout image to ",img_path) |
|
temp = {"Time":[] , "layout": [], "step": [], "desk": [], "seatid": [], "color": []} |
|
temp_stat = pd.DataFrame(temp) |
|
else: |
|
|
|
result = raw_data_step1.apply(lambda row: {f"{int(row['desk'])}_{int(row['seatid'])}": ['A', row['color']]}, axis=1) |
|
|
|
occupy_list = {} |
|
for d in result: |
|
occupy_list.update(d) |
|
print("occupy_list: ",occupy_list) |
|
|
|
img_path = os.path.join(IMAGES_PATH, "layout_step1.png") |
|
draw_classroom(occupy_list = occupy_list, out_path = img_path) |
|
print("Saving layout image to ",img_path) |
|
|
|
else: |
|
print("Emtpy data") |
|
temp_stat.to_csv(file_path, index=False) |
|
|
|
return "Result Updated", img_path, temp_stat |
|
|
|
elif instructor == '6. Remove step 3 data from table': |
|
img_path = os.path.join(IMAGES_PATH, "layout.png") |
|
if not os.path.exists(img_path): |
|
draw_classroom(occupy_list = [], out_path = img_path) |
|
print("Saving layout image to ",img_path) |
|
|
|
temp = {"Time":[] , "layout": [], "step": [], "desk": [], "seatid": [], "color": []} |
|
temp_stat = pd.DataFrame(temp) |
|
|
|
file_path = "data_statistics.csv" |
|
if os.path.exists(file_path): |
|
raw_data = pd.read_csv(file_path) |
|
current_time = datetime.datetime.now().strftime("%Y%m%d_%H%M%S") |
|
backup_filename = f'data_statistics_{current_time}.csv' |
|
shutil.move(file_path, backup_filename) |
|
|
|
temp_stat = raw_data[raw_data['step'] != 'Step 3: Find the closest centroid & update cluster label assignments (for all participator)'] |
|
temp_stat.to_csv(file_path, index=False) |
|
print("Data udpated") |
|
print("temp_stat: ",temp_stat) |
|
|
|
raw_data_step1 = temp_stat[temp_stat['step'] == 'Step 1: Check-in if you want to participate (provide desk_id, seat_id below)'] |
|
|
|
if len(raw_data_step1) == 0: |
|
img_path = os.path.join(IMAGES_PATH, "layout.png") |
|
if not os.path.exists(img_path): |
|
draw_classroom(occupy_list = [], out_path = img_path) |
|
print("Saving layout image to ",img_path) |
|
temp = {"Time":[] , "layout": [], "step": [], "desk": [], "seatid": [], "color": []} |
|
temp_stat = pd.DataFrame(temp) |
|
else: |
|
|
|
result = raw_data_step1.apply(lambda row: {f"{int(row['desk'])}_{int(row['seatid'])}": ['A', row['color']]}, axis=1) |
|
|
|
occupy_list = {} |
|
for d in result: |
|
occupy_list.update(d) |
|
print("occupy_list: ",occupy_list) |
|
|
|
img_path = os.path.join(IMAGES_PATH, "layout_step1.png") |
|
draw_classroom(occupy_list = occupy_list, out_path = img_path) |
|
print("Saving layout image to ",img_path) |
|
else: |
|
print("Emtpy data") |
|
temp_stat.to_csv(file_path, index=False) |
|
|
|
return "Result Updated", img_path, temp_stat |
|
|
|
elif instructor == '0. Display Statistics': |
|
img_path = os.path.join(IMAGES_PATH, "layout.png") |
|
if not os.path.exists(img_path): |
|
draw_classroom(occupy_list = [], out_path = img_path) |
|
print("Saving layout image to ",img_path) |
|
|
|
temp = {"Time":[] , "layout": [], "step": [], "desk": [], "seatid": [], "color": []} |
|
temp_stat = pd.DataFrame(temp) |
|
|
|
file_path = "data_statistics.csv" |
|
if os.path.exists(file_path): |
|
print("File exists. Loading the data") |
|
raw_data = pd.read_csv(file_path) |
|
else: |
|
print("Creating the data") |
|
raw_data = temp_stat |
|
raw_data = raw_data.dropna() |
|
|
|
|
|
raw_data_step1 = raw_data[raw_data['step'] == 'Step 1: Check-in if you want to participate (provide desk_id, seat_id below)'] |
|
|
|
if len(raw_data_step1) == 0: |
|
img_path = os.path.join(IMAGES_PATH, "layout.png") |
|
if not os.path.exists(img_path): |
|
draw_classroom(occupy_list = [], out_path = img_path) |
|
print("Saving layout image to ",img_path) |
|
temp = {"Time":[] , "layout": [], "step": [], "desk": [], "seatid": [], "color": []} |
|
temp_stat = pd.DataFrame(temp) |
|
else: |
|
|
|
result = raw_data_step1.apply(lambda row: {f"{int(row['desk'])}_{int(row['seatid'])}": ['A', row['color']]}, axis=1) |
|
|
|
occupy_list = {} |
|
for d in result: |
|
occupy_list.update(d) |
|
print("occupy_list: ",occupy_list) |
|
|
|
img_path = os.path.join(IMAGES_PATH, "layout_step1.png") |
|
draw_classroom(occupy_list = occupy_list, out_path = img_path) |
|
print("Saving layout image to ",img_path) |
|
|
|
|
|
return "Result Updated", img_path, raw_data |
|
|
|
elif instructor == '1a. Display Check-in Data (Step 1)': |
|
|
|
temp = {"Time":[] , "layout": [], "step": [], "desk": [], "seatid": [], "color": []} |
|
temp_stat = pd.DataFrame(temp) |
|
|
|
file_path = "data_statistics.csv" |
|
if os.path.exists(file_path): |
|
print("File exists. Loading the data") |
|
raw_data = pd.read_csv(file_path) |
|
else: |
|
print("Creating the data") |
|
raw_data = temp_stat |
|
|
|
raw_data = raw_data.dropna() |
|
|
|
raw_data_step1 = raw_data[raw_data['step'] == 'Step 1: Check-in if you want to participate (provide desk_id, seat_id below)'] |
|
|
|
if len(raw_data_step1) == 0: |
|
img_path = os.path.join(IMAGES_PATH, "layout.png") |
|
if not os.path.exists(img_path): |
|
draw_classroom(occupy_list = [], out_path = img_path) |
|
print("Saving layout image to ",img_path) |
|
temp = {"Time":[] , "layout": [], "step": [], "desk": [], "seatid": [], "color": []} |
|
temp_stat = pd.DataFrame(temp) |
|
else: |
|
|
|
result = raw_data_step1.apply(lambda row: {f"{int(row['desk'])}_{int(row['seatid'])}": ['A', row['color']]}, axis=1) |
|
|
|
occupy_list = {} |
|
for d in result: |
|
occupy_list.update(d) |
|
print("occupy_list: ",occupy_list) |
|
|
|
img_path = os.path.join(IMAGES_PATH, "layout_step1.png") |
|
draw_classroom(occupy_list = occupy_list, out_path = img_path) |
|
print("Saving layout image to ",img_path) |
|
|
|
return "Result Updated", img_path, temp_stat |
|
|
|
elif instructor == '1b. Display Centroids Data (Step 2)': |
|
temp = {"Time":[] , "layout": [], "step": [], "desk": [], "seatid": [], "color": []} |
|
temp_stat = pd.DataFrame(temp) |
|
|
|
file_path = "data_statistics.csv" |
|
if os.path.exists(file_path): |
|
print("File exists. Loading the data") |
|
raw_data = pd.read_csv(file_path) |
|
else: |
|
print("Creating the data") |
|
raw_data = temp_stat |
|
|
|
raw_data = raw_data.dropna() |
|
raw_data_step1 = raw_data[raw_data['step'] == 'Step 1: Check-in if you want to participate (provide desk_id, seat_id below)'] |
|
|
|
if len(raw_data_step1) == 0: |
|
img_path = os.path.join(IMAGES_PATH, "layout.png") |
|
if not os.path.exists(img_path): |
|
draw_classroom(occupy_list = [], out_path = img_path) |
|
print("Saving layout image to ",img_path) |
|
temp = {"Time":[] , "layout": [], "step": [], "desk": [], "seatid": [], "color": []} |
|
temp_stat = pd.DataFrame(temp) |
|
else: |
|
|
|
result = raw_data_step1.apply(lambda row: {f"{int(row['desk'])}_{int(row['seatid'])}": ['A', row['color']]}, axis=1) |
|
|
|
occupy_list = {} |
|
for d in result: |
|
occupy_list.update(d) |
|
|
|
|
|
raw_data_step2 = raw_data[raw_data['step'] == 'Step 2: Update the locations of centroids (only for centroid student to select)'] |
|
raw_data_step2 = raw_data_step2.drop_duplicates(subset=['desk', 'seatid'], keep='last') |
|
raw_data_step2 = raw_data_step2.drop_duplicates(subset='color', keep='last').tail(3) |
|
|
|
|
|
if len(raw_data_step2) != 0: |
|
result_step2 = raw_data_step2.apply(lambda row: {f"{int(row['desk'])}_{int(row['seatid'])}": ['A', row['color']]}, axis=1) |
|
|
|
occupy_list_step2 = {} |
|
for d in result_step2: |
|
occupy_list_step2.update(d) |
|
|
|
print("occupy_list_step2: ", occupy_list_step2) |
|
|
|
for key,val in occupy_list_step2.items(): |
|
occupy_list[key] = val |
|
|
|
|
|
|
|
print("occupy_list: ",occupy_list) |
|
|
|
img_path = os.path.join(IMAGES_PATH, "layout_step2.png") |
|
draw_classroom(occupy_list = occupy_list, center_list = occupy_list_step2, out_path = img_path) |
|
else: |
|
|
|
print("occupy_list: ",occupy_list) |
|
|
|
img_path = os.path.join(IMAGES_PATH, "layout_step1.png") |
|
if not os.path.exists(img_path): |
|
draw_classroom(occupy_list = occupy_list, out_path = img_path) |
|
print("Saving layout image to ",img_path) |
|
|
|
return "Result Updated", img_path, raw_data_step2 |
|
|
|
|
|
elif instructor == '2. Display Cluster Centroids (Step 2)': |
|
temp = {"Time":[] , "layout": [], "step": [], "desk": [], "seatid": [], "color": []} |
|
temp_stat = pd.DataFrame(temp) |
|
|
|
file_path = "data_statistics.csv" |
|
if os.path.exists(file_path): |
|
print("File exists. Loading the data") |
|
raw_data = pd.read_csv(file_path) |
|
else: |
|
print("Creating the data") |
|
raw_data = temp_stat |
|
|
|
raw_data = raw_data.dropna() |
|
raw_data_step1 = raw_data[raw_data['step'] == 'Step 1: Check-in if you want to participate (provide desk_id, seat_id below)'] |
|
|
|
if len(raw_data_step1) == 0: |
|
img_path = os.path.join(IMAGES_PATH, "layout.png") |
|
if not os.path.exists(img_path): |
|
draw_classroom(occupy_list = [], out_path = img_path) |
|
print("Saving layout image to ",img_path) |
|
temp = {"Time":[] , "layout": [], "step": [], "desk": [], "seatid": [], "color": []} |
|
temp_stat = pd.DataFrame(temp) |
|
else: |
|
|
|
result = raw_data_step1.apply(lambda row: {f"{int(row['desk'])}_{int(row['seatid'])}": ['A', row['color']]}, axis=1) |
|
|
|
occupy_list = {} |
|
for d in result: |
|
occupy_list.update(d) |
|
|
|
|
|
raw_data_step2 = raw_data[raw_data['step'] == 'Step 2: Update the locations of centroids (only for centroid student to select)'] |
|
raw_data_step2 = raw_data_step2.drop_duplicates(subset=['desk', 'seatid'], keep='last') |
|
raw_data_step2 = raw_data_step2.drop_duplicates(subset='color', keep='last').tail(3) |
|
|
|
if len(raw_data_step2) != 0: |
|
result_step2 = raw_data_step2.apply(lambda row: {f"{int(row['desk'])}_{int(row['seatid'])}": ['A', row['color']]}, axis=1) |
|
|
|
occupy_list_step2 = {} |
|
for d in result_step2: |
|
occupy_list_step2.update(d) |
|
|
|
print("occupy_list_step2: ", occupy_list_step2) |
|
|
|
for key,val in occupy_list_step2.items(): |
|
occupy_list[key] = val |
|
|
|
|
|
|
|
|
|
print("occupy_list: ",occupy_list) |
|
|
|
if len(raw_data_step2) != 0: |
|
img_path = os.path.join(IMAGES_PATH, "layout_step2.png") |
|
draw_classroom(occupy_list = occupy_list, center_list = occupy_list_step2, out_path = img_path) |
|
else: |
|
img_path = os.path.join(IMAGES_PATH, "layout_step1.png") |
|
if not os.path.exists(img_path): |
|
draw_classroom(occupy_list = occupy_list, out_path = img_path) |
|
print("Saving layout image to ",img_path) |
|
|
|
return "Result Updated", img_path, temp_stat |
|
|
|
|
|
elif instructor == '3. Display Updated Cluster Labels (Step 3)': |
|
temp = {"Time":[] , "layout": [], "step": [], "desk": [], "seatid": [], "color": []} |
|
temp_stat = pd.DataFrame(temp) |
|
|
|
file_path = "data_statistics.csv" |
|
if os.path.exists(file_path): |
|
print("File exists. Loading the data") |
|
raw_data = pd.read_csv(file_path) |
|
else: |
|
print("Creating the data") |
|
raw_data = temp_stat |
|
|
|
raw_data = raw_data.dropna() |
|
raw_data_step1 = raw_data[raw_data['step'] == 'Step 1: Check-in if you want to participate (provide desk_id, seat_id below)'] |
|
|
|
if len(raw_data_step1) == 0: |
|
img_path = os.path.join(IMAGES_PATH, "layout.png") |
|
if not os.path.exists(img_path): |
|
draw_classroom(occupy_list = [], out_path = img_path) |
|
print("Saving layout image to ",img_path) |
|
temp = {"Time":[] , "layout": [], "step": [], "desk": [], "seatid": [], "color": []} |
|
temp_stat = pd.DataFrame(temp) |
|
else: |
|
result = raw_data_step1.apply(lambda row: {f"{int(row['desk'])}_{int(row['seatid'])}": ['A', row['color']]}, axis=1) |
|
|
|
occupy_list = {} |
|
for d in result: |
|
occupy_list.update(d) |
|
|
|
|
|
raw_data_step2 = raw_data[raw_data['step'] == 'Step 2: Update the locations of centroids (only for centroid student to select)'] |
|
raw_data_step2 = raw_data_step2.drop_duplicates(subset=['desk', 'seatid'], keep='last') |
|
raw_data_step2 = raw_data_step2.drop_duplicates(subset='color', keep='last').tail(3) |
|
|
|
if len(raw_data_step2) != 0: |
|
result_step2 = raw_data_step2.apply(lambda row: {f"{int(row['desk'])}_{int(row['seatid'])}": ['A', row['color']]}, axis=1) |
|
|
|
occupy_list_step2 = {} |
|
for d in result_step2: |
|
occupy_list_step2.update(d) |
|
|
|
print("occupy_list_step2: ", occupy_list_step2) |
|
|
|
for key,val in occupy_list_step2.items(): |
|
occupy_list[key] = val |
|
|
|
|
|
raw_data_step3 = raw_data[raw_data['step'] == 'Step 3: Find the closest centroid & update cluster label assignments (for all participator)'] |
|
if len(raw_data_step3) != 0: |
|
result_step3 = raw_data_step3.apply(lambda row: {f"{int(row['desk'])}_{int(row['seatid'])}": ['A', row['color']]}, axis=1) |
|
|
|
occupy_list_step3 = {} |
|
for d in result_step3: |
|
occupy_list_step3.update(d) |
|
|
|
print("occupy_list_step3: ", occupy_list_step3) |
|
|
|
for key,val in occupy_list_step3.items(): |
|
occupy_list[key] = val |
|
|
|
|
|
|
|
|
|
print("occupy_list: ",occupy_list) |
|
|
|
if len(raw_data_step2) != 0: |
|
img_path = os.path.join(IMAGES_PATH, "layout_step3.png") |
|
draw_classroom(occupy_list = occupy_list, center_list = occupy_list_step2, out_path = img_path) |
|
else: |
|
img_path = os.path.join(IMAGES_PATH, "layout_step1.png") |
|
if not os.path.exists(img_path): |
|
draw_classroom(occupy_list = occupy_list, out_path = img_path) |
|
print("Saving layout image to ",img_path) |
|
|
|
return "Result Updated", img_path, temp_stat |
|
|
|
|
|
file_lock.acquire() |
|
|
|
try: |
|
|
|
if step == 'Step 1: Check-in if you want to participate (provide desk_id, seat_id below)': |
|
label_color = 'black' |
|
|
|
if step == 'Step 2: Update the locations of centroids (only for centroid student to select)': |
|
if label_color == 'black': |
|
print("Black color is not allowed in Step 2, please choose another color...") |
|
img_path = os.path.join(IMAGES_PATH, "layout_step1.png") |
|
if not os.path.exists(img_path): |
|
img_path = os.path.join(IMAGES_PATH, "layout.png") |
|
if not os.path.exists(img_path): |
|
draw_classroom(occupy_list = [], out_path = img_path) |
|
print("Saving layout image to ",img_path) |
|
temp = {"Time":[] , "layout": [], "step": [], "desk": [], "seatid": [], "color": []} |
|
temp_stat = pd.DataFrame(temp) |
|
|
|
return "Result Updated", img_path, temp_stat |
|
|
|
current_time = datetime.datetime.now() |
|
|
|
|
|
temp = {"Time":current_time , "layout": layout, "step": step, "desk": int(desk), "seatid": int(seatid), "color": label_color} |
|
temp_stat = pd.DataFrame(temp, index=[0]) |
|
|
|
time.sleep(random.uniform(0, 5)) |
|
|
|
if os.path.exists(file_path): |
|
print("File exists. Loading the data") |
|
raw_data = pd.read_csv(file_path) |
|
raw_data = pd.concat([raw_data, temp_stat], ignore_index=True) |
|
|
|
else: |
|
print("Creating the data") |
|
raw_data = temp_stat |
|
|
|
raw_data = raw_data.dropna() |
|
|
|
if step != 'None': |
|
raw_data.to_csv(file_path, index=False) |
|
|
|
finally: |
|
|
|
file_lock.release() |
|
|
|
|
|
if layout: |
|
|
|
temp = {"Time":[] , "layout": [], "step": [], "desk": [], "seatid": [], "color": []} |
|
temp_stat = pd.DataFrame(temp) |
|
|
|
file_path = "data_statistics.csv" |
|
if os.path.exists(file_path): |
|
print("File exists. Loading the data") |
|
raw_data = pd.read_csv(file_path) |
|
else: |
|
print("Creating the data") |
|
raw_data = temp_stat |
|
|
|
raw_data = raw_data.dropna() |
|
|
|
raw_data_step1 = raw_data[raw_data['step'] == 'Step 1: Check-in if you want to participate (provide desk_id, seat_id below)'] |
|
|
|
if len(raw_data_step1) == 0: |
|
img_path = os.path.join(IMAGES_PATH, "layout.png") |
|
if not os.path.exists(img_path): |
|
draw_classroom(occupy_list = [], out_path = img_path) |
|
print("Saving layout image to ",img_path) |
|
temp = {"Time":[] , "layout": [], "step": [], "desk": [], "seatid": [], "color": []} |
|
temp_stat = pd.DataFrame(temp) |
|
else: |
|
|
|
result = raw_data_step1.apply(lambda row: {f"{int(row['desk'])}_{int(row['seatid'])}": ['A', row['color']]}, axis=1) |
|
|
|
occupy_list = {} |
|
for d in result: |
|
occupy_list.update(d) |
|
print("occupy_list: ",occupy_list) |
|
|
|
img_path = os.path.join(IMAGES_PATH, "layout_step1.png") |
|
|
|
|
|
raw_data_step2 = raw_data[raw_data['step'] == 'Step 2: Update the locations of centroids (only for centroid student to select)'] |
|
raw_data_step2 = raw_data_step2.drop_duplicates(subset=['desk', 'seatid'], keep='last') |
|
raw_data_step2 = raw_data_step2.drop_duplicates(subset='color', keep='last').tail(3) |
|
if len(raw_data_step2) != 0: |
|
result_step2 = raw_data_step2.apply(lambda row: {f"{int(row['desk'])}_{int(row['seatid'])}": ['A', row['color']]}, axis=1) |
|
|
|
occupy_list_step2 = {} |
|
for d in result_step2: |
|
occupy_list_step2.update(d) |
|
|
|
|
|
for key,val in occupy_list_step2.items(): |
|
occupy_list[key] = val |
|
|
|
|
|
raw_data_step3 = raw_data[raw_data['step'] == 'Step 3: Find the closest centroid & update cluster label assignments (for all participator)'] |
|
if len(raw_data_step3) != 0: |
|
result_step3 = raw_data_step3.apply(lambda row: {f"{int(row['desk'])}_{int(row['seatid'])}": ['A', row['color']]}, axis=1) |
|
|
|
occupy_list_step3 = {} |
|
for d in result_step3: |
|
occupy_list_step3.update(d) |
|
|
|
print("occupy_list_step3: ", occupy_list_step3) |
|
|
|
for key,val in occupy_list_step3.items(): |
|
occupy_list[key] = val |
|
if len(raw_data_step3) == 0: |
|
img_path = os.path.join(IMAGES_PATH, "layout_step2.png") |
|
else: |
|
img_path = os.path.join(IMAGES_PATH, "layout_step3.png") |
|
draw_classroom(occupy_list = occupy_list, center_list = occupy_list_step2, out_path = img_path) |
|
else: |
|
draw_classroom(occupy_list = occupy_list, out_path = img_path) |
|
|
|
print("Saving layout image to ",img_path) |
|
|
|
return "Result Updated", img_path, temp_stat |
|
|
|
else: |
|
img_path = os.path.join(IMAGES_PATH, "layout.png") |
|
if not os.path.exists(img_path): |
|
draw_classroom(occupy_list = [], out_path = img_path) |
|
print("Saving layout image to ",img_path) |
|
temp = {"Time":[] , "layout": [], "step": [], "desk": [], "seatid": [], "color": []} |
|
temp_stat = pd.DataFrame(temp) |
|
|
|
return "Result Updated", img_path, temp_stat |
|
|
|
|
|
set_layout = gr.Checkbox(label="Show Classroom Layout", value=True, info="Find desk id and seat id in classroom (Check this box, click 'Submit')") |
|
|
|
set_step = gr.Dropdown( |
|
["None", "Step 1: Check-in if you want to participate (provide desk_id, seat_id below)", |
|
"Step 2: Update the locations of centroids (only for centroid student to select)", |
|
"Step 3: Find the closest centroid & update cluster label assignments (for all participator)"], |
|
value="None", label="1. Select the algorithm step" |
|
) |
|
|
|
set_desk = gr.Dropdown( |
|
[i for i in range(0,25)], |
|
value=0, label="2. Submit your Desk ID (Check Classroom Layout)" |
|
) |
|
|
|
|
|
set_seatid = gr.Dropdown( |
|
[i for i in range(1,5)], |
|
value=1, label="3. Submit your Seat ID (Check Classroom Layout)" |
|
) |
|
|
|
|
|
set_color = gr.Dropdown( |
|
["Black (default)", |
|
"Red (Group 1)", |
|
"Green (Group 2)", |
|
"Yellow (Group 3)"], |
|
value="Black (default)", label="4. Pick your seat color (Cluster label)" |
|
) |
|
|
|
|
|
set_instructor = gr.Dropdown( |
|
["I am student", |
|
"0. Display Statistics", |
|
"1a. Display Check-in Data (Step 1)", |
|
"1b. Display Centroids Data (Step 2)", |
|
"2. Display Cluster Centroids (Step 2)", |
|
"3. Display Updated Cluster Labels (Step 3)", |
|
"4. Clean Data", |
|
"5. Remove step 2 data from table", |
|
"6. Remove step 3 data from table"], |
|
value="I am student", label="5. For instructor use only. For students, please always keep 'I am student'" |
|
) |
|
|
|
|
|
set_output = gr.Textbox(label="Status") |
|
|
|
|
|
set_out_images = gr.Image(label="Classroom Layout") |
|
|
|
|
|
set_output2 = gr.Dataframe( |
|
headers=["Time","layout","step","desk","seatid","color"], |
|
datatype=["str","str", "str", "str", "str", "str"], |
|
label = 'Statistics' |
|
) |
|
|
|
|
|
interface = gr.Interface(fn=data_process, |
|
inputs=[set_layout, set_step, set_desk, set_seatid, set_color, set_instructor], |
|
outputs=[set_output,set_out_images,set_output2], |
|
title="CSCI4750/5750 Demo 4: In-class Practice for Seat Clustering in BSC 253", |
|
description= "Author: Jie Hou. \nPlease follow instructions in class", |
|
theme = 'huggingface' |
|
) |
|
interface.launch(debug=True) |
|
|
|
|