forked from npatwari/rti
-
Notifications
You must be signed in to change notification settings - Fork 0
/
histColumns.py
162 lines (142 loc) · 5.5 KB
/
histColumns.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#! /usr/bin/env python
#
# LICENSE:
# Copyright (C) 2016 Neal Patwari
#
# 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/>.
#
# Author: Neal Patwari, [email protected]
#
#
# Version History:
#
# Version 1.0: 15 Sept 2014: Initial Release
#
# Version 2.0: 13 Sept 2016: Update to include particular command line options:
# - plot particular channels rather than all;
# - change number of channels or number of nodes via command line;
# - generally to use the getopt, ast packages for command line inputs;
# - allow up to eight channels.
#
# Version 2.1: 19 Sept 2016: Update to allow a single link to be plotted.
#
#
# Usage:
# python histColumns.py -f file1.txt -f file2.txt ... -f filen.txt
# -l "[rx1,tx1,ch1]" -l "[rx2,tx2,ch2]" ... -l "[rxn,txn,chn]"
#
# where file#.txt was created using, for example:
# python listenSomeColumns_v2.py > file#.txt
# where rx#, tx#, ch# are the receiver id, transmitter id, and channel number
# for a link that you want to plot RSS data for.
#
# Purpose: Plot a histogram of RSS for particular columns (links)
# measured. Compare histograms when using with multiple files,
# in separate histograms stacked on top of each other.
import ast
import getopt
import sys
import matplotlib.pyplot as plt
import numpy as np
import rss
# Accept command line inputs to list the links to plot
linkCombosToPlot = []
fileList = []
numNodes = 10 # Xandem kits come with 10 nodes
numChs = 8 # Xandem kits come programmed with 8 channels
myopts, args = getopt.getopt(sys.argv[1:], "l:n:f:c:")
for o, a in myopts:
if o == "-l":
inputList = ast.literal_eval(a)
linkCombosToPlot.append(inputList)
elif o == "-n":
numNodes = int(a)
elif o == "-c":
numChs = int(a)
elif o == "-f":
fileList.append(a)
files = len(fileList)
streams = len(linkCombosToPlot)
if files == 0:
sys.exit('Usage: histColumns.py -f file1.txt -f file2.txt ... -l "[1,2,3]" ...')
print("Plotting each file in a subplot from top to bottom:")
print(fileList)
print("Plotting links:")
print(linkCombosToPlot)
# Convert the [tx,rx,ch] combos into column numbers
linkNumList = []
nodeList = list(range(1, numNodes + 1))
channelList = list(range(numChs))
for l in linkCombosToPlot:
linkNumList.append(rss.linkNumForTxRxChLists(l[0], l[1], l[2], nodeList, channelList))
links = len(linkNumList)
# Inputs / Settings
startSkip = 0
markerlist = ['-o', '-s', '-v', '-*', '-8', '-p', '-+', '-x']
xbincenters = list(range(-110, -10)) # lowest to highest POSSIBLE rss
# Create the bin edges.
xbinedges = [x - 0.5 for x in xbincenters]
xbinedges.append(xbincenters[-1] + 0.5)
# Init the plot.
plt.ion()
plt.cla()
junk = plt.figure()
fig = plt.figure()
# Keep track of min and max of data. Initialize by giving
# absurdly low max value and absurdly high min value.
minrss = 0
maxrss = -110
# Load data from each file.
for i, file in enumerate(fileList):
data = np.loadtxt(file, dtype=float, delimiter=' ', skiprows=startSkip)
cols = data.shape[1]
# Remove 127 values from, and plot a histogram, for each column
good_data = [data[data[:, k] < 127, k] for k in linkNumList]
# Print error message if a link has NO good data
for j in range(links):
if not good_data[j]:
sys.exit("Error: Link " + str(
linkCombosToPlot[j]) + " in file " + file + " has no non-127 values.")
# Update the min and max
maxrss = max(maxrss, max([gd.max() for gd in good_data]))
minrss = min(minrss, min([gd.min() for gd in good_data]))
# I don't want the histogram "bars", just the count.
# So I'm calling hist() for a plot I don't want to look at.
axjunk = junk.add_subplot(files, 1, i + 1)
axfig = fig.add_subplot(files, 1, i + 1)
n, bins, patches = axjunk.hist(good_data, histtype='bar', bins=xbinedges)
# Plot the probability mass function for each channel's
# (good) data, normalized so the pmf sums to one.
if links > 1:
for j, count in enumerate(n):
# This commented line caused errors in past versions of matplotlib
# because "count" was integers and so integer division would give zero.
# axfig.plot(xbincenters, count/sum(count), markerlist[j])
total = float(count.sum())
axfig.plot(xbincenters, count / total, markerlist[j])
# if one link, the n variable is only a single list, not a list of lists,
# so you can't use enumerate(n) and get the same result.
else:
total = float(n.sum())
axfig.plot(xbincenters, n / total, markerlist[0])
# Additional formatting done on each subplot
for i in range(files):
ax = plt.subplot(files, 1, i + 1)
ax.set_xlim([minrss - 1, maxrss + 1])
ax.set_ylabel('Probability Mass')
ax.grid()
ax = plt.subplot(files, 1, 1)
ax.set_xlabel('RSS Value (dBm)')
# Wait for user to hit enter so that the plot doesn't disappear immediately
input("Hit enter to end.")