Command Line Interface (CLI)
Relevant source files
The following files were used as context for generating this wiki page:
The Command Line Interface (CLI) in this project serves as the primary entry point for executing statistical analysis on medical datasets. It operates by invoking Python scripts that ingest raw text files, perform mathematical computations using scientific libraries, and output descriptive statistics directly to the terminal.
The system is designed to handle specific data formats, such as cholesterol and weight measurements, by reading newline-separated numeric values from local files. The interface focuses on providing immediate feedback through standard output for various statistical metrics including central tendency and dispersion.
Sources: hw1/p1.py:1-63, hw1/CHOLESTEROL.txt:1-28, hw1/WEIGHT.txt:1-12
Architecture and Data Flow
The CLI logic is encapsulated within functional blocks that manage file I/O and data processing. The workflow starts with a file name being passed to a computation function, which then delegates file reading to a specialized utility.
Data Processing Pipeline
The following diagram illustrates the flow from file input to terminal output:
The diagram shows the iterative process of reading lines from a text file and the conditional logic used to handle non-numeric data before final computation.
Sources: hw1/p1.py:6-21
Key Components and Functions
The CLI functionality is built upon several core Python functions that handle different stages of the data lifecycle.
File Handling
The open_file(file_name) function serves as the data ingestion layer. It opens a specified file in read mode, strips whitespace, and attempts to cast each line to a float. It includes basic error handling for ValueError to ensure the program does not crash on malformed input.
Sources: hw1/p1.py:7-17
Statistical Computation Engine
The compute_file(file_name) function acts as the main controller. It orchestrates the call to open_file and then utilizes numpy and statistics libraries to generate metrics.
Sources: hw1/p1.py:19-21
| Metric | Method/Formula | Library |
|---|---|---|
| Mean | np.mean(data) | NumPy |
| Median | np.median(data) | NumPy |
| Mode | statistics.mode(data) | Statistics |
| Range | np.ptp(data) | NumPy |
| Standard Deviation | np.std(data) | NumPy |
| Variance | np.var(data) | NumPy |
| Coeff. of Variation 1 | 3*(mean - median) / std | Custom |
| Coeff. of Variation 2 | sum((x-mean)**3)/(len*std**3) | Custom |
Sources: hw1/p1.py:25-58
Implementation Details
The interface relies on standard Python print() statements to communicate results to the user. Each calculation is labeled and displayed sequentially.
def compute_file(file_name):
open_file(file_name)
print(file_name)
# Example: Calculate Mean
mean = np.mean(data)
print("Mean: ", mean)
Sources: hw1/p1.py:19-27
Sequence of Operations
The CLI execution sequence for a typical analysis task is shown below:
Sources: hw1/p1.py:19-63
Summary
The CLI provides a straightforward mechanism for processing medical data files (CHOLESTEROL.txt, WEIGHT.txt). By combining standard file I/O with robust numerical libraries, it transforms raw data into a suite of descriptive statistics displayed directly in the user's terminal. This modular approach allows for different datasets to be analyzed by simply changing the file path argument in the script's entry point.
Sources: hw1/p1.py:61-63