Compare commits

...

3 Commits

Author SHA1 Message Date
Diana
447e88b212 no idea 2024-10-25 17:10:56 +02:00
Diana
136e8a380c Merge branch 'main' of https://whale.am28.uni-tuebingen.de/git/mbergmann/gpgrewe2024 2024-10-25 17:09:46 +02:00
Diana
423fe451be Changes integral function 2024-10-25 17:09:29 +02:00
2 changed files with 36 additions and 60 deletions

View File

@ -72,14 +72,16 @@ functions_path = r"C:\Users\diana\OneDrive - UT Cloud\Master\GPs\GP1_Grewe\Proje
sys.path.append(functions_path) sys.path.append(functions_path)
import useful_functions as u import useful_functions as u
import matplotlib.ticker as ticker import matplotlib.ticker as ticker
import matplotlib.patches as mpatches
def float_formatter(x, _): def float_formatter(x, _):
"""Format the y-axis values as floats with a specified precision.""" """Format the y-axis values as floats with a specified precision."""
return f'{x:.5f}' return f'{x:.5f}'
def plot_highlighted_integrals(ax, frequency, power, points, color_mapping, points_categories, delta=2.5): def plot_highlighted_integrals(ax, frequency, power, points, color_mapping, points_categories, delta=2.5):
""" """
Highlight integrals on the existing axes of the power spectrum. Highlights integrals on the existing axes of the power spectrum for a given dataset.
Parameters Parameters
---------- ----------
@ -102,38 +104,40 @@ def plot_highlighted_integrals(ax, frequency, power, points, color_mapping, poin
------- -------
None None
""" """
ax.plot(frequency, power, color = "k") # Plot power spectrum on the existing axes _, _, AM, df, eodf, nyquist, stim_freq = u.sam_data(sam)
# Plot the power spectrum on the provided axes
ax.plot(frequency, power, color="k")
for point in points: for point in points:
# Calculate the integral and local mean # Identify the category for the current point
integral, local_mean = u.calculate_integral_2(frequency, power, point) point_category = next((cat for cat, pts in points_categories.items() if point in pts), "Unknown")
# Check if the point is valid # Assign color based on category, or default to grey if unknown
color = color_mapping.get(point_category, 'gray')
# Calculate the integral and check validity
integral, local_mean = u.calculate_integral_2(frequency, power, point)
valid = u.valid_integrals(integral, local_mean, point) valid = u.valid_integrals(integral, local_mean, point)
if valid: if valid:
# Define color based on the category of the point # Highlight valid points with a shaded region
point_category = next((cat for cat, pts in points_categories.items() if point in pts), "Unknown")
color = next((c for cat, c in color_mapping.items() if point in points_categories[cat]), 'gray')
# Shade the region around the point where the integral was calculated
ax.axvspan(point - delta, point + delta, color=color, alpha=0.2, label=f'{point_category}') ax.axvspan(point - delta, point + delta, color=color, alpha=0.2, label=f'{point_category}')
# Text with categories and colors # Set plot limits and labels
ax.text(1000, 5.8e-5, "AM", fontsize=10, color="green", alpha=0.2)
ax.text(1000, 5.6e-5, "Nyquist", fontsize=10, color="blue", alpha=0.2)
ax.text(1000, 5.4e-5, "EODf", fontsize=10, color="red", alpha=0.2)
ax.text(1000, 5.2e-5, "Stimulus frequency", fontsize=10, color="orange", alpha=0.2)
ax.text(1000, 5.0e-5, "EODf of awake fish", fontsize=10, color="purple", alpha=0.2)
ax.set_xlim([0, 1200]) ax.set_xlim([0, 1200])
ax.set_ylim([0, 6e-5]) ax.set_ylim([0, 6e-5])
ax.axvline(nyquist, color = "k", linestyle = "--")
ax.set_xlabel('Frequency (Hz)') ax.set_xlabel('Frequency (Hz)')
ax.set_ylabel('Power') ax.set_ylabel('Power')
ax.set_title('Power Spectrum with highlighted Integrals') ax.set_title('Power Spectrum with Highlighted Integrals')
# Apply float formatting to the y-axis # Apply float formatting to the y-axis
ax.yaxis.set_major_formatter(ticker.FuncFormatter(float_formatter)) ax.yaxis.set_major_formatter(ticker.FuncFormatter(float_formatter))
ax.legend(loc="upper right")

View File

@ -3,40 +3,8 @@ import rlxnix as rlx
from scipy.signal import welch from scipy.signal import welch
def all_coming_together(freq_array, power_array, points_list, categories, num_harmonics_list, colors, delta=2.5, threshold=0.5): def all_coming_together(freq_array, power_array, points_list, categories, num_harmonics_list, colors, delta=2.5, threshold=0.5):
""" # Initialize dictionaries and lists
Process a list of points, calculating integrals, checking validity, and preparing harmonics for valid points. valid_points = []
Parameters
----------
freq_array : np.array
Array of frequencies corresponding to the power values.
power_array : np.array
Array of power spectral density values.
points_list : list
List of harmonic frequency points to process.
categories : list
List of corresponding categories for each point.
num_harmonics_list : list
List of the number of harmonics for each point.
colors : list
List of colors corresponding to each point's category.
delta : float, optional
Radius of the range for integration around each point (default is 2.5).
threshold : float, optional
Threshold value to compare integrals with local mean (default is 0.5).
Returns
-------
valid_points : list
A continuous list of harmonics for all valid points.
color_mapping : dict
A dictionary mapping categories to corresponding colors.
category_harmonics : dict
A mapping of categories to their harmonic frequencies.
messages : list
A list of messages for each point, stating whether it was valid or not.
"""
valid_points = [] # A continuous list of harmonics for valid points
color_mapping = {} color_mapping = {}
category_harmonics = {} category_harmonics = {}
messages = [] messages = []
@ -46,21 +14,25 @@ def all_coming_together(freq_array, power_array, points_list, categories, num_ha
num_harmonics = num_harmonics_list[i] num_harmonics = num_harmonics_list[i]
color = colors[i] color = colors[i]
# Step 1: Calculate the integral for the point # Calculate the integral for the point
integral, local_mean = calculate_integral_2(freq_array, power_array, point, delta) integral, local_mean = calculate_integral_2(freq_array, power_array, point, delta)
# Step 2: Check if the point is valid # Check if the point is valid
valid = valid_integrals(integral, local_mean, point, threshold) valid = valid_integrals(integral, local_mean, point, threshold)
if valid: if valid:
# Step 3: Prepare harmonics if the point is valid # Prepare harmonics if the point is valid
harmonics, color_map, category_harm = prepare_harmonic(point, category, num_harmonics, color) harmonics, color_map, category_harm = prepare_harmonic(point, category, num_harmonics, color)
valid_points.extend(harmonics) # Use extend() to append harmonics in a continuous manner valid_points.extend(harmonics)
color_mapping.update(color_map) color_mapping[category] = color # Store color for category
category_harmonics.update(category_harm) category_harmonics[category] = harmonics
messages.append(f"The point {point} is valid.") messages.append(f"The point {point} is valid.")
else: else:
messages.append(f"The point {point} is not valid.") messages.append(f"The point {point} is not valid.")
# Debugging print statements
print("Color Mapping:", color_mapping)
print("Category Harmonics:", category_harmonics)
return valid_points, color_mapping, category_harmonics, messages return valid_points, color_mapping, category_harmonics, messages