<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Using the Illumina InterOp Library in Python
Step1:  Getting SAV Imaging Tab-like Metrics
The run_metrics class encapsulates the model for all the individual InterOp files as well as containing information
from the RunInfo.xml. The Modules page contains a subset of the applications programmer's interface
for all the major classes in C++. The available Python models all have the same names (with a few exceptions) and take
the same parameters. This page is useful for accessing specific values loaded from the individual files.
Step2:  By default, the run_metrics class loads all the InterOp files.
run_folder = run_metrics.read(run_folder)
The InterOp library can provide a list of all necessary InterOp files for a specific application. The following shows how to generate that list for the index summary statistics
Step3:  The run_metrics class can use this list to load only the required InterOp files as follows
Step4:  The column headers for the imaing table can be created as follows
Step5:  Convert the columns object to a list of strings.
Step6:  Subsample rows and columns
Step7:  The data from imaging table can populate a numpy ndarray as follows
Step8:  Convert the header list and data ndarray into a Pandas table.
Step9:  Render the Imaging Table data using Pandas
Step10:  Getting Only Occpuancy from the imaging table
This section shows how to get all metrics from a single InterOp file. Here we are getting all metrics from ExtendedTileMetricsOut.bin, which is % Occupied
Step11:  This allows you to select a specific file to load. 
Note that tables require at least one cycle metric set to be built. We include Extraction below for that reason.
Step12:  Select only the first row_count rows.
Step13:  Select a subset of columns
Step14:  Convert to a Pandas DataFrame object
Step15:  Only display data from the first cycle
<ASSISTANT_TASK:>
Python Code:
run_folder = r""
Explanation: Using the Illumina InterOp Library in Python: Part 5
Install
If you do not have the Python InterOp library installed, then you can do the following:
$ pip install interop
You can verify that InterOp is properly installed:
$ python -m interop --test
Before you begin
If you plan to use this tutorial in an interactive fashion, then you should get an example run folder that contains InterOp files such as TileMetricsOut.bin.
Please change the path below so that it points at the run folder you wish to use:
End of explanation
from interop import py_interop_run_metrics, py_interop_run, py_interop_table
import numpy
import pandas as pd
run_metrics = py_interop_run_metrics.run_metrics()
Explanation: Getting SAV Imaging Tab-like Metrics
The run_metrics class encapsulates the model for all the individual InterOp files as well as containing information
from the RunInfo.xml. The Modules page contains a subset of the applications programmer's interface
for all the major classes in C++. The available Python models all have the same names (with a few exceptions) and take
the same parameters. This page is useful for accessing specific values loaded from the individual files.
End of explanation
valid_to_load = py_interop_run.uchar_vector(py_interop_run.MetricCount, 0)
py_interop_table.list_imaging_table_metrics_to_load(valid_to_load, False)
Explanation: By default, the run_metrics class loads all the InterOp files.
run_folder = run_metrics.read(run_folder)
The InterOp library can provide a list of all necessary InterOp files for a specific application. The following shows how to generate that list for the index summary statistics:
End of explanation
run_metrics.read(run_folder, valid_to_load)
Explanation: The run_metrics class can use this list to load only the required InterOp files as follows:
End of explanation
columns = py_interop_table.imaging_column_vector()
py_interop_table.create_imaging_table_columns(run_metrics, columns)
Explanation: The column headers for the imaing table can be created as follows:
End of explanation
headers = []
for i in range(columns.size()):
    column = columns[i]
    if column.has_children():
        headers.extend([column.name()+"("+subname+")" for subname in column.subcolumns()])
    else:
        headers.append(column.name())
Explanation: Convert the columns object to a list of strings.
End of explanation
row_count=3
column_count=7
headers=headers[:column_count]
print headers
Explanation: Subsample rows and columns
End of explanation
column_count = py_interop_table.count_table_columns(columns)
row_offsets = py_interop_table.map_id_offset()
py_interop_table.count_table_rows(run_metrics, row_offsets)
data = numpy.zeros((row_offsets.size(), column_count), dtype=numpy.float32)
py_interop_table.populate_imaging_table_data(run_metrics, columns, row_offsets, data.ravel())
data=data[:row_count, :]
Explanation: The data from imaging table can populate a numpy ndarray as follows:
End of explanation
d = []
for col, label in enumerate(headers):
    d.append( (label, pd.Series([val for val in data[:, col]], index=[tuple(r) for r in  data[:, :3]])))
Explanation: Convert the header list and data ndarray into a Pandas table.
End of explanation
df = pd.DataFrame.from_dict(dict(d))
print(df.to_string(index=False))
Explanation: Render the Imaging Table data using Pandas
End of explanation
valid_to_load = py_interop_run.uchar_vector(py_interop_run.MetricCount, 0)
Explanation: Getting Only Occpuancy from the imaging table
This section shows how to get all metrics from a single InterOp file. Here we are getting all metrics from ExtendedTileMetricsOut.bin, which is % Occupied
End of explanation
valid_to_load[py_interop_run.ExtendedTile] = 1
valid_to_load[py_interop_run.Tile] = 1
valid_to_load[py_interop_run.Extraction] = 1
run_metrics.clear()
run_metrics.read(run_folder, valid_to_load)
py_interop_table.create_imaging_table_columns(run_metrics, columns)
headers = []
for i in range(columns.size()):
    column = columns[i]
    if column.has_children():
        headers.extend([column.name()+"("+subname+")" for subname in column.subcolumns()])
    else:
        headers.append(column.name())
column_count = py_interop_table.count_table_columns(columns)
row_offsets = py_interop_table.map_id_offset()
py_interop_table.count_table_rows(run_metrics, row_offsets)
data = numpy.zeros((row_offsets.size(), column_count), dtype=numpy.float32)
py_interop_table.populate_imaging_table_data(run_metrics, columns, row_offsets, data.ravel())
Explanation: This allows you to select a specific file to load. 
Note that tables require at least one cycle metric set to be built. We include Extraction below for that reason.
End of explanation
data=data[:row_count, :]
Explanation: Select only the first row_count rows.
End of explanation
header_subset = ["Lane", "Tile", "Cycle", "% Occupied"]
header_index = [(header, headers.index(header)) for header in header_subset]
ids = numpy.asarray([headers.index(header) for header in header_subset[:3]])
d = []
for label, col in header_index:
    d.append( (label, pd.Series([val for val in data[:, col]], index=[tuple(r) for r in data[:, ids]])))
Explanation: Select a subset of columns: 
Lane, Tile, Cycle, % Occupied
End of explanation
df = pd.DataFrame.from_dict(dict(d))
Explanation: Convert to a Pandas DataFrame object
End of explanation
df = df.loc[df['Cycle'] == 1.0]
print(df.to_string(index=False))
Explanation: Only display data from the first cycle
End of explanation
<END_TASK>