Commit dc7c0afb authored by Peter Jansweijer's avatar Peter Jansweijer

Reads the TIC "freq_cnt_keysight_53230A" in a directory and plots trend.

parent eb3be8aa
#!/usr/bin/python
"""
trend.py:
Reads the TIC "freq_cnt_keysight_53230A" in a directory. Each file contains multiple
measurements. The mean value is calculated and added to an array.
A directory may contain multiple measurements, each with a set of "freq_cnt_keysight_53230A"
files. For example, 10 measurements are done using 25 link restarts each so there
are 250 files.
The link restart number must be forwarded to "trend.py" such that it knows at what file
one measurement ends and another starts.
Outliers are reported and their values are replaced by the average values recorded so far.
An output file is created that reports the mean values of all tic-measurement files
per measurement (excluding outliers!)
-------------------------------------------------------------------------------
Copyright (C) 2016 Peter Jansweijer, Henk Peek
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>
-------------------------------------------------------------------------------
Usage:
trend.py -name dir
trend.py -h | --help
Options:
-h --help Show this screen.
-name pointer to a directory with measurement files "freq_cnt_keysight_53230A"
-o <dir> optional directory for output file storage, default: "data/"
-r <number> number of link restarts over which the delta_delay is averaged (default = 10)
-t <number> tolerance [ps], skip outliers that are offset from the mean value
"""
import os
import sys
import numpy
import scipy
import time
import matplotlib.pyplot as plt
import pdb
# Add parent directory (containing 'lib') to the module search path
lib_path = (os.path.dirname(os.path.abspath(__file__)))
lib_path = os.path.join(lib_path,"..")
sys.path.insert(0,lib_path)
# private imports:
import lib.Keysight_53230A as tic
############################################################################
def save_plot(out_file, name, nameref, measurement_str, measurement_lst, outliers):
"""
outfile <file> handle to outfput file
name <str> name of the input file (with fiber spool PPS skew measurements)
nameref <str> name of the input file (with reference setup PPS skew measurements)
measurement_str <str> processed measurement
measurement_lst <list> list of PPS skew measurements (from multiple link restarts)
outliers <int> number of outliers encountered while processing
"""
measurement_str_shortend = measurement_str.replace("# Selected ITU channel for ","")
measurement_str_shortend = measurement_str_shortend.replace("\n","")
meas_arr= numpy.array(measurement_lst)
mean = meas_arr.mean()
stdev = meas_arr.std(ddof=1)
#out_file.write("outliers: " + str(outliers) + "\n")
#out_file.write("mean: " + str(mean) + "\n")
#out_file.write("stdev: " + str(stdev) + "\n")
out_file.write(str(mean) + ", " + str(stdev) + ", " +str(outliers) + ", " +str(measurement_str_shortend) + "\n")
fig = plt.figure("PPS_skew_fiberspool - PPS_skew_reference [ps]")
ax = fig.add_subplot(111)
ax.set_ylabel('PPS difference [ps]')
ax.set_title("PPS_skew_fiberspool - PPS_skew_reference [ps]")
ax.set_xlabel('Link restart number')
fig_str = measurement_str_shortend + "\n" + "file: "+name + "\n" + "ref file: "+nameref + "\n" + "outliers: " + str(outliers) + "\n" + "mean: " + str(mean) + "\n" +"stdev: " + str(stdev)
ax.text(0.01, 0.70, fig_str, transform=ax.transAxes)
ax.plot(range(1,len(meas_arr)+1), meas_arr, color = 'blue', label='data')
png_name = measurement_str_shortend.replace("[","")
png_name = png_name.replace("]","")
png_name = png_name.replace("(","")
png_name = png_name.replace(")","")
png_name = png_name.replace(":","")
png_name = png_name.replace(",","")
png_name = png_name.replace(" ","_")
png_name = png_name.replace("->","2")
fig.savefig(output_dir+png_name+".png")
fig.clf() # clear figure (it was stored in a png file)
return
############################################################################
###############################################
# Main
###############################################
"""
Usage:
trend.py -name dir
trend.py -h | --help
Options:
-h --help Show this screen.
-name pointer to a directory with measurement files "freq_cnt_keysight_53230A"
-o <dir> optional directory for output file storage, default: "data/"
-r <number> number of link restarts over which the delta_delay is averaged (default = 10)
-t <number> tolerance [ps], skip outliers that are offset from the mean value
"""
if __name__ == "__main__":
#arguments = docopt(__doc__,version='White Rabbit controled via serial port')
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("name", help="directory containing measurement files freq_cnt_keysight_53230A")
parser.add_argument("-output_dir", default="data")
parser.add_argument("-r", default=10)
parser.add_argument("-t", default=200)
args = parser.parse_args()
name = args.name
restarts = int(args.r)
tolerance = int(args.t)
print("Used directory: ",name)
print("Output directory: ",args.output_dir)
print("link restarts: ",restarts)
print("tolerance: " + str(tolerance) + " [ps]")
tolerance = tolerance * 1e-12 # tolerance in [ps]!
files = []
# Figure out if the input is a single measurement file or a directory with files
if os.path.exists(name) == True:
if os.path.isdir(name) == True:
print(name+": is a directory")
# Any result files already there will be removed
if os.path.isfile(os.path.join(name,"result.txt")) == True:
os.remove(os.path.join(name, "result.txt"))
# Get a file list (exclude files with extensions!)
for file_name in os.listdir(name):
if len(file_name.split('.')) <= 1:
files.append(os.path.join(name, file_name))
else:
print(name+": is a file, not a directory.")
sys.exit()
else:
print(name+": directory does not exist")
# add trailing slash if not present
output_dir = os.path.join(args.output_dir,'')
if os.path.exists(output_dir) != True:
os.mkdir(output_dir)
print("Output directory does not exist => created: "+output_dir)
timestamp = time.localtime()
filename=output_dir+time.strftime(format("%y%m%d_%H_%M_%S"),timestamp)+"_PPS_skew_mean_values"
print("save PPS-Skew measurements average values into file:",filename)
out_file = open(filename,"w")
out_file.write("# PPS-Skew measurements average values\n")
out_file.write("# date:"+time.strftime(format("%d %b %Y"),timestamp)+"\n")
out_file.write("# time:"+time.strftime(format("%H:%M:%S"),timestamp)+"\n")
out_file.write("# mean, StdDev, outliers\n")
cnt_restart = 0
file_average = []
outlier_cnt = 0
outlier_in_meas = 0
fig = plt.figure("Trend")
ax = fig.add_subplot(111)
ax.set_ylabel('PPS difference [ps]')
ax.set_title("PPS_skew Trend[ps]")
ax.set_xlabel('measurement number')
ax.text(0.01, 0.95, str(name), transform=ax.transAxes)
for tic_file in sorted(files):
try:
#print(tic_file+"\n")
data_file = open(tic_file,"r")
line = data_file.readline()
data_file.close()
if line.strip() == "#MeasurementData:Keysight 53230A":
tic_meas = tic.file_to_scipy_array(tic_file)
tic_mean = tic_meas[1].mean() # the average value of all measurements in this file
if len(file_average) > 0:
dirty_arr = numpy.array(file_average) # Take the average of all values up to now
dirty_mean = dirty_arr.mean()
if abs(tic_mean - dirty_mean) > tolerance:
outlier_cnt = outlier_cnt + 1
outlier_in_meas = outlier_in_meas + 1
print("outlier? " + tic_file)
print("average = " + str(tic_mean) + "\n")
dirty_arr = numpy.array(file_average) # Take the average of all values up to now
file_average.append(dirty_arr.mean()) # and replace the outlier by this mean value.
else:
file_average.append(tic_mean)
else:
file_average.append(tic_mean)
cnt_restart = cnt_restart + 1
except:
print (tic_file + ": wrong file type")
continue
if cnt_restart == restarts:
# Keep track of statistics per measurement
file_average_arr = numpy.array(file_average)
out_file.write(str(file_average_arr.mean()) + ", " + str(file_average_arr.std(ddof=1)) + ", " + str(outlier_in_meas) + "\n")
cnt_restart = 0
file_average = []
outlier_in_meas = 0
ax.plot(range(len(file_average_arr)), file_average_arr)
#print(len(file_average_arr))
ax.text(0.01, 0.90, "tolerance: " + str(tolerance) + "; outliers: " + str(outlier_cnt), transform=ax.transAxes)
plt.draw()
plt.show()
out_file.close()
sys.exit()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment