Skip to content

Commit 146cdb8

Browse files
committed
Addition of spatialResolution_StdDevDistribution_creator.py to create the files for SpatialResolution 2D distributions
1 parent a0b96f5 commit 146cdb8

File tree

1 file changed

+73
-0
lines changed

1 file changed

+73
-0
lines changed
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import os
2+
import matplotlib.pyplot as plt
3+
import pandas as pd
4+
import numpy as np
5+
import argparse
6+
7+
# Function to pool the matrix
8+
def pool_matrix(df, N_prime, M_prime):
9+
# Get the original dimensions
10+
N, M = df.shape
11+
12+
# Calculate block sizes
13+
A = N // N_prime
14+
B = M // M_prime
15+
16+
if N % N_prime != 0 or M % M_prime != 0:
17+
raise ValueError("N and M must be divisible by N' and M' respectively.")
18+
19+
# Reshape into blocks and calculate mean for each block
20+
pooled = (
21+
df.values.reshape(N_prime, A, M_prime, B) # Reshape into blocks
22+
.mean(axis=(1, 3)) # Average over the blocks
23+
)
24+
25+
# Convert back to DataFrame for better readability
26+
return pd.DataFrame(pooled)
27+
28+
# Function to write matrix with coordinates
29+
def write_matrix_with_coordinates(df, lenX, lenY, outputfile="output.txt"):
30+
# Get number of rows and columns from the matrix shape
31+
rows, cols = df.shape
32+
33+
# Generate coordinate arrays for X and Y
34+
x_coords = np.linspace(-lenX/2, lenX/2, cols)
35+
y_coords = np.linspace(-lenY/2, lenY/2, rows)
36+
37+
# Open file to write the output
38+
with open(outputfile, "w") as file:
39+
# Write header row with x coordinates
40+
file.write(" ".join([f"{x:.2f}" for x in x_coords]) + "\n")
41+
42+
# Write each row with y coordinates and corresponding matrix data
43+
for y, row_data in zip(y_coords, df.values):
44+
file.write(f"{y:.2f}" + " " + " ".join(map(str, row_data)) + "\n")
45+
46+
return y_coords, df
47+
48+
# Main function that will be executed when the script runs
49+
def main():
50+
# Parse command-line arguments
51+
parser = argparse.ArgumentParser(description="Process matrix with coordinate output.")
52+
53+
# Define the flags and their corresponding arguments
54+
parser.add_argument('-i', '--inputfile', type=str, required=True, help="Input file path")
55+
parser.add_argument('-o', '--outputfile', type=str, required=True, help="Output file path")
56+
parser.add_argument('-n', '--NumRows', type=int, required=True, help="Number of rows of the final matrix.")
57+
parser.add_argument('-m', '--NumColumns', type=int, required=True, help="Number of columns of the final matrix.")
58+
parser.add_argument('-x', '--lenX', type=float, required=True, help="Length of X axis")
59+
parser.add_argument('-y', '--lenY', type=float, required=True, help="Length of Y axis")
60+
61+
args = parser.parse_args()
62+
63+
# Read the input file into a DataFrame
64+
df = pd.read_csv(args.inputfile, sep='\s+', header=None, index_col=None)
65+
66+
# Pool the matrix if necessary
67+
pooled_df = pool_matrix(df, args.N_prime, args.M_prime)
68+
69+
# Write the matrix with coordinates to the output file
70+
write_matrix_with_coordinates(pooled_df, lenX=args.lenX, lenY=args.lenY, outputfile=args.outputfile)
71+
72+
if __name__ == "__main__":
73+
main()

0 commit comments

Comments
 (0)