<jupyter_start><jupyter_text>Request Roll damping API<jupyter_code>%matplotlib.notebook
import requests
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
plt.style.use('dark_background')
from collections import OrderedDict
from rolldecayestimators.polynom_estimator import Polynom<jupyter_output><empty_output><jupyter_text>Simple requestThe API uses the following mathematical model:<jupyter_code>file_path = '../../static/models/polynom_complex.sym'
polynom = Polynom.load(file_path=file_path)
polynom.equation

url = r'http://marale.pythonanywhere.com/api/roll_damping'
request = url + r'?lpp=100&beam=10&T=2&BK_L=0&BK_B=0&OG=0&omega0_hat=0.1&C_b=0.7&A_0=0.97&V=10&phi_a=0.1'

r = requests.get(request)
assert r.status_code is 200
result = r.json()
result
result['B_e_hat']
def get_rolldamping(row):

    r = requests.get(url, params=dict(row))

    assert r.status_code is 200
    result = r.json()
    result_ = pd.Series(result, name=row.name)
    return result_
N=10
index = np.arange(0,N)
inputs = pd.DataFrame(index=index)

inputs['lpp']=100
inputs['beam']=10
inputs['T']=2
inputs['BK_L']=0
inputs['BK_B']=0
inputs['OG']=0
inputs['omega0_hat']=0.5
inputs['C_b']=0.8
inputs['A_0']=0.96
inputs['V']=np.linspace(0,10,N)
inputs['phi_a']=np.deg2rad(3)

result = inputs.apply(func=get_rolldamping, axis=1)
result.head()
fig,ax=plt.subplots()
result.plot(x='V',y='B_e_hat', ax=ax)

inputs2 = inputs.copy()
inputs2['V']=5
inputs2['phi_a']=np.deg2rad(np.linspace(0,10,N))
result2 = inputs2.apply(func=get_rolldamping, axis=1)

fig,ax=plt.subplots()
result2.plot(x='phi_a',y='B_e_hat', ax=ax)<jupyter_output><empty_output><jupyter_text>Using POST instead...to request roll damping for many ships in one single request<jupyter_code>def get_rolldampings(inputs):


    input_list = [record for record in inputs.to_dict('records')]
    response = requests.post(url=url,
                             json=input_list)

    outputs = response.json()
    outputs
    result = pd.DataFrame(outputs)
    return result
result3 = get_rolldampings(inputs=inputs)
fig,ax=plt.subplots()
result3.plot(x='V',y='B_e_hat', ax=ax)<jupyter_output><empty_output><jupyter_text>Playing with the API<jupyter_code>X=polynom.X
X.head()
input_ranges = OrderedDict()
input_parameters=[
    'beam', 'T', 'BK_L', 'BK_B', 'OG', 'omega0_hat', 'C_b', 'A_0', 'V', 'phi_a'
]
for input_parameter in input_parameters:
    x=X[input_parameter]
    input_ranges[input_parameter]=(x.min(),x.max())

mean_ship = pd.Series()
for parameter, limits in input_ranges.items():
    mean_ship[parameter]=np.mean(limits)
mean_ship['lpp']=1

N=10
df_results=pd.DataFrame()
axes = []
for parameter, limits in input_ranges.items():

    repeats = np.tile(mean_ship, (N,1))
    df = pd.DataFrame(repeats, columns=mean_ship.index)
    df[parameter] = np.linspace(limits[0],limits[1],N)

    result4 = get_rolldampings(inputs=df)
    df_results=df_results.append(result4, ignore_index=True)

    fig,ax=plt.subplots()
    axes.append(ax)
    result4.plot(x=parameter, y='B_e_hat', ax=ax)

for ax in axes:
    ax.set_ylim(df_results['B_e_hat'].min(), df_results['B_e_hat'].max())<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15825977301387137, 'fraction_numerical': 0.025220680958385876, 'mean_word_length': 6.051111111111111, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '24925', 'license': 'MIT', 'n_tokens_mistral': 1307, 'n_tokens_neox': 1253, 'n_words': 199, 'path': 'doc/notebooks/01_request_API.ipynb', 'repo_name': 'martinlarsalbert/REST_API_roll_damping', 'size': '3172'}