# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

# # Step 0 and 1
# Import Data (only FD001)

# Download data from S3

# +
import boto3
import sagemaker

#role = sagemaker.get_execution_role()
role = '<your role>'
region = boto3.Session().region_name

print(region)
print(role)

# Test bucket name immersionday-sagemaker-test
bucket_name = 'iiot-book-data'
file_name_train = 'train_FD001.txt'
file_name_test = 'test_FD001.txt'

import boto3

s3 = boto3.resource('s3')
s3.Bucket(bucket_name).download_file(file_name_train, file_name_train)
s3.Bucket(bucket_name).download_file(file_name_test,file_name_test)
# -

# Import Data (only FD001)

# step 1: read the dataset
columns = ['unitid', 'time', 'set_1','set_2','set_3']
columns.extend(['sensor_' + str(i) for i in range(1,22)])
df = pd.read_csv(file_name_train, delim_whitespace=True,names=columns)
df_test = pd.read_csv(file_name_test, delim_whitespace=True,names=columns)

# ## Prepare the data set
# Remove unusefull columns.

# +
# only engine 1
i=1

# prepare model
columns_feature=['sensor_4','sensor_7']

file_name_train = 'train.csv'
file_name_test = 'test.csv'

dataset_train=df[(df.unitid ==i)]
dataset_test=df_test[(df_test.unitid ==i)]
# -

# ## Save on local test

print(dataset_train)
np.savetxt(os.path.join('./container/local_test/test_dir/input/data/training',file_name_train), dataset_train, delimiter=",",fmt='%s')

# ---
# # Build and deploy Docker File
#
# Now we can build our docker file, test locally and deploy on the cloud

# +
## Local Test

# +
import os
import subprocess

def run_interactive(command, working_directory='.'):
    p = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, cwd=working_directory)
    for line in p.stdout.readlines():
        print (line)
    retval = p.wait()
    
# build docker file    
run_interactive('docker build -t rul-estimator .','container')
# -

# ## Train the model
# Train model 'container/local_test/train_local.sh rul-estimator'

#
# ### Sart locally
# From command line:
# Start 'container/local_test/serve_local.sh rul-estimator'

# ## Test local model

run_interactive('./predict_local.sh ../../test.csv', 'container/local_test')

# ---------
# # Publish image on AWS ECS 

# publish image on AWS ECS file    
run_interactive('./build_and_push.sh rul-estimator','container/')

print('now you can eccass to https://console.aws.amazon.com/ecs/ ')



StarcoderdataJupyterScriptsDedupFiltered