Programming Techniques for Data Analysis
Engineering Data Analysis Programs: From Inputs to Results
Engineering Data Analysis Programs turn an engineering calculation into a repeatable sequence of instructions. You might use one to summarize repeated force measurements or calculate a performance measure for several test runs. The central task is to express the calculation clearly: identify the inputs, apply the intended operations, and return results with meaningful units. Here, assume that the measurements supplied to the program have already been checked and are suitable for the calculation.
Specify the calculation before coding
A function contract states what a function receives, what it returns, and the conditions under which its calculation applies. For a function that calculates mean force, the contract might specify a nonempty sequence of equally weighted readings in newtons and a returned mean in newtons.
Write the governing calculation first. For n equally weighted readings,
The sum S and mean xˉ have the same units as each reading. The count n is dimensionless. Equally weighted readings are an assumption: averaging values without weights may answer a different question when readings represent different durations.
Parameters make choices explicit. If a calculation counts readings at or above a force limit, pass that limit into the function instead of hiding it inside the calculation.
Build a reusable function
A function packages a calculation so that you can call it with different inputs. This Python function implements the mean directly:
def mean_force(forces_N):
total_N = 0.0
count = 0
for force_N in forces_N:
total_N += force_N
count += 1
return total_N / countAn accumulator stores a running result as a loop processes successive values. Here, total_N accumulates force and count accumulates the number of readings. Initialize both before the loop so that each reading contributes to the same calculation.
The return statement sends the computed value back to the calling code. Printing a value displays it, but printing alone does not provide that value for a later calculation. Returning results lets another part of the program calculate a comparison, format a report, or combine outputs.
Place updates and decisions deliberately
A loop repeats work; a conditional decides whether particular work happens. For example, a conditional can increment a selected-reading count when force_N >= limit_N. The total number of readings should still include every reading if the required result is a fraction of the complete run.
Different summaries require different accumulated quantities:
Required result | Accumulated quantities | Final operation |
|---|---|---|
Sum of readings | Sum of all readings | Return the sum |
Mean of readings | Sum and total count | Divide sum by total count |
Fraction meeting a condition | Selected count and total count | Divide selected count by total count |
Indentation determines which statements repeat in Python. A final division normally belongs after all readings have been processed. A return inside the loop exits the function as soon as execution reaches it, which can leave later readings unused.
Check behavior and report clearly
Use small cases with predictable results to check the program. Identical readings should produce that same mean. Reordering equally weighted readings should preserve the mathematical result, although floating-point arithmetic can introduce tiny numerical differences. Repeating a function call with the same inputs should give the same result when the function depends only on those inputs.
Check units alongside the arithmetic. A force mean remains in newtons; a selected fraction has no units. Multiply a fraction by 100 only when reporting a percentage. Retain full working precision and round the final reported value to the requested precision.
What to remember
- State inputs, outputs, units, and assumptions before coding.
- Initialize accumulators where each independent calculation begins.
- Use loops for repetition and conditionals for selection.
- Return the completed result and round when reporting it.
Example: Summarizing Repeated Load-Cell Readings
A fixture test produces four equally weighted force readings: 120, 135, 145, and 140 N. An engineer needs the mean force and the fraction of readings at or above a specified limit. For this run, the limit is 140 N. Assume each input list is nonempty and its readings have already been checked.
Use Python. Retain full precision in calculations, then report mean forces to one decimal place and percentages to one decimal place.
a) Write a function that accepts the readings and limit as arguments and returns the mean force and selected fraction. Keep display formatting outside the function.
Use a running sum for the mean and a separate counter for readings meeting the inclusive limit. Both accumulators start at ________ inside the function, so every call begins a new calculation.
Write the following Python lines in order. The indentation specified for each line determines which statements belong to the function, loop and conditional:
- With no indentation, define the function: def summarize_forces(forces_N, limit_N):
- Indent four spaces: total_N = 0.0
- Indent four spaces: selected = 0
- Indent four spaces: for force_N in forces_N:
- Indent eight spaces: total_N += force_N
- Indent eight spaces: if force_N >= limit_N:
- Indent twelve spaces: selected += 1
- Return to four spaces, ending the loop: count = len(forces_N)
- Indent four spaces: mean_N = total_N / count
- Indent four spaces: fraction = selected / count
- Indent four spaces: return mean_N, fraction
The sum update occurs for every reading. The selected-count update occurs only when its condition is true. After the loop, dividing by the complete list length gives the equally weighted mean and the fraction of all readings meeting the limit. The returned mean has units of newtons; the returned fraction is dimensionless.
b) Trace the function for the fixture test and calculate both reported results.
Call the function with the supplied readings and the 140 N limit using mean_N, fraction = summarize_forces([120, 135, 145, 140], 140).
The running values change as follows. A reading equal to the limit contributes to the selected count because the condition uses >=.
- After 120 N: the running sum is 120 N and the selected count is 0.
- After 135 N: the running sum is 255 N and the selected count is 0.
- After 145 N: the running sum is 400 N and the selected count is 1.
- After 140 N: the running sum is 540 N and the selected count is 2.
There are ________ readings, so
The selected fraction and percentage are
The function returns (135.0, 0.5). Report a mean force of 135.0 N and 50.0% of readings at or above the limit. The function returns a fraction; multiplying by 100 belongs to percentage reporting.
c) Reuse the function for readings of 100, 110, and 120 N with a new limit of 115 N. Explain why the earlier run does not affect the result.
Pass the new readings and limit as arguments using mean_N, fraction = summarize_forces([100, 110, 120], 115). There is no need to edit the function or manually clear its previous results.
On entering the function, total_N and selected are initialized again. The new running sum is
Only the 120 N reading meets the new limit. With ________ readings in this call,
The reported percentage is
Report 110.0 N and 33.3%. The earlier run contributes nothing because the accumulators are local variables initialized during each function call. The function returns the unrounded fraction for any later calculations.
A program calculates the fraction of five force readings that meet or exceed a specified limit. All readings and the limit are in newtons.
def selected_fraction(forces_N, limit_N):
selected = 0
for force_N in forces_N:
if force_N >= limit_N:
selected += 1
return selected / len(forces_N)
fraction = selected_fraction([95, 100, 105, 110, 90], 100)What value is assigned to fraction?
An engineering prediction program has three force residuals, defined as predicted force minus measured force: 3.0, −4.0, and 0.0 N. The following function calculates their root-mean-square error:
def rms_error(errors_N):
squared_total = 0.0
for error_N in errors_N:
squared_total += error_N ** 2
return (squared_total / len(errors_N)) ** 0.5
result_N = rms_error([3.0, -4.0, 0.0])What is result_N? Answer in newtons, to two decimal places, entering only the number.