Created
June 11, 2018 17:53
-
-
Save tlancon/4c8ebf654d58467fcd6cea56f9bfab0f to your computer and use it in GitHub Desktop.
Converts HxLabelAnalysis or HxSpreadSheet objects to Pandas data frames in Amira/Avizo.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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): | |
sscl.append(interface.tables[0].columns[i].name) | |
# Get data from spreadsheet into an array | |
ssnda = np.empty([nrows,ncols]) | |
for i in range(0,nrows): | |
for j in range(0,ncols): | |
try: | |
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