|
| 1 | +# %% [markdown] |
| 2 | +# This script shows off how we can filter collections down to flat lists of |
| 3 | +# products using the tools in HIPPO. The main object of interest here is the |
| 4 | +# `FilteredCollection` class. |
| 5 | +# |
| 6 | +# The hippo API is broken down into modules that you can use to interact wtih certain |
| 7 | +# sub-sections of the API. Here, we're going to be interacting with three main concepts: |
| 8 | +# |
| 9 | +# 1. Collections: A collection is a group of products that are related in some way. For |
| 10 | +# our case here, the colleciton is the DR4/S13 150 GHz data. |
| 11 | +# 2. Products: A product is a single piece of data. In this case, we're going to be |
| 12 | +# working with the "splits" in a particular patch. |
| 13 | +# 3. Relationships: Products can be related to one another. In this case, we're going to |
| 14 | +# be creating a parent/child relationship between the splits and the co-added map. |
| 15 | +# |
| 16 | +# There are two main objects of interest that are accessed through the core |
| 17 | +# `ClientSettings` singleton: |
| 18 | +# |
| 19 | +# 1. The `client`: this object is your HTTP link to the hippo server. |
| 20 | +# 2. The `cache`: this is an on-disk cache of products in hippo. This is the only |
| 21 | +# way you should interact with the filesystem for existing products when using hippo. |
| 22 | +# %% |
| 23 | +from henry import Henry |
| 24 | +from hippoclient.core import ClientSettings |
| 25 | + |
| 26 | +settings = ClientSettings( |
| 27 | + host="http://localhost:8000", |
| 28 | + token_tag=None, |
| 29 | + caches=[ |
| 30 | + { |
| 31 | + "type": "filesystem", |
| 32 | + "path": "/tmp", |
| 33 | + } |
| 34 | + ], |
| 35 | +) |
| 36 | + |
| 37 | +henry = Henry(settings=settings, slugify=True) |
| 38 | + |
| 39 | +# %% [markdown] |
| 40 | +# We can filter on three things: |
| 41 | +# |
| 42 | +# - Product type (based on its metadata class) |
| 43 | +# - Metadata keys and values |
| 44 | +# - Available slugs. |
| 45 | +# |
| 46 | + |
| 47 | +# %% |
| 48 | +PATCH = "D6" |
| 49 | +COLLECTION_ID = "6aee15399c65b086f7388bc4" |
| 50 | + |
| 51 | +if COLLECTION_ID is None: |
| 52 | + raise ValueError( |
| 53 | + "Please set the COLLECTION variable to the ID of the collection you want to use." |
| 54 | + ) |
| 55 | + |
| 56 | +collection = henry.pull_collection( |
| 57 | + COLLECTION_ID, realize_sources=False, pull_children=True |
| 58 | +) |
| 59 | + |
| 60 | +filtered = collection.filter( |
| 61 | + metadata_filter={"patch": PATCH}, |
| 62 | +) |
| 63 | + |
| 64 | +print("Found", len(filtered.products), "matching products") |
| 65 | + |
| 66 | + |
| 67 | +# %% [markdown] |
| 68 | +# We can further filter collections, using the same functionality. If we are _sure_ |
| 69 | +# that we want to have only a single (or no) product, we can realize this with the |
| 70 | +# `one` method. The first cell below -- called on our filtered collection -- will raise |
| 71 | +# an error: |
| 72 | + |
| 73 | +# %% |
| 74 | +match = filtered.one() |
| 75 | + |
| 76 | +# %% |
| 77 | +SPLIT = "set1" |
| 78 | + |
| 79 | +second_filter = filtered.filter( |
| 80 | + metadata_filter={"split": SPLIT}, |
| 81 | +) |
| 82 | + |
| 83 | +print("Found", len(second_filter.products), "matching products") |
| 84 | +product = second_filter.one() |
| 85 | + |
| 86 | +# %% [markdown] |
| 87 | +# Now we have a single product that matches our query. There are two options here: |
| 88 | +# we can realize this product, or check it out to the filesystem. |
| 89 | + |
| 90 | +# %% |
| 91 | +realized_product = henry.realize(product) |
| 92 | +print(realized_product) |
| 93 | +# %% [markdown] |
| 94 | +# This realizes all of the 'slugs' and associated sources. If you wanted just a sub-set |
| 95 | +# you could have pasesd the 'slugs' argument. To check out the product to the filesystem, |
| 96 | +# you can also use the `checkout` method: |
| 97 | + |
| 98 | +# %% |
| 99 | +checked_out_product = henry.checkout( |
| 100 | + product, directory="/tmp/product_checkout", slugs=["xlink"] |
| 101 | +) |
| 102 | + |
| 103 | +# %% |
| 104 | +print(checked_out_product) |
| 105 | +for slug, source in checked_out_product.sources.items(): |
| 106 | + print(f"Slug: {slug}, Source: {source}") |
| 107 | +# %% [markdown] |
| 108 | +# With both, we can now access the products directly: |
| 109 | + |
| 110 | +# %% |
| 111 | +print("Realized source_only map:", realized_product["source_only"]) |
| 112 | +print("Checked out xlink map:", checked_out_product["xlink"]) |
| 113 | + |
| 114 | +# %% |
| 115 | +from astropy.io import fits |
| 116 | + |
| 117 | +source_only_map = fits.open(realized_product["source_only"].path) |
| 118 | +xlink_map = fits.open(checked_out_product["xlink"].path) |
| 119 | +# %% |
0 commit comments