import pickle
import pandas
import csv
import sys

print("Read arguments from the terminal:")
for i in sys.argv[1:]:
    print(i)
    
project_data_path=sys.argv[1]
tuned_model_path=sys.argv[2]

print("\nImporting the new data...")
X_deploy = pandas.read_csv(project_data_path+"X_deploy.csv",header=None)

print("\nLoading the tuned model...")
loaded_model = pickle.load(open(tuned_model_path+"tuned_model.pickle", 'rb'))

print("\nMaking predictions...")
y_pred=loaded_model.predict(X_deploy)

print("\nImporting the ids of the new data...")
X_deploy_ids = pandas.read_csv(project_data_path+"X_deploy_ids.csv",header=None)

print("\nAdding prediction values to the ids...")
X_deploy_ids["Predictions"] = y_pred
print(X_deploy_ids.head())

print("\nExporting the predictions...")
with open(tuned_model_path+"predictions.csv", 'w', newline='') as csvfile:
    writer = csv.writer(csvfile)
    row_list = X_deploy_ids.values.tolist()
    writer.writerows(row_list)
csvfile.close()
print("\nClose connection to file: {a}".format(a=csvfile.closed))

print("\nDone!")
