Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

KeyError: 'the label [time] is not in the [index]' #141

Closed
jgphpc opened this issue Apr 14, 2020 · 3 comments
Closed

KeyError: 'the label [time] is not in the [index]' #141

jgphpc opened this issue Apr 14, 2020 · 3 comments

Comments

@jgphpc
Copy link

jgphpc commented Apr 14, 2020

Hello,

Printing the tree of caliper-ex.cali can be done by calling cali-query:

cali-query -q 'select function,sum(sum#time.duration),
inclusive_sum(sum#time.duration) group by 
function format json-split' caliper-ex.cali > 0.json

and into hatchet:

>>> import hatchet as ht
>>> gf = ht.GraphFrame.from_caliper_json('./0.json')
>>> print(gf.tree())

but it will fail with:

Traceback (most recent call last):
  File "pandas/core/indexing.py", line 1790, in _validate_key
    error()
  File "pandas/core/indexing.py", line 1785, in error
    axis=self.obj._get_axis_name(axis)))
KeyError: 'the label [time] is not in the [index]'

The traceback is:

During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "hatchet/graphframe.py", line 539, in tree
    color=color,
  File "hatchet/external/printtree.py", line 64, in trees_as_text
    color=color,
  File "hatchet/external/printtree.py", line 102, in as_text
    node_time = dataframe.loc[df_index, metric]
  File "pandas/core/indexing.py", line 1472, in __getitem__
    return self._getitem_tuple(key)
  File "pandas/core/indexing.py", line 870, in _getitem_tuple
    return self._getitem_lowerdim(tup)
  File "pandas/core/indexing.py", line 1027, in _getitem_lowerdim
    return getattr(section, self.name)[new_key]
  File "pandas/core/indexing.py", line 1478, in __getitem__
    return self._getitem_axis(maybe_callable, axis=axis)
  File "pandas/core/indexing.py", line 1911, in _getitem_axis
    self._validate_key(key, axis)
  File "pandas/core/indexing.py", line 1798, in _validate_key
    error()
  File "pandas/core/indexing.py", line 1785, in error
    axis=self.obj._get_axis_name(axis)))
KeyError: 'the label [time] is not in the [index]'

The solution is to rename the second column in the json file:

grep columns 0.json
"columns": [ "inclusive#sum#time.duration", "sum#time.duration", "path" ],

instead of:

grep columns 0.json
"columns": [ "inclusive#sum#time.duration", "sum#sum#time.duration", "path" ],

My version is:

cali-query --version

2.3.0

caliper-ex.json is actually fine. Am i using cali-query in a wrong way ?

Thanks,

jg.

@slabasan
Copy link
Collaborator

Hi @jgphpc,

Welcome to Hatchet! You are using cali-query in the correct way. The tree API by default assumes that an index called time exists in the dataframe, but for caliper, the time column really depends on what the user has specified in the query.

In your example, you actually have 2 different time-related columns to choose from. So rather than renaming a column in your dataframe, you can change the metric parameter in the tree API:
print(gf.tree(metric="inclusive#sum#time.duration"))
and
print(gf.tree(metric="sum#sum#time.duration"))

Hope this helps, and let us know if you have any more questions.

@jgphpc
Copy link
Author

jgphpc commented Apr 14, 2020

This is amazing 👍 thank you

gf.dataframe.columns

Index(['time (inc)', 'sum#sum#time.duration', 'nid', 'name'], dtype='object')

print(gf.tree(metric="sum#sum#time.duration"))

0.025 main
├─ 0.000 TimeIncrement
└─ 0.000 LagrangeLeapFrog
   ├─ 0.012 LagrangeNodal
   │  └─ 0.002 CalcForceForNodes
   │     └─ 0.002 CalcVolumeForceForElems
   │        ├─ 0.057 IntegrateStressForElems
   │        └─ 0.060 CalcHourglassControlForElems
   │           └─ 0.081 CalcFBHourglassForceForElems
   ├─ 0.001 LagrangeElements
   │  ├─ 0.003 CalcLagrangeElements
   │  │  └─ 0.056 CalcKinematicsForElems
   │  ├─ 0.032 CalcQForElems
   │  │  └─ 0.014 CalcMonotonicQForElems
   │  └─ 0.002 ApplyMaterialPropertiesForElems
   │     └─ 0.045 EvalEOSForElems
   │        └─ 0.096 CalcEnergyForElems
   └─ 0.005 CalcTimeConstraintsForElems

print(gf.tree(metric="time (inc)"))

0.492 main
├─ 0.000 TimeIncrement
└─ 0.467 LagrangeLeapFrog
   ├─ 0.213 LagrangeNodal
   │  └─ 0.201 CalcForceForNodes
   │     └─ 0.199 CalcVolumeForceForElems
   │        ├─ 0.057 IntegrateStressForElems
   │        └─ 0.140 CalcHourglassControlForElems
   │           └─ 0.081 CalcFBHourglassForceForElems
   ├─ 0.249 LagrangeElements
   │  ├─ 0.059 CalcLagrangeElements
   │  │  └─ 0.056 CalcKinematicsForElems
   │  ├─ 0.046 CalcQForElems
   │  │  └─ 0.014 CalcMonotonicQForElems
   │  └─ 0.143 ApplyMaterialPropertiesForElems
   │     └─ 0.141 EvalEOSForElems
   │        └─ 0.096 CalcEnergyForElems
   └─ 0.005 CalcTimeConstraintsForElems

@slabasan
Copy link
Collaborator

Looks great!! Other things you can do with the tree API are specifying colors (color=True), change precision (precision=2), and specify particular ranks/threads (rank=1, thread=0). We're also working on adding a depth parameter to the tree, so you can opt to print a certain number of levels of the tree if your tree has many levels.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants