# How to setup subject hierarchy tree filtering?

**URL:** <https://discourse.slicer.org/t/how-to-setup-subject-hierarchy-tree-filtering/32435>\
**Category:** Development\
**Created:** [October 26, 2023, 10:53pm UTC](https://discourse.slicer.org/t/how-to-setup-subject-hierarchy-tree-filtering/32435 "2023-10-26T22:53:00Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![jamesobutler](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jamesobutler/32/7511_2.png) [@jamesobutler](https://discourse.slicer.org/u/jamesobutler)\
**Post date:** [October 26, 2023, 10:53pm UTC](https://discourse.slicer.org/t/how-to-setup-subject-hierarchy-tree-filtering/32435/1 "2023-10-26T22:53:00Z")

</div>

I’m currently having difficulty filtering the subject hierarchy using the attribute filter API. The following illustrates the unexpected behavior upon using `setAttributeFilter`. Does anyone know why this may be happening?

```python
tree_view = slicer.qMRMLSubjectHierarchyTreeView()
tree_view.setMRMLScene(slicer.mrmlScene)
line_node_1 = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLMarkupsLineNode", "Line1")
line_node_2 = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLMarkupsLineNode", "Line2")
line_node_1.SetAttribute("Name", "Value")

```

![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/1/b/1b5779bf066d4a36d0e6790275f4c303342b6360.png)

`tree_view.displayedItemCount()` is 2

```python
tree_view.setAttributeFilter("Name", "Value")

```

![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/8/f/8ff42fcdd4396867cdabc388de9cfdbc74f16bb9.png)

`tree_view.displayedItemCount()` is 0, although it was expected to be 1.

---

<div class="post-metadata">

**Author:** ![jcfr](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jcfr/32/17825_2.png) [@jcfr](https://discourse.slicer.org/u/jcfr)\
**Post date:** [October 27, 2023, 1:53am UTC](https://discourse.slicer.org/t/how-to-setup-subject-hierarchy-tree-filtering/32435/2 "2023-10-27T01:53:09Z")

</div>

You may also want to look in the recent changes related to filtering that were introduced in [BUG: Fix SubjectHierarchyGenericSelfTest by cpinter · Pull Request #7289 · Slicer/Slicer · GitHub](https://github.com/Slicer/Slicer/pull/7289)

---

<div class="post-metadata">

**Author:** ![cpinter](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/cpinter/32/7995_2.png) [@cpinter](https://discourse.slicer.org/u/cpinter)\
**Post date:** [October 27, 2023, 8:47am UTC](https://discourse.slicer.org/t/how-to-setup-subject-hierarchy-tree-filtering/32435/3 "2023-10-27T08:47:30Z")

</div>

The `setAttributeFilter` method sets an attribute filter for _items_ and not nodes. This is why when you set a node attribute but then were filtering on item attribute it did not show anything. It is also deprecated. See

> <https://github.com/Slicer/Slicer/blob/3f46b467d3cde16d0d60e0e059439eea2728a967/Modules/Loadable/SubjectHierarchy/Widgets/qMRMLSortFilterSubjectHierarchyProxyModel.h#L88-L90>

Please use these properties instead:

> <https://github.com/Slicer/Slicer/blob/3f46b467d3cde16d0d60e0e059439eea2728a967/Modules/Loadable/SubjectHierarchy/Widgets/qMRMLSortFilterSubjectHierarchyProxyModel.h#L73-L86>

The node and item attribute filters have their different properties, as well as their include and exclude variants.

---

<div class="post-metadata">

**Author:** ![jamesobutler](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jamesobutler/32/7511_2.png) [@jamesobutler](https://discourse.slicer.org/u/jamesobutler)\
**Post date:** [October 27, 2023, 11:37am UTC](https://discourse.slicer.org/t/how-to-setup-subject-hierarchy-tree-filtering/32435/4 "2023-10-27T11:37:15Z")

</div>

@cpinter Is that deprecation comment wrong in that it says `Sets and returns the first attribute in \sa includeNodeAttributeNamesFilter` when actually it should be `Sets and returns the first attribute in \sa includeItemAttributeNamesFilter`?

Also does this mean the non-deprecated API can only filter by attribute name and not also by attribute value like `SetAttributeFilter` could do?

---

<div class="post-metadata">

**Author:** ![cpinter](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/cpinter/32/7995_2.png) [@cpinter](https://discourse.slicer.org/u/cpinter)\
**Post date:** [October 27, 2023, 12:06pm UTC](https://discourse.slicer.org/t/how-to-setup-subject-hierarchy-tree-filtering/32435/5 "2023-10-27T12:06:11Z")

</div>

1. You’re right, the comment is referencing the wrong function, it should be `includeItemAttributeNamesFilter`.

2. No, you can still filter by value, see

> <https://github.com/Slicer/Slicer/blob/3f46b467d3cde16d0d60e0e059439eea2728a967/Modules/Loadable/SubjectHierarchy/Widgets/qMRMLSortFilterSubjectHierarchyProxyModel.h#L115-L139>

I think the best thing to do would be to fix that comment in (1), and add an actual deprecation warning in the bodies of `setAttributeNameFilter` and `setAttributeValueFilter`.

---

<div class="post-metadata">

**Author:** ![jamesobutler](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jamesobutler/32/7511_2.png) [@jamesobutler](https://discourse.slicer.org/u/jamesobutler)\
**Post date:** [October 27, 2023, 12:38pm UTC](https://discourse.slicer.org/t/how-to-setup-subject-hierarchy-tree-filtering/32435/6 "2023-10-27T12:38:27Z")

</div>

Yes I agree with those improvements.

Since there are some methods specific to items and others for nodes it was getting a bit confusing. I was thinking of “item” as a generic term about a thing in a list of things which could be a node object.

---

<div class="post-metadata">

**Author:** ![jamesobutler](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jamesobutler/32/7511_2.png) [@jamesobutler](https://discourse.slicer.org/u/jamesobutler)\
**Post date:** [October 27, 2023, 2:05pm UTC](https://discourse.slicer.org/t/how-to-setup-subject-hierarchy-tree-filtering/32435/7 "2023-10-27T14:05:05Z")

</div>

Here’s an update to my original example code that shows how to successfully filter using node attributes.

```python
tree_view = slicer.qMRMLSubjectHierarchyTreeView()
tree_view.setMRMLScene(slicer.mrmlScene)
line_node_1 = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLMarkupsLineNode", "Line1")
line_node_2 = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLMarkupsLineNode", "Line2")
line_node_1.SetAttribute("Name", "Value")
tree_view.displayedItemCount() # 2

tree_view.addNodeAttributeFilter("Name", "Value")
tree_view.displayedItemCount() # 1

tree_view.removeNodeAttributeFilter("Name", "Value")
tree_view.displayedItemCount() # 2

```

---

<div class="post-metadata">

**Author:** ![jcfr](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jcfr/32/17825_2.png) [@jcfr](https://discourse.slicer.org/u/jcfr)\
**Post date:** [October 31, 2023, 2:10am UTC](https://discourse.slicer.org/t/how-to-setup-subject-hierarchy-tree-filtering/32435/8 "2023-10-31T02:10:57Z")

</div>

The following should help clarify

1. Create the tree view

2. Set the node attribute

3. Add a subject hierarchy item attribute fltter

4. Set the subject hierarchy item attribute

---

<div class="post-metadata">

**Author:** ![jcfr](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jcfr/32/17825_2.png) [@jcfr](https://discourse.slicer.org/u/jcfr)\
**Post date:** [November 15, 2023, 6:14pm UTC](https://discourse.slicer.org/t/how-to-setup-subject-hierarchy-tree-filtering/32435/9 "2023-11-15T18:14:25Z")

</div>

For reference, deprecation warnings and improved documentation are being integrated through the following pull request:

> <https://github.com/Slicer/Slicer/pull/7314>
>
> Re https://discourse.slicer.org/t/problems-filtering-subject-hierarchy-tree/3243…5/5
