-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
134 lines (117 loc) · 5.57 KB
/
Copy pathutils.py
File metadata and controls
134 lines (117 loc) · 5.57 KB
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
import csv
import os
import random
def create_circle_in_rectangular_csv(input_filepath, output_filepath, radius, fill_value,
center_row=None, center_col=None,
do_not_replace_values=None,
random_center_target_value="0"):
"""
Reads a rectangular CSV. If center_row/col are not provided, it randomly selects
a center from cells matching random_center_target_value.
Places a 'circle' of fill_value around the center index.
Optionally, a specific value can be preserved and not replaced.
Args:
input_filepath (str): Path to the input CSV file.
output_filepath (str): Path to save the modified CSV file.
radius (int): The radius of the circle (Manhattan distance).
fill_value (any): The value to place in the circle cells.
It will be converted to a string for CSV writing.
center_row (int, optional): Row index of the circle's center. If None, random selection is attempted.
center_col (int, optional): Column index of the circle's center. If None, random selection is attempted.
do_not_replace_value (any, optional): If a cell's current value matches this,
it will not be replaced. Defaults to None.
random_center_target_value (str, optional): The value to look for when randomly selecting a center.
Defaults to "0".
"""
data = []
try:
with open(input_filepath, mode='r', newline='', encoding='utf-8') as infile:
reader = csv.reader(infile)
for r in reader:
data.append(list(r))
except FileNotFoundError:
print(f"Error: Input file '{input_filepath}' not found.")
return
except Exception as e:
print(f"Error reading CSV: {e}")
return
if not data:
print("Error: CSV file is empty.")
return
num_rows = len(data)
num_cols = len(data[0]) if num_rows > 0 else 0
if num_cols == 0 and num_rows > 0:
print("Error: CSV rows are empty (no columns).")
return
# Determine center coordinates
if center_row is None or center_col is None:
print(f"Attempting to find a random center with value '{random_center_target_value}'...")
possible_centers = []
for r_idx in range(num_rows):
for c_idx in range(num_cols):
if data[r_idx][c_idx] == str(random_center_target_value): # Compare with string version
possible_centers.append((r_idx, c_idx))
if not possible_centers:
print(f"Error: No cells found with value '{random_center_target_value}' to pick a random center.")
return
center_row, center_col = random.choice(possible_centers)
print(f"Randomly selected center: ({center_row}, {center_col})")
else:
# Validate provided center coordinates
if not (0 <= center_row < num_rows):
print(f"Error: Provided center row {center_row} is out of bounds (0-{num_rows-1}).")
return
if not (0 <= center_col < num_cols):
print(f"Error: Provided center column {center_col} is out of bounds (0-{num_cols-1}).")
return
print(f"Using provided center: ({center_row}, {center_col})")
fill_value_str = str(fill_value)
do_not_replace_values_str_list = []
if do_not_replace_values is not None:
if isinstance(do_not_replace_values, list):
do_not_replace_values_str_list = [str(val) for val in do_not_replace_values]
else: # Assume it's a single value
do_not_replace_values_str_list = [str(do_not_replace_values)]
if do_not_replace_values_str_list: # Check if the list is not empty
print(f"Values that will not be replaced: {do_not_replace_values_str_list}")
modified_data = [list(row_content) for row_content in data]
for r_idx in range(num_rows):
for c_idx in range(num_cols):
distance = abs(r_idx - center_row) + abs(c_idx - center_col)
if distance <= radius:
if do_not_replace_values and modified_data[r_idx][c_idx] in do_not_replace_values:
continue
modified_data[r_idx][c_idx] = fill_value_str
try:
with open(output_filepath, mode='w', newline='', encoding='utf-8') as outfile:
writer = csv.writer(outfile)
writer.writerows(modified_data)
print(f"Successfully created circle and saved to '{output_filepath}'")
return (center_col, center_row)
except Exception as e:
print(f"Error writing CSV: {e}")
if __name__ == "__main__":
input_file = "data/sunset_heatmap.csv"
output_file = "data/defender_sunset_heatmap.csv"
# --- Example 1: Randomly chosen center (looking for "0") ---
print(f"\n--- Running Example 1 (Random Center) ---")
circle_radius_random = 20
value_to_fill_random = 4
value_to_preserve = ["3", "1"]
center_col, center_row = create_circle_in_rectangular_csv(
input_filepath=input_file,
output_filepath=output_file,
radius=circle_radius_random,
fill_value=value_to_fill_random,
do_not_replace_values=value_to_preserve,
random_center_target_value="0"
)
create_circle_in_rectangular_csv(
input_filepath=output_file,
output_filepath=output_file,
center_row=center_row,
center_col=center_col,
radius=10,
fill_value=2,
do_not_replace_values=value_to_preserve
)