Tutorial for HN module — HavNegpy 0.1 documentation

# Tutorial for HN module

    [1]:

    # import the necessary packages along with HNpy
    import numpy as np
    import pandas as pd
    import matplotlib.pyplot as plt
    import os
    import HavNegpy as dd
    %matplotlib qt

    [2]:

    # extract the data

    filename = 'hn_example_data.txt'
    col_names = ['log f', 'log eps2']
    df = pd.read_csv(filename, sep=',',index_col=False,usecols = [0,1],names=col_names,header=None,skiprows=2,encoding='unicode_escape',engine='python')

    [3]:

    # plot the data

    x,y = df['log f'], df['log eps2']
    plt.scatter(x,y,label='example data')
    plt.xlabel('log f [Hz]')
    plt.ylabel('log $\epsilon$"')
    plt.legend()
    plt.title('Example for HN fitting')

    [3]:

    Text(0.5, 1.0, 'Example for HN fitting')

    [4]:

    # instantiate the HN module
    hn = dd.HN()

Select the ROI to fit the data using the select range method. The select range functions pops in a separate window and allows you two clicks to select the region of interest (ROI). The data in ROI is shown as image in the following cells

    [5]:

    #select range
    hn = dd.HN()
    x1,y1 = hn.select_range(x,y)

    x_lower_limit 2.5234707551868087 x_upper_limit 4.4693345833813005

Plot of the ROI to fit the HN function

    [6]:

    #dump the initial guess parameters using dump parameters method (varies for each fn), which dumps the parameters in a json file'
    #this is required before performing the first fitting as it takes the initial guess from the json file created

    hn.dump_parameters_hn()

    enter the beta value:0.5
    enter the gamma value:1
    enter the fm:3.75
    enter the deps:0.5
    enter the cond:0
    enter the s:1
    dumped_parameters {'beta': 0.5, 'gamma': 1.0, 'freq': 5623.413251903491, 'deps': 0.5, 'cond': 0.0, 'n': 1.0}

    [6]:

    ()

    [7]:

    # view the initial fit based on the dumped parameters
    # the plot is shown as a image in the next cell

    hn.initial_view_hn(x1,y1)

    loaded parameters
     {'beta': 0.5, 'gamma': 1.0, 'freq': 5623.413251903491, 'deps': 0.5, 'cond': 0.0, 'n': 1.0}

Plot of the initial fit based on the supplied parameters

    [8]:

    # perform least squares fitting
    # the plot is shown as a image in the next cell

    hn.fit(x1,y1)

    Choose the fit function
     1 -- HN, 2 -- HN with cond, 3 -- Hn-flank, 4 -- double HN, 5 -- double HN with cond:1
    0.7326563571608663 0.34641197051059136 1945.8637251236246 3.2548369440810547
    log fmax: 3.8521833734663224
    fit parameters dumped for next iteration {'beta': 0.7326563571608663, 'gamma': 0.34641197051059136, 'freq': 1945.8637251236246, 'deps': 3.2548369440810547, 'cond': 0, 'n': 0}

Plot of the final fit of the HN function to the data

    [9]:

    #create a file to save fit results using create_analysis file method
    #before saving fit results an analysis file has to be created

    hn.create_analysis_file()

    Do you want to use an existing file to save fit results?
     eg: existing file to save HN parameters, y or n:n
    Choose the fit function
     1 -- HN, 2 -- HN with cond, 3 -- HN-flank, 4 -- double HN, 5 -- double HN with cond:1
    Enter the analysis_file_name:hn_fit.TXT
    file did not exist, created hn_fit.TXT

    [9]:

    ()

    [10]:

    #save the fit results using save_fit method of the corresponding fit function
    #takes one argument, read more on the documentation

    hn.save_fit_hn(1)

    [10]:

    ()

    [ ]: