<filename>analysis/plot_item_b.py
# Runs after normalizer_b
import shutil
from pathlib import Path
import itertools
import numpy as np
import pandas as pd
from matplotlib import pyplot as plt
import collections
from scipy.optimize import minimize_scalar

cols1 = ['F1_' + str(i) for i in range(3, 20, 2)]
cols2 = ['F2_' + str(i) for i in range(3, 20, 2)]
kCols = cols1 + cols2

def GetVowel(row):
    if row['Annotation'] in ['b1', 'b2']:
      return 'b'
    else:
      return row['Annotation']

def PlotNoSlice(df, full_group_name, output_dir):
    x = np.arange(0, 9)
    y1 = df[cols1].to_numpy(dtype='float')
    y2 = df[cols2].to_numpy(dtype='float')
    coeff1 = np.polyfit(x, y1, 4)
    coeff2 = np.polyfit(x, y2, 4)
    line1 = np.poly1d(coeff1)
    line2 = np.poly1d(coeff2)
    line1dd = np.polyder(line1, 2)
    line2dd = np.polyder(line2, 2)
    line1dd_max = minimize_scalar(-line1dd,
                                  bounds=(0, 8), method='bounded')
    line2dd_max = minimize_scalar(-line2dd,
                                  bounds=(0, 8), method='bounded')
    inflection1 = line1dd_max.x
    inflection2 = line2dd_max.x

    # Plot f1/f2
    plt.plot(x, y1, 'o')
    plt.plot(x, y2, 'x')
    plt.plot(x, line1(x), label='F1 fitted')
    plt.plot(x, line2(x), label='F2 fitted')
    plt.legend(bbox_to_anchor=(1.04, 1), loc="upper left")
    plt.title(full_group_name)
    plt.savefig(output_dir / (full_group_name + '.fitted.png'),
                bbox_inches="tight")
    plt.clf()
    plt.cla()
    # Plot deriv and inflection
    plt.plot(x, line1dd(x), label='F1 2nd deriv')
    plt.plot(x, line2dd(x), label='F2 2nd deriv')
    plt.axvline(x=inflection1, linestyle=':', label='F1 break')
    plt.axvline(x=inflection2, linestyle='-.', label='F2 break')
    plt.legend(bbox_to_anchor=(1.04, 1), loc="upper left")
    plt.title(full_group_name)
    plt.savefig(output_dir / (full_group_name + '.break.png'),
                bbox_inches="tight")
    plt.clf()
    plt.cla()

input_base_dir = Path('./analysis/output_b/')
output_base_dir = Path('./analysis/output_b/')

df = pd.read_csv(input_base_dir / 'S_normalized.csv')
df['Vowel'] = df.apply(lambda row: GetVowel(row), axis=1)
data = []
vowels = ['i', 'e', 'a', 'c', 'u', 'b', 'ic', 'ue']
for vowel in vowels:
  df_vowel = df.loc[df['Vowel'] == vowel, 'BD']
  data.append(df_vowel)
plt.boxplot(data)
plt.xticks(range(1, len(vowels) + 1), vowels)
plt.title('Itemb S/normS BD distribution')
plt.savefig(output_base_dir / 'S_normS_boxplot.png', bbox_inches="tight")
plt.clf()
plt.cla()


df_bd_bucket = df.groupby(['BD_S'])[kCols].mean()
df_bd_bucket.to_csv(Path('./analysis/output_b/break_by_bd_bucket.CSV'), index=True)
PlotNoSlice(df_bd_bucket.iloc[0], 'S_BD_S1', output_base_dir)
PlotNoSlice(df_bd_bucket.iloc[1], 'S_BD_S2', output_base_dir)
PlotNoSlice(df_bd_bucket.iloc[2], 'S_BD_S3', output_base_dir)

<filename>analysis/plot_item_b.py
# Runs after normalizer_b
import shutil
from pathlib import Path
import itertools
import numpy as np
import pandas as pd
from matplotlib import pyplot as plt
import collections
from scipy.optimize import minimize_scalar

cols1 = ['F1_' + str(i) for i in range(3, 20, 2)]
cols2 = ['F2_' + str(i) for i in range(3, 20, 2)]
kCols = cols1 + cols2

def GetVowel(row):
    if row['Annotation'] in ['b1', 'b2']:
      return 'b'
    else:
      return row['Annotation']

def PlotNoSlice(df, full_group_name, output_dir):
    x = np.arange(0, 9)
    y1 = df[cols1].to_numpy(dtype='float')
    y2 = df[cols2].to_numpy(dtype='float')
    coeff1 = np.polyfit(x, y1, 4)
    coeff2 = np.polyfit(x, y2, 4)
    line1 = np.poly1d(coeff1)
    line2 = np.poly1d(coeff2)
    line1dd = np.polyder(line1, 2)
    line2dd = np.polyder(line2, 2)
    line1dd_max = minimize_scalar(-line1dd,
                                  bounds=(0, 8), method='bounded')
    line2dd_max = minimize_scalar(-line2dd,
                                  bounds=(0, 8), method='bounded')
    inflection1 = line1dd_max.x
    inflection2 = line2dd_max.x

    # Plot f1/f2
    plt.plot(x, y1, 'o')
    plt.plot(x, y2, 'x')
    plt.plot(x, line1(x), label='F1 fitted')
    plt.plot(x, line2(x), label='F2 fitted')
    plt.legend(bbox_to_anchor=(1.04, 1), loc="upper left")
    plt.title(full_group_name)
    plt.savefig(output_dir / (full_group_name + '.fitted.png'),
                bbox_inches="tight")
    plt.clf()
    plt.cla()
    # Plot deriv and inflection
    plt.plot(x, line1dd(x), label='F1 2nd deriv')
    plt.plot(x, line2dd(x), label='F2 2nd deriv')
    plt.axvline(x=inflection1, linestyle=':', label='F1 break')
    plt.axvline(x=inflection2, linestyle='-.', label='F2 break')
    plt.legend(bbox_to_anchor=(1.04, 1), loc="upper left")
    plt.title(full_group_name)
    plt.savefig(output_dir / (full_group_name + '.break.png'),
                bbox_inches="tight")
    plt.clf()
    plt.cla()

input_base_dir = Path('./analysis/output_b/')
output_base_dir = Path('./analysis/output_b/')

df = pd.read_csv(input_base_dir / 'S_normalized.csv')
df['Vowel'] = df.apply(lambda row: GetVowel(row), axis=1)
data = []
vowels = ['i', 'e', 'a', 'c', 'u', 'b', 'ic', 'ue']
for vowel in vowels:
  df_vowel = df.loc[df['Vowel'] == vowel, 'BD']
  data.append(df_vowel)
plt.boxplot(data)
plt.xticks(range(1, len(vowels) + 1), vowels)
plt.title('Itemb S/normS BD distribution')
plt.savefig(output_base_dir / 'S_normS_boxplot.png', bbox_inches="tight")
plt.clf()
plt.cla()


df_bd_bucket = df.groupby(['BD_S'])[kCols].mean()
df_bd_bucket.to_csv(Path('./analysis/output_b/break_by_bd_bucket.CSV'), index=True)
PlotNoSlice(df_bd_bucket.iloc[0], 'S_BD_S1', output_base_dir)
PlotNoSlice(df_bd_bucket.iloc[1], 'S_BD_S2', output_base_dir)
PlotNoSlice(df_bd_bucket.iloc[2], 'S_BD_S3', output_base_dir)

# Runs after normalizer_b # Plot f1/f2 # Plot deriv and inflection