{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\nRunning CF-FM call segmentation\n===============================\nHere we will run the :code:`segment_and_measure` function and store the \nresults of how long each Cf/FM segment is. \n\nDataset creation\n~~~~~~~~~~~~~~~~\nThe synthetic dataset has already been created in a separate module. \nSee 'Generating the CF-FM synthetic calls' in the main page. \n\nIt can take long\n~~~~~~~~~~~~~~~~\nWe're running a few hundred synthetic audio clips with a few seconds (1-10s)\nneeded per iteration. This could mean, it might take a while(5,10 or more minutes)!\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import h5py\nimport itsfm\nimport pandas as pd\nfrom tqdm import tqdm"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Now, let's load each synthetic call and proceed to save the \nresults from the PWVD and peak-percentage based methods.\n\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "FM rate based segmentation \n--------------------------\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "obtained = []\n\nf = h5py.File('horseshoe_test.hdf5', 'r')\nsynthesised = pd.read_csv('horseshoe_test_parameters.csv')\n\n\nfs = float(f['fs'][:])\n\nparameters = {}\nparameters['segment_method'] = 'pwvd'\nparameters['window_size'] = int(fs*0.001)\nparameters['fmrate_threshold'] = 2.0\nparameters['max_acc'] = 10\nparameters['extrap_window'] = 75*10**-6\n\nfor call_num in tqdm(range(synthesised.shape[0])):\n    synthetic_call = f[str(call_num)][:]\n    output = itsfm.segment_and_measure_call(synthetic_call, fs, **parameters)\n                                    \n    seg_output, call_parts, measurements= output\n    # save the long format output into a wide format output to\n    # allow comparison\n    sub = measurements[['region_id', 'duration']]\n    sub['call_number'] = call_num\n    region_durations = sub.pivot(index='call_number',\n                                 columns='region_id', values='duration')\n    obtained.append(region_durations)\n\nall_obtained = pd.concat(obtained)\n\nall_obtained.to_csv('obtained_pwvd_horseshoe_sim.csv')"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Peak-percentage based segmentation\n----------------------------------\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "pkpctg_parameters = {}\npkpctg_parameters['segment_method'] = 'peak_percentage'\npkpctg_parameters['peak_percentage'] = 0.99\npkpctg_parameters['window_size'] = 125\npkpctg_parameters['double_pass'] = True\n\npkpct_obtained = []\n\nfor call_num in tqdm(range(synthesised.shape[0])):\n    synthetic_call = f[str(call_num)][:]\n    output = itsfm.segment_and_measure_call(synthetic_call, fs, **pkpctg_parameters)\n                                    \n    seg_output, call_parts, measurements= output\n    # save the long format output into a wide format output to\n    # allow comparison\n    sub = measurements[['region_id', 'duration']]\n    sub['call_number'] = call_num\n    region_durations = sub.pivot(index='call_number',\n                                 columns='region_id', values='duration')\n    pkpct_obtained.append(region_durations)\n\n\nf.close()\n\npk_pctage = pd.concat(pkpct_obtained)\n\npk_pctage.to_csv('obtained_pkpct_horseshoe_sim.csv')"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.7.9"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}