Commit bc197f986d6a2e1db893abed5dd1870955245a84
1 parent
bdcd55cc
Add cscripts.ioutils.read_pull_values() function (#1828)
This function avoids code duplication in show_pull_histogram.py and show_pull_evolution.py.
Showing
3 changed files
with
60 additions
and
106 deletions
cscripts/ioutils.py
| @@ -65,6 +65,61 @@ def write_csv_row(outfile, row, colnames, colvalues): | @@ -65,6 +65,61 @@ def write_csv_row(outfile, row, colnames, colvalues): | ||
| 65 | return | 65 | return |
| 66 | 66 | ||
| 67 | 67 | ||
| 68 | +# ================ # | ||
| 69 | +# Read pull values # | ||
| 70 | +# ================ # | ||
| 71 | +def read_pull_values(filename, parname): | ||
| 72 | + """ | ||
| 73 | + Read pull values from pull distribution ASCII file | ||
| 74 | + | ||
| 75 | + Parameters | ||
| 76 | + ---------- | ||
| 77 | + filename : str | ||
| 78 | + Pull distribution ASCII file | ||
| 79 | + parname : str | ||
| 80 | + Parameter | ||
| 81 | + | ||
| 82 | + Returns | ||
| 83 | + ------- | ||
| 84 | + values : list of float | ||
| 85 | + List of pull values | ||
| 86 | + | ||
| 87 | + Raises | ||
| 88 | + ------ | ||
| 89 | + NameError | ||
| 90 | + """ | ||
| 91 | + # Initialise list of values | ||
| 92 | + values = [] | ||
| 93 | + | ||
| 94 | + # Open reader | ||
| 95 | + reader = csv.reader(open(filename, 'r'), delimiter=',') | ||
| 96 | + | ||
| 97 | + # Read rows | ||
| 98 | + first = True | ||
| 99 | + index = -1 | ||
| 100 | + for row in reader: | ||
| 101 | + | ||
| 102 | + # Get column index if first row | ||
| 103 | + if first: | ||
| 104 | + try: | ||
| 105 | + index = row.index(parname) | ||
| 106 | + except: | ||
| 107 | + print('ERROR: Parameter "'+parname+'" not found in list:') | ||
| 108 | + for p in row: | ||
| 109 | + print(' "'+p+'"') | ||
| 110 | + raise NameError(parname) | ||
| 111 | + | ||
| 112 | + # Handle data rows | ||
| 113 | + else: | ||
| 114 | + values.append(float(row[index])) | ||
| 115 | + | ||
| 116 | + # Flag that first row has been passed | ||
| 117 | + first = False | ||
| 118 | + | ||
| 119 | + # Return values | ||
| 120 | + return values | ||
| 121 | + | ||
| 122 | + | ||
| 68 | # ============================================ # | 123 | # ============================================ # |
| 69 | # Get arguments and options from argument list # | 124 | # Get arguments and options from argument list # |
| 70 | # ============================================ # | 125 | # ============================================ # |
examples/show_pull_evolution.py
| @@ -35,55 +35,6 @@ except: | @@ -35,55 +35,6 @@ except: | ||
| 35 | sys.exit() | 35 | sys.exit() |
| 36 | 36 | ||
| 37 | 37 | ||
| 38 | -# ====================== # | ||
| 39 | -# Read pull distribution # | ||
| 40 | -# ====================== # | ||
| 41 | -def read_pull(filename, parname): | ||
| 42 | - """ | ||
| 43 | - Read pull distribution | ||
| 44 | - | ||
| 45 | - Parameters | ||
| 46 | - ---------- | ||
| 47 | - filename : str | ||
| 48 | - Pull distribution ASCII file | ||
| 49 | - parname : str | ||
| 50 | - Parameter | ||
| 51 | - """ | ||
| 52 | - # Initialise list | ||
| 53 | - values = [] | ||
| 54 | - | ||
| 55 | - # Open reader | ||
| 56 | - reader = csv.reader(open(filename, 'r'), delimiter=',') | ||
| 57 | - | ||
| 58 | - # Read rows | ||
| 59 | - first = True | ||
| 60 | - index = -1 | ||
| 61 | - for row in reader: | ||
| 62 | - | ||
| 63 | - # Get column index if first row | ||
| 64 | - if first: | ||
| 65 | - try: | ||
| 66 | - index = row.index(parname) | ||
| 67 | - except: | ||
| 68 | - print('ERROR: Parameter "'+parname+'" not found in list:') | ||
| 69 | - for p in row: | ||
| 70 | - print(' "'+p+'"') | ||
| 71 | - raise NameError(parname) | ||
| 72 | - | ||
| 73 | - # Handle data rows | ||
| 74 | - else: | ||
| 75 | - values.append(float(row[index])) | ||
| 76 | - | ||
| 77 | - # Flag that first row has been passed | ||
| 78 | - first = False | ||
| 79 | - | ||
| 80 | - # Create numpy array | ||
| 81 | - a = np.array(values) | ||
| 82 | - | ||
| 83 | - # Return array | ||
| 84 | - return a | ||
| 85 | - | ||
| 86 | - | ||
| 87 | # =================== # | 38 | # =================== # |
| 88 | # Plot pull evolution # | 39 | # Plot pull evolution # |
| 89 | # =================== # | 40 | # =================== # |
| @@ -101,7 +52,7 @@ def plot_pull_evolution(filename, parname, plotfile): | @@ -101,7 +52,7 @@ def plot_pull_evolution(filename, parname, plotfile): | ||
| 101 | Plot filename | 52 | Plot filename |
| 102 | """ | 53 | """ |
| 103 | # Read values from CSV file | 54 | # Read values from CSV file |
| 104 | - values = read_pull(filename, parname) | 55 | + values = np.array(cscripts.ioutils.read_pull_values(filename, parname)) |
| 105 | 56 | ||
| 106 | # Compute mean and rms | 57 | # Compute mean and rms |
| 107 | nsamples = len(values) | 58 | nsamples = len(values) |
examples/show_pull_histogram.py
| @@ -36,55 +36,6 @@ except: | @@ -36,55 +36,6 @@ except: | ||
| 36 | sys.exit() | 36 | sys.exit() |
| 37 | 37 | ||
| 38 | 38 | ||
| 39 | -# ====================== # | ||
| 40 | -# Read pull distribution # | ||
| 41 | -# ====================== # | ||
| 42 | -def read_pull(filename, parname): | ||
| 43 | - """ | ||
| 44 | - Read pull distribution | ||
| 45 | - | ||
| 46 | - Parameters | ||
| 47 | - ---------- | ||
| 48 | - filename : str | ||
| 49 | - Pull distribution ASCII file | ||
| 50 | - parname : str | ||
| 51 | - Parameter | ||
| 52 | - """ | ||
| 53 | - # Initialise list | ||
| 54 | - values = [] | ||
| 55 | - | ||
| 56 | - # Open reader | ||
| 57 | - reader = csv.reader(open(filename, 'r'), delimiter=',') | ||
| 58 | - | ||
| 59 | - # Read rows | ||
| 60 | - first = True | ||
| 61 | - index = -1 | ||
| 62 | - for row in reader: | ||
| 63 | - | ||
| 64 | - # Get column index if first row | ||
| 65 | - if first: | ||
| 66 | - try: | ||
| 67 | - index = row.index(parname) | ||
| 68 | - except: | ||
| 69 | - print('ERROR: Parameter "'+parname+'" not found in list:') | ||
| 70 | - for p in row: | ||
| 71 | - print(' "'+p+'"') | ||
| 72 | - raise NameError(parname) | ||
| 73 | - | ||
| 74 | - # Handle data rows | ||
| 75 | - else: | ||
| 76 | - values.append(float(row[index])) | ||
| 77 | - | ||
| 78 | - # Flag that first row has been passed | ||
| 79 | - first = False | ||
| 80 | - | ||
| 81 | - # Create numpy array | ||
| 82 | - a = np.array(values) | ||
| 83 | - | ||
| 84 | - # Return array | ||
| 85 | - return a | ||
| 86 | - | ||
| 87 | - | ||
| 88 | # =================== # | 39 | # =================== # |
| 89 | # Plot pull histogram # | 40 | # Plot pull histogram # |
| 90 | # =================== # | 41 | # =================== # |
| @@ -104,7 +55,7 @@ def plot_pull_histogram(filename, parname, nbins, plotfile): | @@ -104,7 +55,7 @@ def plot_pull_histogram(filename, parname, nbins, plotfile): | ||
| 104 | Plot filename | 55 | Plot filename |
| 105 | """ | 56 | """ |
| 106 | # Read values from CSV file | 57 | # Read values from CSV file |
| 107 | - values = read_pull(filename, parname) | 58 | + values = np.array(cscripts.ioutils.read_pull_values(filename, parname)) |
| 108 | 59 | ||
| 109 | # Create histogram | 60 | # Create histogram |
| 110 | n, bins, patches = plt.hist(values, nbins, range=[-4.0,4.0], | 61 | n, bins, patches = plt.hist(values, nbins, range=[-4.0,4.0], |
| @@ -112,15 +63,12 @@ def plot_pull_histogram(filename, parname, nbins, plotfile): | @@ -112,15 +63,12 @@ def plot_pull_histogram(filename, parname, nbins, plotfile): | ||
| 112 | 63 | ||
| 113 | # Create expected distribution | 64 | # Create expected distribution |
| 114 | y = mlab.normpdf(bins, 0.0, 1.0) | 65 | y = mlab.normpdf(bins, 0.0, 1.0) |
| 115 | - l = plt.plot(bins, y, 'r-', linewidth=2) | ||
| 116 | - | ||
| 117 | - # Set parname for plotting | ||
| 118 | - name = parname | 66 | + plt.plot(bins, y, 'r-', linewidth=2) |
| 119 | 67 | ||
| 120 | # Set plot | 68 | # Set plot |
| 121 | - plt.xlabel('Pull ('+name+')') | 69 | + plt.xlabel('Pull ('+parname+')') |
| 122 | plt.ylabel('Arbitrary units') | 70 | plt.ylabel('Arbitrary units') |
| 123 | - plt.title(name) | 71 | + plt.title(parname) |
| 124 | plt.grid(True) | 72 | plt.grid(True) |
| 125 | 73 | ||
| 126 | # Show figure | 74 | # Show figure |