# Is there a way to introspect on a qiime2.Artifact to find out the available classes for .view()?

**URL:** <https://forum.qiime2.org/t/is-there-a-way-to-introspect-on-a-qiime2-artifact-to-find-out-the-available-classes-for-view/22965>\
**Category:** Developer Discussion\
**Tags:** python-api\
**Created:** [May 3, 2022, 6:43pm UTC](https://forum.qiime2.org/t/is-there-a-way-to-introspect-on-a-qiime2-artifact-to-find-out-the-available-classes-for-view/22965 "2022-05-03T18:43:55Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![rrichter](https://forum.qiime2.org/letter_avatar_proxy/v4/letter/r/f04885/32.png) [@rrichter](https://forum.qiime2.org/u/rrichter)\
**Post date:** [May 3, 2022, 6:43pm UTC](https://forum.qiime2.org/t/is-there-a-way-to-introspect-on-a-qiime2-artifact-to-find-out-the-available-classes-for-view/22965/1 "2022-05-03T18:43:55Z")

</div>

I struggle every time I deal with a new output type to figure out how to pull it into python if it’s not `DataFrame`. Unfortunately, I can’t figure out how to get the class of an `Artifact` object; `PluginManager.transformers` keys are classes and `Artifact.format`, while it’s **str** is the name of the backing class, itself seems to be a semantic type class.

I eventually just printed out a list from directly from PluginManager

```from
for to, frm in PluginManager().transformers.items():
    for f in frm:
        print(f'{to. __module__ }.{to. __name__ } -> {f. __module__ }.{f. __name__ }')

```

and then look back at the list to see what options there are for some `Artifact.format`.

I’d really like a cleaner way to do this, preferably on a loaded Artifact. Anyone know of a way?

Well, actually, what I'd _really_ like was that `.view()` defaulted to the backing object (i.e. `biom`, `skbio.stats.distance.DistanceMatrix`, etc), but that's a feature request, not a question.

---

<div class="post-metadata">

**Author:** ![thermokarst](https://forum.qiime2.org/letter_avatar_proxy/v4/letter/t/8e7dd6/32.png) [@thermokarst](https://forum.qiime2.org/u/thermokarst)\
**Post date:** [May 13, 2022, 6:31pm UTC](https://forum.qiime2.org/t/is-there-a-way-to-introspect-on-a-qiime2-artifact-to-find-out-the-available-classes-for-view/22965/3 "2022-05-13T18:31:47Z")

</div>

Hi @rrichter - you can use an Artifact's `format` property to look up the available transformers:

```python
from qiime2 import Artifact, sdk

pm = sdk.PluginManager()
def transformable_to_view_types(artifact):
    from_format = artifact.format
    if issubclass(from_format, sdk.plugin_manager.SingleFileDirectoryFormatBase):
        from_format = artifact.format.file.format
    return set(pm.transformers[from_format].keys())

# load our own data to check the function above out
table = Artifact.load('table.qza')
print(transformable_to_view_types(table))
# displays:
# {<class 'q2_types.feature_data._format.TSVTaxonomyFormat'>,
# <class 'pandas.core.frame.DataFrame'>,
# <class 'qiime2.metadata.metadata.Metadata'>,
# <class 'biom.table.Table'>}

# neat, looks like we can transform to a pandas dataframe, let's confirm:
import pandas as pd
print(table.view(pd.DataFrame))
...

```

> [@rrichter](#):
>
> Well, actually, what I'd _really_ like was that `.view()` defaulted to the backing object (i.e. `biom`, `skbio.stats.distance.DistanceMatrix`, etc), but that's a feature request, not a question.

The `Artifact.format` _is_ the default backing object's type. It is always a file (because an Artifact's data format must be serializable to disk). How you access that data is going to really depend on the Artifact format - `SingleFileDirectoryFormat` will always have precisely one single file, but a `DirectoryFormat` can have many - some `DirectoryFormat`s have a fixed schema, while others use a regex pattern for a dynamic number of backing files.

Check out the dev docs for more details:

[https://dev.qiime2.org/latest/storing-data/formats/](https://dev.qiime2.org/latest/storing-data/formats/)

---

<div class="post-metadata">

**Author:** ![rrichter](https://forum.qiime2.org/letter_avatar_proxy/v4/letter/r/f04885/32.png) [@rrichter](https://forum.qiime2.org/u/rrichter)\
**Post date:** [May 17, 2022, 6:08pm UTC](https://forum.qiime2.org/t/is-there-a-way-to-introspect-on-a-qiime2-artifact-to-find-out-the-available-classes-for-view/22965/4 "2022-05-17T18:08:36Z")

</div>

Thank you!  
It even looks like I can even use a variant of this to grab the 1st non-`q2_types` object and return that transformed version for just about any `SingleFileDirectoryFormat` type.

Something like

```auto
def default_view(artifact):
    from_format = artifact.format
    if not issubclass(from_format, sdk.plugin_manager.SingleFileDirectoryFormatBase):
        throw(NotImplementedError)
    else:
        from_format = artifact.format.file.format
        for fmt in pm.transformers[from_format]:
            if not 'q2_types' in str(fmt):
                return artifact.view(fmt)

```

Other than sometimes not getting the DataFrame, if it's there, can you see a clear problem with this?  
-Alex

---

<div class="post-metadata">

**Author:** ![thermokarst](https://forum.qiime2.org/letter_avatar_proxy/v4/letter/t/8e7dd6/32.png) [@thermokarst](https://forum.qiime2.org/u/thermokarst)\
**Post date:** [May 17, 2022, 6:09pm UTC](https://forum.qiime2.org/t/is-there-a-way-to-introspect-on-a-qiime2-artifact-to-find-out-the-available-classes-for-view/22965/5 "2022-05-17T18:09:18Z")

</div>



---

<div class="post-metadata">

**Author:** ![thermokarst](https://forum.qiime2.org/letter_avatar_proxy/v4/letter/t/8e7dd6/32.png) [@thermokarst](https://forum.qiime2.org/u/thermokarst)\
**Post date:** [May 20, 2022, 5:08pm UTC](https://forum.qiime2.org/t/is-there-a-way-to-introspect-on-a-qiime2-artifact-to-find-out-the-available-classes-for-view/22965/6 "2022-05-20T17:08:52Z")

</div>

> [@rrichter](#):
>
> can you see a clear problem with this?

This is unstable - the insertion order of the transformers isn't guaranteed, you might run into different "defaults" across plugin manager instantiations.

Please review the docs I shared above - even if this dict was stable you still aren't guaranteed to get a non-file-based format using this approach.

---

<div class="post-metadata">

**Author:** ![thermokarst](https://forum.qiime2.org/letter_avatar_proxy/v4/letter/t/8e7dd6/32.png) [@thermokarst](https://forum.qiime2.org/u/thermokarst)\
**Post date:** [May 20, 2022, 5:09pm UTC](https://forum.qiime2.org/t/is-there-a-way-to-introspect-on-a-qiime2-artifact-to-find-out-the-available-classes-for-view/22965/7 "2022-05-20T17:09:12Z")

</div>


