Generating Sample Data
Relevant source files
The following files were used as context for generating this wiki page:The Campuspal Room Finder project includes a robust sample data generation system to facilitate testing and demonstration. This system is centered around the generate_sample_data.py script, which creates realistic, large-scale datasets for rooms and class schedules. The purpose is to provide a comprehensive dataset that accurately represents a real college environment, enabling thorough testing of the application's features and performance.
The generated data is output into two Excel files, rooms.xlsx and timetable.xlsx, which can then be uploaded into the application to populate the database. This process is crucial for development and for providing a clear example for users on how to structure their own data.
Sources: README.md:65-71, generate_sample_data.py
Data Generation Process
The core of the system is the generate_sample_data.py script. It uses the pandas library to programmatically create and export data. The process can be broken down into configuration, room data generation, timetable data generation, and file output.
The following diagram illustrates the end-to-end flow from script execution to data being loaded into the application's database.
Script Configuration
The script begins with a configuration section that defines the parameters for data generation. These variables control the size and variety of the dataset.
| Variable | Description | Example Value |
|---|---|---|
NUM_ROOMS | The total number of rooms to generate. | 50 |
NUM_SCHEDULES | The total number of class schedule entries to generate. | 300 |
BUILDINGS | A dictionary mapping full building names to their short codes. | {"Hephaestus Hall": "HH"} |
AMENITIES_LIST | A list of possible amenities a room can have. | ["Projector", "Whiteboard"] |
PHOTO_URLS | A list of sample image URLs for rooms. | https://images.unsplash.com/... |
COURSES | A list of course codes. | ["CS101", "MA203"] |
SUBJECTS | A list of course subjects corresponding to the codes. | ["Intro to Programming"] |
LECTURERS | A list of lecturer names. | ["Dr. Smith", "Prof. Jones"] |
DAYS | A list of valid days for class schedules. | ["Mon", "Tue", "Wed", "Thu", "Fri"] |
Sources: generate_sample_data.py:5-32
Room Data Generation
The script generates data for NUM_ROOMS. For each room, it performs the following steps:
- Randomly selects a building name and code from the
BUILDINGSdictionary. - Generates a room number by combining the building code with a random integer (e.g.,
HH123). - Randomly assigns a capacity from a predefined list (
[20, 25, 30, 40, 50, 60, 100]). - Randomly selects 1 to 4 amenities from
AMENITIES_LISTand formats them as a JSON string. - Randomly assigns a photo URL from the
PHOTO_URLSlist. - Appends the generated room data to a list, which is then converted into a pandas DataFrame.
Sources: generate_sample_data.py:35-46
Timetable Data Generation
After generating rooms, the script creates NUM_SCHEDULES class schedule entries. For each entry:
- It randomly selects a course, subject, lecturer, and day from the respective configuration lists.
- It generates a random start time between 7:00 (7 AM) and 20:00 (8 PM), on a 30-minute interval (e.g., 07:00, 07:30).
- It randomly selects a duration of 1, 1.5, or 2 hours.
- It calculates the end time based on the start time and duration.
- It formats the day and time into a single string (e.g.,
Mon 09:00-11:00). - It randomly assigns one of the previously generated rooms to the schedule entry.
- The data is appended to a list and converted into a pandas DataFrame.
Sources: generate_sample_data.py:51-70
Output File Structures
The script outputs two .xlsx files into the sample_data/ directory. These files are structured to be compatible with the application's database models and the upload_excel view.
Sources: generate_sample_data.py:72-75
rooms.xlsx
This file contains the list of all generated rooms and their properties. It directly maps to the Room model in the database.
| Column | Data Type | Description | Corresponding Model Field |
|---|---|---|---|
Room | string | The unique name/number of the room (e.g., "HH101"). | name |
Building | string | The full name of the building. | building |
capacity | integer | The seating capacity of the room. | capacity |
amenities | JSON string | A JSON array of amenities (e.g., ["Projector"]). | amenities |
photo_url | string | A URL to an image of the room. | photo_url |
Sources: generate_sample_data.py:48, finder/models.py:7-13
timetable.xlsx
This file contains the schedule for various courses in the generated rooms. It maps to the ClassSchedule model.
| Column | Data Type | Description | Corresponding Model Field(s) |
|---|---|---|---|
Course | string | The course code (e.g., "CS101"). | course |
Subject | string | The full subject name. | subject |
Day/Time | string | The day and time range (e.g., "Mon 09:00-10:30"). | day, start_time, end_time |
Room | string | The name of the room where the class is held. | room (Foreign Key) |
Building | string | The building of the room. | room (Foreign Key) |
Lecturer | string | The name of the lecturer. | lecturer |
Sources: generate_sample_data.py:71, finder/models.py:38-51
Data Consumption and Database Schema
The generated Excel files are consumed by the upload_excel view, which is accessible to staff members. This view uses pandas to read the files and populate the Room and ClassSchedule tables in the database. The relationship between these two models is central to the application's functionality.
The ER diagram below shows the database schema that the generated data populates.
This diagram illustrates that a Room can have zero or more ClassSchedule entries.
Sources: finder/models.py, finder/views.py:110-238