-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlocal_script.py
50 lines (41 loc) · 1.68 KB
/
local_script.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import boto3
import time
import csv
import numpy as np
from io import StringIO
import datetime
startTime = datetime.datetime.now()
session = boto3.session.Session(region_name='us-east-1')
# kicking off the workflow, calling controller lambda
client_lambda = session.client('lambda')
response = client_lambda.invoke(
FunctionName='lambda_calc_exposures',
InvocationType='Event',
)
s3_client = session.client('s3')
s3_resource = session.resource('s3')
file_count = 0
# waiting for a minute for files to appear in S3 bucket
time.sleep(60)
while file_count < 500:
filenames = [e['Key'] for p in s3_client.get_paginator("list_objects_v2").paginate(Bucket='calc-exposures-out') for e in p['Contents']]
file_count = len(filenames)
time.sleep(5)
# at this point, its all done, get the exposures
exposures = np.zeros(shape=(500, 262), dtype=float)
filenames = [e['Key'] for p in s3_client.get_paginator("list_objects_v2").paginate(Bucket='calc-exposures-out') for e in p['Contents']]
for f in filenames:
if f == 'simulated-fixings.csv':
continue
sim_num = int(f.split('_')[0])
csvfile = s3_client.get_object(Bucket='calc-exposures-out', Key=f)
lines = csvfile['Body'].read().decode('utf-8')
exposures[sim_num, :] = np.genfromtxt(StringIO(lines), delimiter=",")
print('\nTime elasped: ', datetime.datetime.now() - startTime)
# calculate expected positive and negative exposures
positiveExposures = exposures.copy()
positiveExposures[positiveExposures < 0.0] = 0.0
EPE = np.mean(positiveExposures, axis=0)
negativeExposures = exposures.copy()
negativeExposures[negativeExposures > 0.0] = 0.0
ENE = np.mean(negativeExposures, axis=0)