Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor school center allocation script to use settings module for configuration #34

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
39 changes: 15 additions & 24 deletions school_center.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,3 @@
OUTPUT_DIR = 'results/'

PREF_DISTANCE_THRESHOLD = 2 # Preferred threshold distance in kilometers
ABS_DISTANCE_THRESHOLD = 7 # Absolute threshold distance in kilometers
MIN_STUDENT_IN_CENTER = 10 # minimum number of students from a school to be assigned to a center in normal circumstances
STRETCH_CAPACITY_FACTOR = 0.02 # how much can center capacity be streched if need arises
PREF_CUTOFF = -4 # Do not allocate students with pref score less than cutoff

import math
import csv
import random
Expand All @@ -20,6 +12,15 @@
configure_logging()

logger = logging.getLogger(__name__)
from settings import (DISTANCE_TSV, OUTPUT_DIR,
PREF_DISTANCE_THRESHOLD,
ABS_DISTANCE_THRESHOLD,
MIN_STUDENT_IN_CENTER,
PREF_CUTOFF,
STRETCH_CAPACITY_FACTOR,
PREFS_TSV, SCHOOLS_TSV,
CENTERS_TSV,OUTPUT_TSV,)


def create_dir(dirPath:str):
"""
Expand Down Expand Up @@ -143,26 +144,16 @@ def is_allocated(scode1: str, scode2:str) -> bool:
else:
return False

parser = argparse.ArgumentParser(
prog='center randomizer',
description='Assigns centers to exam centers to students')
parser.add_argument('schools_tsv', default='schools.tsv', help="Tab separated (TSV) file containing school details")
parser.add_argument('centers_tsv', default='centers.tsv', help="Tab separated (TSV) file containing center details")
parser.add_argument('prefs_tsv', default='prefs.tsv', help="Tab separated (TSV) file containing preference scores")
parser.add_argument('-o', '--output', default='school-center.tsv', help='Output file')
args = parser.parse_args()

schools = sorted(read_tsv(args.schools_tsv), key= school_sort_key)
centers = read_tsv(args.centers_tsv)
schools = sorted(read_tsv(SCHOOLS_TSV), key= school_sort_key)
centers = read_tsv(CENTERS_TSV)
centers_remaining_cap = {c['cscode']:int(c['capacity']) for c in centers}
prefs = read_prefs(args.prefs_tsv)
prefs = read_prefs(PREFS_TSV)

remaining = 0 # stores count of non allocated students
allocations = {} # to track mutual allocations

create_dir(OUTPUT_DIR) # Create the output directory if not exists
with open('{}school-center-distance.tsv'.format(OUTPUT_DIR), 'w', encoding='utf-8') as intermediate_file, \
open(OUTPUT_DIR + args.output, 'w', encoding='utf-8') as a_file:
create_dir(OUTPUT_DIR)
with open(DISTANCE_TSV, 'w', encoding='utf-8') as intermediate_file, \
open(OUTPUT_TSV, 'w', encoding='utf-8') as a_file:
writer = csv.writer(intermediate_file, delimiter="\t")
writer.writerow(["scode", "s_count", "school_name", "school_lat", "school_long", "cscode", "center_name", "center_address", "center_capacity", "distance_km"])

Expand Down
14 changes: 14 additions & 0 deletions settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import os

PREF_DISTANCE_THRESHOLD = 2 # Preferred threshold distance in kilometers
ABS_DISTANCE_THRESHOLD = 7 # Absolute threshold distance in kilometers
MIN_STUDENT_IN_CENTER = 10 # minimum number of students from a school to be assigned to a center in normal circumstances
STRETCH_CAPACITY_FACTOR = 0.02 # how much can center capacity be streched if need arises
PREF_CUTOFF = -4 # Do not allocate students with pref score less than cutoff
BASE_DIR=os.path.dirname(os.path.abspath(__file__))
SCHOOLS_TSV = f"{BASE_DIR}/sample_data/schools_grade12_2081.tsv"
CENTERS_TSV = f"{BASE_DIR}/sample_data/centers_grade12_2081.tsv"
PREFS_TSV = f"{BASE_DIR}/sample_data/prefs.tsv"
OUTPUT_DIR=f"{BASE_DIR}/results"
OUTPUT_TSV=f"{OUTPUT_DIR}/school_center_allocation.tsv"
DISTANCE_TSV=f"{OUTPUT_DIR}/school_center_distance.tsv"