Skip to content

Instantly share code, notes, and snippets.

@taldcroft
Created January 28, 2015 22:40
Show Gist options
  • Save taldcroft/5062733933da7531d02a to your computer and use it in GitHub Desktop.
Save taldcroft/5062733933da7531d02a to your computer and use it in GitHub Desktop.
# Example - Generating the Dataframe and Panel objects - pulled from pyger_cases.py
#
# The code below was pieced together from snippets in pyger_cases.py and was not fully tested.
# Testing is not entirely possible since some of it is fictitious to simplify the example.
#
# The dictionary values, pitch_set, hot_max, hot90, and cool90 are one dimensional numpy arrays
# representing heating and cooling data for one case (i.e. set of conditions). Each case is designed
# to limit the dwell time for one msid. For example, a set of cases would be used to represent the
# dwell capability for each msid at their current planning limit along with a common number of ACIS
# CCDs (where applicable).
#
colnames = ['pitch', 'hot_max', 'hot90', 'cool90']
for case in cases:
# Imaginary function to simplify this example
pitch, hot_max, hot90, cool90 = fetch_simulation_data(case)
frame = table.Table([pitch, hot_max, hot90, cool90], names=colnames)
# Note that each key is the msid and the value is the dataframe
panel_dict[case['msid']] = frame
cols2d = []
msids = np.array([case['msid'] for case in cases])
for colname in colnames:
# This is what astropy table can do that Pandas cannot: support for arbitrary
# dimensions in the column. Make a 2-d column with the axis=1 dimension being case.
col2d = np.vstack([panel_dict[msid][colname] for msid in msids])
cols2d.append(col2d.transpose())
constraint_panel = table.Table(cols2d, names=colnames)
allowable_hot_time = np.min(constraint_panel['max_hot_time'], axis=1)
allowable_max_hot_time_idx = np.argmin(constraint_panel['max_hot_time'], axis=1)
allowable_max_hot_time_msid = msids[allowable_max_hot_time_idx]
panel_dict= {}
for case in cases:
# Imaginary function to simplify this example
pitch_set, hot_max, hot90, cool90 = fetch_simulation_data(case)
frame = pandas.DataFrame({'max_hot_time':hot_max, 'hot_dwell':hot90, 'cool_dwell':cool90},
index=pitch_set)
# Note that each key is the msid and the value is the dataframe
panel_dict[case['msid']] = frame
# 3 dimensional dataset
constraint_panel = pandas.Panel(panel_dict)
# Determine the minimum values in the "max_hot_time" column for each pitch (2D table index) through
# all cases (indexed by msid). To put it a different way, I want to know the minimum allowed time
# (as listed in the "max_hot_time" column) at each pitch, given a set of cases where each case
# represents the capability when limiting one msid.
#
allowable_hot_time = constraint_panel.minor_xs('max_hot_time').min(axis=1)
# Similar operation, only instead of storing the minimum value, I want the name of the case (msid)
# that has the minimum value at each pitch.
allowable_max_hot_time_msid = constraint_panel.minor_xs('max_hot_time').idxmin(axis=1)
# The last two lines of code use a method called "minor_xs" which grabs the slice of data for one
# column into the third dimension (over all msids/cases). I then grab the minimum values along the
# second axis (axis=1) using the "min" method. In this "sliced" table, axis=1 corresponds to the
# different cases.
# This data can be combined into one datastructure to make it easier to pass around.
max_hot_time = pd.DataFrame({'max_time': allowable_hot_time,
'limiting_msid': allowable_max_hot_time_msid})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment