Skip to content
Snippets Groups Projects
Commit 3f89b0f9 authored by Kelvin Rodriguez's avatar Kelvin Rodriguez
Browse files

no longer inits the interpreter on every call

parent 4cd15e16
Branches
Tags
No related merge requests found
%% Cell type:code id: tags:
``` python
import ale
from ale import load
```
%% Cell type:code id: tags:
``` python
label = """
PDS_VERSION_ID = PDS3
/*FILE CHARACTERISTICS*/
RECORD_TYPE = FIXED_LENGTH
RECORD_BYTES = 5064
FILE_RECORDS = 13313
LABEL_RECORDS = 1
^IMAGE = 2
/*DATA IDENTIFICATION*/
DATA_SET_ID = "LRO-L-LROC-2-EDR-V1.0"
ORIGINAL_PRODUCT_ID = nacl0002fc60
PRODUCT_ID = M128963531LE
MISSION_NAME = "LUNAR RECONNAISSANCE ORBITER"
MISSION_PHASE_NAME = "NOMINAL MISSION"
INSTRUMENT_HOST_NAME = "LUNAR RECONNAISSANCE ORBITER"
INSTRUMENT_HOST_ID = LRO
INSTRUMENT_NAME = "LUNAR RECONNAISSANCE ORBITER CAMERA"
INSTRUMENT_ID = LROC
LRO:PREROLL_TIME = 2010-05-20T02:57:44.373
START_TIME = 2010-05-20T02:57:44.720
STOP_TIME = 2010-05-20T02:57:49.235
LRO:SPACECRAFT_CLOCK_PREROLL_COUNT = "1/296017064:22937"
SPACECRAFT_CLOCK_START_COUNT = "1/296017064:45694"
SPACECRAFT_CLOCK_STOP_COUNT = "1/296017069:13866"
ORBIT_NUMBER = 4138
PRODUCER_ID = LRO_LROC_TEAM
PRODUCT_CREATION_TIME = 2013-09-16T19:57:12
PRODUCER_INSTITUTION_NAME = "ARIZONA STATE UNIVERSITY"
PRODUCT_TYPE = EDR
PRODUCT_VERSION_ID = "v1.8"
UPLOAD_ID = "SC_2010140_0000_A_V01.txt"
/*DATA DESCRIPTION*/
TARGET_NAME = "MOON"
RATIONALE_DESC = "TARGET OF OPPORTUNITY"
FRAME_ID = LEFT
DATA_QUALITY_ID = "0"
DATA_QUALITY_DESC = "The DATA_QUALITY_ID is set to an 8-bit
value that encodes the following data quality information for the
observation. For each bit a value of 0 means FALSE and a value of 1 means
TRUE. More information about the data quality ID can be found in the LROC
EDR/CDR SIS, section 3.3 'Label and Header Descriptions'.
Bit 1: Temperature of focal plane array is out of bounds.
Bit 2: Threshold for saturated pixels is reached.
Bit 3: Threshold for under-saturated pixels is reached.
Bit 4: Observation is missing telemetry packets.
Bit 5: SPICE information is bad or missing.
Bit 6: Observation or housekeeping information is bad or missing.
Bit 7: Spare.
Bit 8: Spare."
/*ENVIRONMENT*/
LRO:TEMPERATURE_SCS = 4.51 <degC>
LRO:TEMPERATURE_FPA = 17.88 <degC>
LRO:TEMPERATURE_FPGA = -12.33 <degC>
LRO:TEMPERATURE_TELESCOPE = 5.91 <degC>
LRO:TEMPERATURE_SCS_RAW = 2740
LRO:TEMPERATURE_FPA_RAW = 2107
LRO:TEMPERATURE_FPGA_RAW = 3418
LRO:TEMPERATURE_TELESCOPE_RAW = 2675
/*IMAGING PARAMETERS*/
CROSSTRACK_SUMMING = 1
BANDWIDTH = 300 <nm>
CENTER_FILTER_WAVELENGTH = 600 <nm>
LINE_EXPOSURE_DURATION = 0.337600 <ms>
LRO:LINE_EXPOSURE_CODE = 0
LRO:DAC_RESET_LEVEL = 198
LRO:CHANNEL_A_OFFSET = 60
LRO:CHANNEL_B_OFFSET = 123
LRO:COMPAND_CODE = 3
LRO:LINE_CODE = 13
LRO:BTERM = (0,16,69,103,128)
LRO:MTERM = (0.5,0.25,0.125,0.0625,0.03125)
LRO:XTERM = (0,64,424,536,800)
LRO:COMPRESSION_FLAG = 1
LRO:MODE = 7
/*DATA OBJECT*/
OBJECT = IMAGE
LINES = 13312
LINE_SAMPLES = 5064
SAMPLE_BITS = 8
SAMPLE_TYPE = LSB_INTEGER
UNIT = "RAW_INSTRUMENT_COUNT"
MD5_CHECKSUM = "0fe91f4b2e93083ee0093e7c8d05f3bc"
END_OBJECT = IMAGE
END
"""
```
%% Cell type:code id: tags:
``` python
load(label)
```
%% Output
TRYING: <class 'ale.drivers.mro_driver.MRO_CTX'>
TRYING: <class 'ale.drivers.mdis_driver.Messenger'>
TRYING: <class 'ale.drivers.cassini_driver.CassiniISS'>
TRYING: <class 'ale.drivers.lro_driver.LRO_LROC'>
IN FURNISH
Traceback (most recent call last):
File "/Users/krodriguez-pr/repos/ale/ale/drivers/__init__.py", line 25, in load
with res as r:
File "/Users/krodriguez-pr/repos/ale/ale/drivers/base.py", line 30, in __enter__
if self.metakernel:
File "/Users/krodriguez-pr/repos/ale/ale/drivers/lro_driver.py", line 17, in metakernel
self._metakernel = metakernels['data'][0]['path']
IndexError: list index out of range
---------------------------------------------------------------------------
Exception Traceback (most recent call last)
<ipython-input-3-c10fc6e56b98> in <module>
----> 1 load(label)
~/repos/ale/ale/drivers/__init__.py in load(label)
29 import traceback
30 traceback.print_exc()
---> 31 raise Exception('No Such Driver for Label')
Exception: No Such Driver for Label
%% Cell type:code id: tags:
``` python
```
%% Cell type:code id: tags:
``` python
```
%% Cell type:code id: tags:
``` C++11
#pragma cling add_include_path("/Users/krodriguez-pr/repos/ale/include/")
#pragma cling add_include_path("/Users/krodriguez-pr/repos/ale/include/json")
#pragma cling add_include_path("/Users/krodriguez-pr/anaconda3/envs/ale/include/python3.7m")
#pragma cling add_include_path("/home/krodriguez/repos/ale/include/")
#pragma cling add_include_path("/home/krodriguez/repos/ale/include/json")
#pragma cling add_include_path("/data/big/anaconda3/envs/ale/include/python3.7m")
#pragma cling load("/Users/krodriguez-pr/repos/ale/build/libale.dylib")
#pragma cling load("/home/krodriguez/repos/ale/build/libale.so")
```
%% Cell type:code id: tags:
``` C++11
#include "Python.h"
#include "ale.h"
#include <iostream>
#include <string.h>
std::string test_lro_label =
"PDS_VERSION_ID = PDS3"
""
"/*FILE CHARACTERISTICS*/"
"RECORD_TYPE = FIXED_LENGTH"
"RECORD_BYTES = 5064"
"FILE_RECORDS = 13313"
"LABEL_RECORDS = 1"
"^IMAGE = 2"
""
"/*DATA IDENTIFICATION*/"
"DATA_SET_ID = \"LRO-L-LROC-2-EDR-V1.0\""
"ORIGINAL_PRODUCT_ID = nacl0002fc60"
"PRODUCT_ID = M128963531LE"
"MISSION_NAME = \"LUNAR RECONNAISSANCE ORBITER\""
"MISSION_PHASE_NAME = \"NOMINAL MISSION\""
"INSTRUMENT_HOST_NAME = \"LUNAR RECONNAISSANCE ORBITER\""
"INSTRUMENT_HOST_ID = LRO"
"INSTRUMENT_NAME = \"LUNAR RECONNAISSANCE ORBITER CAMERA\""
"INSTRUMENT_ID = LROC"
"LRO:PREROLL_TIME = 2010-05-20T02:57:44.373"
"START_TIME = 2010-05-20T02:57:44.720"
"STOP_TIME = 2010-05-20T02:57:49.235"
"LRO:SPACECRAFT_CLOCK_PREROLL_COUNT = \"1/296017064:22937\""
"SPACECRAFT_CLOCK_START_COUNT = \"1/296017064:45694\""
"SPACECRAFT_CLOCK_STOP_COUNT = \"1/296017069:13866\""
"ORBIT_NUMBER = 4138"
"PRODUCER_ID = LRO_LROC_TEAM"
"PRODUCT_CREATION_TIME = 2013-09-16T19:57:12"
"PRODUCER_INSTITUTION_NAME = \"ARIZONA STATE UNIVERSITY\""
"PRODUCT_TYPE = EDR"
"PRODUCT_VERSION_ID = \"v1.8\""
"UPLOAD_ID = \"SC_2010140_0000_A_V01.txt\""
""
"/*DATA DESCRIPTION*/"
"TARGET_NAME = \"MOON\""
"RATIONALE_DESC = \"TARGET OF OPPORTUNITY\""
"FRAME_ID = LEFT"
"DATA_QUALITY_ID = \"0\""
"DATA_QUALITY_DESC = \"The DATA_QUALITY_ID is set to an 8-bit"
" value that encodes the following data quality information for the"
" observation. For each bit a value of 0 means FALSE and a value of 1 means"
" TRUE. More information about the data quality ID can be found in the LROC"
" EDR/CDR SIS, section 3.3 'Label and Header Descriptions'."
" Bit 1: Temperature of focal plane array is out of bounds."
" Bit 2: Threshold for saturated pixels is reached."
" Bit 3: Threshold for under-saturated pixels is reached."
" Bit 4: Observation is missing telemetry packets."
" Bit 5: SPICE information is bad or missing."
" Bit 6: Observation or housekeeping information is bad or missing."
" Bit 7: Spare."
" Bit 8: Spare.\""
""
"/*ENVIRONMENT*/"
"LRO:TEMPERATURE_SCS = 4.51 <degC>"
"LRO:TEMPERATURE_FPA = 17.88 <degC>"
"LRO:TEMPERATURE_FPGA = -12.33 <degC>"
"LRO:TEMPERATURE_TELESCOPE = 5.91 <degC>"
"LRO:TEMPERATURE_SCS_RAW = 2740"
"LRO:TEMPERATURE_FPA_RAW = 2107"
"LRO:TEMPERATURE_FPGA_RAW = 3418"
"LRO:TEMPERATURE_TELESCOPE_RAW = 2675"
""
"/*IMAGING PARAMETERS*/"
"CROSSTRACK_SUMMING = 1"
"BANDWIDTH = 300 <nm>"
"CENTER_FILTER_WAVELENGTH = 600 <nm>"
"LINE_EXPOSURE_DURATION = 0.337600 <ms>"
"LRO:LINE_EXPOSURE_CODE = 0"
"LRO:DAC_RESET_LEVEL = 198"
"LRO:CHANNEL_A_OFFSET = 60"
"LRO:CHANNEL_B_OFFSET = 123"
"LRO:COMPAND_CODE = 3"
"LRO:LINE_CODE = 13"
"LRO:BTERM = (0,16,69,103,128)"
"LRO:MTERM = (0.5,0.25,0.125,0.0625,0.03125)"
"LRO:XTERM = (0,64,424,536,800)"
"LRO:COMPRESSION_FLAG = 1"
"LRO:MODE = 7"
""
"/*DATA OBJECT*/"
"OBJECT = IMAGE"
" LINES = 13312"
" LINE_SAMPLES = 5064"
" SAMPLE_BITS = 8"
" SAMPLE_TYPE = LSB_INTEGER"
" UNIT = \"RAW_INSTRUMENT_COUNT\""
" MD5_CHECKSUM = \"0fe91f4b2e93083ee0093e7c8d05f3bc\""
"END_OBJECT = IMAGE"
"END";
"PDS_VERSION_ID = PDS3\n"
"\n"
"/*FILE CHARACTERISTICS*/\n"
"RECORD_TYPE = FIXED_LENGTH\n"
"RECORD_BYTES = 5064\n"
"FILE_RECORDS = 13313\n"
"LABEL_RECORDS = 1\n"
"^IMAGE = 2\n"
"\n"
"/*DATA IDENTIFICATION*/\n"
"DATA_SET_ID = \"LRO-L-LROC-2-EDR-V1.0\"\n"
"ORIGINAL_PRODUCT_ID = nacl0002fc60\n"
"PRODUCT_ID = M128963531LE\n"
"MISSION_NAME = \"LUNAR RECONNAISSANCE ORBITER\"\n"
"MISSION_PHASE_NAME = \"NOMINAL MISSION\"\n"
"INSTRUMENT_HOST_NAME = \"LUNAR RECONNAISSANCE ORBITER\"\n"
"INSTRUMENT_HOST_ID = LRO\n"
"INSTRUMENT_NAME = \"LUNAR RECONNAISSANCE ORBITER CAMERA\"\n"
"INSTRUMENT_ID = LROC\n"
"LRO:PREROLL_TIME = 2010-05-20T02:57:44.373\n"
"START_TIME = 2010-05-20T02:57:44.720\n"
"STOP_TIME = 2010-05-20T02:57:49.235\n"
"LRO:SPACECRAFT_CLOCK_PREROLL_COUNT = \"1/296017064:22937\"\n"
"SPACECRAFT_CLOCK_START_COUNT = \"1/296017064:45694\"\n"
"SPACECRAFT_CLOCK_STOP_COUNT = \"1/296017069:13866\"\n"
"ORBIT_NUMBER = 4138\n"
"PRODUCER_ID = LRO_LROC_TEAM\n"
"PRODUCT_CREATION_TIME = 2013-09-16T19:57:12\n"
"PRODUCER_INSTITUTION_NAME = \"ARIZONA STATE UNIVERSITY\"\n"
"PRODUCT_TYPE = EDR\n"
"PRODUCT_VERSION_ID = \"v1.8\"\n"
"UPLOAD_ID = \"SC_2010140_0000_A_V01.txt\"\n"
"\n"
"/*DATA DESCRIPTION*/\n"
"TARGET_NAME = \"MOON\"\n"
"RATIONALE_DESC = \"TARGET OF OPPORTUNITY\"\n"
"FRAME_ID = LEFT\n"
"DATA_QUALITY_ID = \"0\"\n"
"DATA_QUALITY_DESC = \"The DATA_QUALITY_ID is set to an 8-bit\n"
" value that encodes the following data quality information for the\n"
" observation. For each bit a value of 0 means FALSE and a value of 1 means\n"
" TRUE. More information about the data quality ID can be found in the LROC\n"
" EDR/CDR SIS, section 3.3 'Label and Header Descriptions'.\n"
" Bit 1: Temperature of focal plane array is out of bounds.\n"
" Bit 2: Threshold for saturated pixels is reached.\n"
" Bit 3: Threshold for under-saturated pixels is reached.\n"
" Bit 4: Observation is missing telemetry packets.\n"
" Bit 5: SPICE information is bad or missing.\n"
" Bit 6: Observation or housekeeping information is bad or missing.\n"
" Bit 7: Spare.\n"
" Bit 8: Spare.\"\n"
"\n"
"/*ENVIRONMENT*/\n"
"LRO:TEMPERATURE_SCS = 4.51 <degC>\n"
"LRO:TEMPERATURE_FPA = 17.88 <degC>\n"
"LRO:TEMPERATURE_FPGA = -12.33 <degC>\n"
"LRO:TEMPERATURE_TELESCOPE = 5.91 <degC>\n"
"LRO:TEMPERATURE_SCS_RAW = 2740\n"
"LRO:TEMPERATURE_FPA_RAW = 2107\n"
"LRO:TEMPERATURE_FPGA_RAW = 3418\n"
"LRO:TEMPERATURE_TELESCOPE_RAW = 2675\n"
"\n"
"/*IMAGING PARAMETERS*/\n"
"CROSSTRACK_SUMMING = 1\n"
"BANDWIDTH = 300 <nm>\n"
"CENTER_FILTER_WAVELENGTH = 600 <nm>\n"
"LINE_EXPOSURE_DURATION = 0.337600 <ms>\n"
"LRO:LINE_EXPOSURE_CODE = 0\n"
"LRO:DAC_RESET_LEVEL = 198\n"
"LRO:CHANNEL_A_OFFSET = 60\n"
"LRO:CHANNEL_B_OFFSET = 123\n"
"LRO:COMPAND_CODE = 3\n"
"LRO:LINE_CODE = 13\n"
"LRO:BTERM = (0,16,69,103,128)\n"
"LRO:MTERM = (0.5,0.25,0.125,0.0625,0.03125)\n"
"LRO:XTERM = (0,64,424,536,800)\n"
"LRO:COMPRESSION_FLAG = 1\n"
"LRO:MODE = 7\n"
"\n"
"/*DATA OBJECT*/\n"
"OBJECT = IMAGE\n"
" LINES = 13312\n"
" LINE_SAMPLES = 5064\n"
" SAMPLE_BITS = 8\n"
" SAMPLE_TYPE = LSB_INTEGER\n"
" UNIT = \"RAW_INSTRUMENT_COUNT\"\n"
" MD5_CHECKSUM = \"0fe91f4b2e93083ee0093e7c8d05f3bc\"\n"
"END_OBJECT = IMAGE\n"
"END\n";
```
%% Cell type:code id: tags:
``` C++11
std::string load(std::string filename) {
static bool first_run = true;
if(first_run) {
// Initialize the Python interpreter but only once.
first_run = !first_run;
Py_Initialize();
atexit(Py_Finalize);
}
// Import the file as a Python module.
PyObject *pModule = PyImport_Import(PyUnicode_FromString("ale"));
// Create a dictionary for the contents of the module.
PyObject *pDict = PyModule_GetDict(pModule);
// Get the add method from the dictionary.
PyObject *pFunc = PyDict_GetItemString(pDict, "loads");
// Create a Python tuple to hold the arguments to the method.
PyObject *pArgs = PyTuple_New(1);
// Set the Python int as the first and second arguments to the method.
PyObject *pString = PyUnicode_FromString(filename.c_str());
PyTuple_SetItem(pArgs, 0, pString);
// Call the function with the arguments.
PyObject* pResult = PyObject_CallObject(pFunc, pArgs);
// Print a message if calling the method failed.
if(pResult == NULL) {
throw "pResult";
}
std::string cResult;
PyObject *temp_bytes = PyUnicode_AsUTF8String(pResult); // Owned reference
if (temp_bytes != NULL) {
char *temp_str = PyBytes_AS_STRING(temp_bytes); // Borrowed pointer
cResult = temp_str; // copy into std::string
Py_DECREF(temp_str);
} else {
throw "pybytes";
}
Py_DECREF(temp_bytes);
Py_DECREF(pArgs);
Py_DECREF(pModule);
Py_DECREF(pString);
return cResult;
}
```
%% Cell type:code id: tags:
``` C++11
std::string label;
```
%% Cell type:code id: tags:
``` C++11
label = load(test_lro_label);
```
%% Cell type:code id: tags:
``` C++11
// label
```
%% Cell type:code id: tags:
``` C++11
using json = nlohmann::json;
json jlabel = json::parse(label)
```
%% Cell type:code id: tags:
``` C++11
std::vector<double> orientations = jlabel.at("sensor_orientation").at(0).get<std::vector<double>>()
```
%% Cell type:code id: tags:
``` C++11
ale::load(test_lro_label);
orientations
```
%% Output
Standard Exception: Calling the load method failed.
{ 0.622216, 0.274174, -0.668159, 0.30206 }
%% Cell type:code id: tags:
``` C++11
```
......
......@@ -263,12 +263,14 @@ namespace ale {
return result;
}
std::string load(std::string filename) {
// Initialize the Python interpreter.
static bool first_run = true;
if(first_run) {
// Initialize the Python interpreter but only once.
first_run = !first_run;
Py_Initialize();
atexit(Py_Finalize);
}
// Import the file as a Python module.
PyObject *pModule = PyImport_Import(PyUnicode_FromString("ale"));
......@@ -277,36 +279,39 @@ std::string load(std::string filename) {
PyObject *pDict = PyModule_GetDict(pModule);
// Get the add method from the dictionary.
PyObject *pFunc = PyDict_GetItemString(pDict, "load");
PyObject *pFunc = PyDict_GetItemString(pDict, "loads");
// Create a Python tuple to hold the arguments to the method.
PyObject *pArgs = PyTuple_New(1);
// Set the Python int as the first and second arguments to the method.
PyTuple_SetItem(pArgs, 0, PyUnicode_FromString(filename.c_str()));
PyObject *pString = PyUnicode_FromString(filename.c_str());
PyTuple_SetItem(pArgs, 0, pString);
// Call the function with the arguments.
PyObject* pResult = PyObject_CallObject(pFunc, pArgs);
// Print a message if calling the method failed.
if(pResult == NULL) {
throw invalid_argument("Calling the load method failed.");
throw "pResult";
}
char *cResult = 0;
if (PyUnicode_Check(pResult)) {
PyObject * temp_bytes = PyUnicode_AsEncodedString(pResult, "UTF-8", "strict"); // Owned reference
std::string cResult;
PyObject *temp_bytes = PyUnicode_AsUTF8String(pResult); // Owned reference
if (temp_bytes != NULL) {
cResult = PyBytes_AS_STRING(temp_bytes); // Borrowed pointer
cResult = strdup(cResult);
Py_DECREF(temp_bytes);
char *temp_str = PyBytes_AS_STRING(temp_bytes); // Borrowed pointer
cResult = temp_str; // copy into std::string
Py_DECREF(temp_str);
} else {
// some error handling
}
throw "pybytes";
}
Py_Finalize();
Py_DECREF(temp_bytes);
Py_DECREF(pArgs);
Py_DECREF(pModule);
Py_DECREF(pString);
return std::string(cResult);
return cResult;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment