#!/usr/bin/python3
# ===================================================================
#
# ===================================================================
import numpy as np
import draw_xy_axes as ax
import coordinate_conversion as cc
import user_interface as ui
from graphics import *
import random
import sys
popfile = 'bell_curve_data.dat'
# -------------------------------------------------------------------
# ---- read population data from a file
# ---- return a list of population values
# -------------------------------------------------------------------
def pop_data_list(fn):
pop = [] # population data from file
lc = 0 # line count
inFile = open(fn,'r') # open population data file
for l in inFile: # read each line
l = l.strip() # strip whitespace
tf,i = ui.is_integer(l) # test/convert to integer
if not tf: # non-integer?
print()
print(f'non-integer in input data ({l})')
print('exit program')
print()
sys.exit()
pop.append(i) # save value in the list
lc += 1 # increment line read count
inFile.close() # close population data file
print()
print(f'{lc} population values read from file ({fn})')
return pop # return population list
# ------------------------------------------------------------------
# ---- return a list of samples from the population list
# ---- poplst - population data list
# ---- samsiz - size of sample
# ---- -------------------------------------------------------------
# ---- Note: the population is a normal distribution (bell curve)
# ------------------------------------------------------------------
def sample(poplst,samsiz):
poplen = len(poplst)
# ---- collect samsiz sample
sam = []
for _ in range(samsiz):
i = random.randint(0,poplen-1)
sam.append(poplst[i])
# ---- calculate mean and standard deviation
avg = np.mean(sam) # average (mean)
std = np.std(sam) # standard deviation
return (sam,avg,std)
# -------------------------------------------------------------------
# ---- main
# -------------------------------------------------------------------
# ---- get population data
poplst = pop_data_list(popfile)
print()
print(f'population size: {len(poplst)}')
# ---- size of sample from population
samsiz = 21 # sample size
sam,avg,std = sample(poplst,samsiz)
print()
print(f'Mean : {avg:.02f}')
print(f'STD : {std:.02f}')
print(f'Sample: {sam}')
print()