|
| 1 | +#!/usr/bin/python |
| 2 | +""" |
| 3 | +Script: 2D_generate_histogram.py |
| 4 | +Author: Asaminew Aytenfisu |
| 5 | +Description: This script generates a histogram from data points. |
| 6 | +""" |
| 7 | + |
| 8 | +import argparse |
| 9 | +import sys |
| 10 | +import math |
| 11 | +import numpy as np |
| 12 | + |
| 13 | +def read_angles(line): |
| 14 | + tokens = line.split() |
| 15 | + x = float(tokens[0]) |
| 16 | + y = float(tokens[1]) |
| 17 | + return [x, y] |
| 18 | + |
| 19 | +def main(args): |
| 20 | + try: |
| 21 | + data_file = args.data_file |
| 22 | + histogram_file = args.histogram_file |
| 23 | + x_min = args.x_min |
| 24 | + x_max = args.x_max |
| 25 | + y_min = args.y_min |
| 26 | + y_max = args.y_max |
| 27 | + x_resolution = args.x_resolution |
| 28 | + y_resolution = args.y_resolution |
| 29 | + |
| 30 | + points = [read_angles(line) for line in open(data_file)] |
| 31 | + count = len(points) |
| 32 | + histogram = np.zeros([x_resolution, y_resolution]) |
| 33 | + x_interval_length = (x_max - x_min) / x_resolution |
| 34 | + y_interval_length = (y_max - y_min) / y_resolution |
| 35 | + interval_surface = x_interval_length * y_interval_length |
| 36 | + increment = count / interval_surface |
| 37 | + |
| 38 | + for i in points: |
| 39 | + x = int((i[0] - x_min) / x_interval_length) |
| 40 | + y = int((i[1] - y_min) / y_interval_length) |
| 41 | + histogram[x,y] += increment |
| 42 | + |
| 43 | + x_intervals = np.arange(x_min, x_max, (x_max - x_min) / x_resolution) |
| 44 | + y_intervals = np.arange(y_min, y_max, (y_max - y_min) / y_resolution) |
| 45 | + |
| 46 | + with open(histogram_file, 'w') as o: |
| 47 | + for i, x in enumerate(x_intervals): |
| 48 | + for j, y in enumerate(y_intervals): |
| 49 | + if histogram[i,j] != 0.0: |
| 50 | + o.write('%f %f %f \n' % (x, y, -0.001982923700*298.0*math.log(histogram[i,j]/histogram.max()))) |
| 51 | + else: |
| 52 | + o.write('%f %f %f \n' % (x, y, 99999.0)) |
| 53 | + o.write('\n') |
| 54 | + |
| 55 | + except Exception as e: |
| 56 | + print("Error:", e) |
| 57 | + sys.exit(1) |
| 58 | + |
| 59 | +if __name__ == "__main__": |
| 60 | + parser = argparse.ArgumentParser(description="Generate a histogram from data points") |
| 61 | + parser.add_argument("data_file", help="input data file") |
| 62 | + parser.add_argument("histogram_file", help="output histogram file") |
| 63 | + parser.add_argument("x_min", type=float, help="minimum x value") |
| 64 | + parser.add_argument("x_max", type=float, help="maximum x value") |
| 65 | + parser.add_argument("y_min", type=float, help="minimum y value") |
| 66 | + parser.add_argument("y_max", type=float, help="maximum y value") |
| 67 | + parser.add_argument("x_resolution", type=int, help="x resolution") |
| 68 | + parser.add_argument("y_resolution", type=int, help="y resolution") |
| 69 | + args = parser.parse_args() |
| 70 | + main(args) |
0 commit comments