Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Converts HxLabelAnalysis or HxSpreadSheet objects to Pandas data frames in Amira/Avizo.
# Copy/paste into the Avizo/Amira Python console.
# Alternatively, save to a file, add that file to your path, and import.
# See docstring for usage.
def labelanalysis_to_dataframe(tablelike_object):
Converts a tablelike object (HxSpreadSheet, HxLabelAnalysis, or anything with
.all_interfaces.HxSpreadSheetInterface) to a Pandas data frame with the column names intact.
You must use the object handle, not just the name of the object! For example,
to convert a HxLabelAnalysis data object named "chocolate-bar.Label-Analysis":
>>> spreadsheet_data = hx_project.get('chocolate-bar.Label-Analysis')
>>> df = labelanalysis_to_dataframe(spreadsheet_data)
Returns a Pandas data frame.
import numpy as np
import pandas as pd
# Grab interface to data in spreadsheet
interface = tablelike_object.all_interfaces.HxSpreadSheetInterface
# Count number of columns/rows
nrows = len(interface.tables[0].rows)
ncols = len(interface.tables[0].columns)
# Create a list of column names in spreadsheet
sscl = []
for i in range(0,ncols):
# Get data from spreadsheet into an array
ssnda = np.empty([nrows,ncols])
for i in range(0,nrows):
for j in range(0,ncols):
ssnda[i,j] = interface.tables[0].items[i,j]
except ValueError:
ssnda[i,j] = 0
# Pass array to data frame with correct column names
ssdf_b = pd.DataFrame(data=ssnda,index=range(0,nrows),columns=sscl)
return ssdf_b
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment