# New contributed notebook on subtracted backgrounds

**URL:** <https://www.rubin.community/t/new-contributed-notebook-on-subtracted-backgrounds/6346>\
**Category:** Support\
**Tags:** dp0\
**Created:** [February 24, 2022, 6:21pm UTC](https://www.rubin.community/t/new-contributed-notebook-on-subtracted-backgrounds/6346 "2022-02-24T18:21:44Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![MelissaGraham](https://sea2.discourse-cdn.com/flex002/user_avatar/www.rubin.community/melissagraham/32/2549_2.png) [@MelissaGraham](https://www.rubin.community/u/MelissaGraham)\
**Post date:** [February 24, 2022, 6:21pm UTC](https://www.rubin.community/t/new-contributed-notebook-on-subtracted-backgrounds/6346/1 "2022-02-24T18:21:44Z")

</div>

In the shared `delegate-contributions-dp01` repository there is a new directory, `backgrounds/`, with a new notebook that explains the basics of how to retrieve and display the backgrounds which were subtracted from `calexps` and `deepCoadds`.

[View the notebook intro-to-backgrounds.ipynb](https://github.com/rubin-dp0/delegate-contributions-dp01/blob/main/backgrounds/intro-to-backgrounds.ipynb).

The notebook also contains a few links to more information about background estimation and subtraction in the LSST Science Pipelines. It is a fairly basic demonstration, and DP0 Delegates might be interested to develop and contribute notebooks to the `backgrounds/` directory that, e.g., demonstrate science analyses which make use of backgrounds, or alternative background estimators.

> **How do I get a copy of this notebook?**
>
> Unlike the `tutorial-notebooks` repository ([here](https://github.com/rubin-dp0/tutorial-notebooks)), `delegate-contributions-dp01` repository ([here](https://github.com/rubin-dp0/delegate-contributions-dp01)) does not automatically appear in the home directories of all RSP user accounts. Delegates must `git clone` the `delegate-contributions-dp01` repository for themselves. If you’re new to GitHub, [these instructions](https://github.com/rubin-dp0/delegate-contributions-dp01#need-to-learn-how-to-use-github) will help you out.

---

<div class="post-metadata">

**Author:** ![babel](https://sea2.discourse-cdn.com/flex002/user_avatar/www.rubin.community/babel/32/2085_2.png) [@babel](https://www.rubin.community/u/babel)\
**Post date:** [March 7, 2022, 5:16pm UTC](https://www.rubin.community/t/new-contributed-notebook-on-subtracted-backgrounds/6346/2 "2022-03-07T17:16:42Z")

</div>

I got a skymap error when I ran this - I had to add ‘skymap’: ‘DC2’ to the coaddId:

coaddId = {‘tract’: 4226, ‘patch’: 17, ‘band’: ‘r’, ‘skymap’: ‘DC2’}  
coadd = butler.get(‘deepCoadd\_calexp’, coaddId)

---

<div class="post-metadata">

**Author:** ![MelissaGraham](https://sea2.discourse-cdn.com/flex002/user_avatar/www.rubin.community/melissagraham/32/2549_2.png) [@MelissaGraham](https://www.rubin.community/u/MelissaGraham)\
**Post date:** [March 7, 2022, 6:00pm UTC](https://www.rubin.community/t/new-contributed-notebook-on-subtracted-backgrounds/6346/3 "2022-03-07T18:00:52Z")

</div>

@babel could you post a copy-paste of your code and the exact error message? (or upload a screenshot if that’s more convenient)

---

<div class="post-metadata">

**Author:** ![babel](https://sea2.discourse-cdn.com/flex002/user_avatar/www.rubin.community/babel/32/2085_2.png) [@babel](https://www.rubin.community/u/babel)\
**Post date:** [March 8, 2022, 4:38pm UTC](https://www.rubin.community/t/new-contributed-notebook-on-subtracted-backgrounds/6346/4 "2022-03-08T16:38:57Z")

</div>

I think this is the same issue Jim Bosch resolved for me recently:

> [@afwTables: A Guided Tour - No value in data ID error](http://www.rubin.community/t/afwtables-a-guided-tour-no-value-in-data-id-error/6334/5):
>
> Ok, I see what happened: we registered a new skymap in the main skymaps collection, which is chained to 2.2i/runs/DP0.1, and hence it’s no longer true that there is only one skymap value for the collection used to initialize the butler, and as a result there’s no default value for the skymap data ID key. The fix is to add skymap="DC2" to the data ID. This is going to affect a lot of other users who rely on that defaulting, and it’s a problem I hadn’t anticipated with having all skymaps go into…

Here’s the input (I commented out the fix):

# coaddId = {‘tract’: 4226, ‘patch’: 17, ‘band’: ‘r’, ‘skymap’: ‘DC2’}

coaddId = {‘tract’: 4226, ‘patch’: 17, ‘band’: ‘r’}  
coadd = butler.get(‘deepCoadd\_calexp’, coaddId)

## and the error message:

KeyError Traceback (most recent call last)  
/opt/lsst/software/stack/stack/miniconda3-py38\_4.9.2-0.7.0/Linux64/daf\_butler/22.0.1-110-g1427568b+500492d978/python/lsst/daf/butler/core/dimensions/\_coordinate.py in standardize(mapping, graph, universe, defaults, \*\*kwargs)  
233 try:  
 → 234 values = tuple(d[name] for name in graph.required.names)  
235 except KeyError as err:

/opt/lsst/software/stack/stack/miniconda3-py38\_4.9.2-0.7.0/Linux64/daf\_butler/22.0.1-110-g1427568b+500492d978/python/lsst/daf/butler/core/dimensions/\_coordinate.py in (.0)  
233 try:  
 → 234 values = tuple(d[name] for name in graph.required.names)  
235 except KeyError as err:

KeyError: ‘skymap’

The above exception was the direct cause of the following exception:

KeyError Traceback (most recent call last)  
/tmp/ipykernel\_4563/2571319927.py in   
1 # coaddId = {‘tract’: 4226, ‘patch’: 17, ‘band’: ‘r’, ‘skymap’: ‘DC2’}  
2 coaddId = {‘tract’: 4226, ‘patch’: 17, ‘band’: ‘r’}  
----\> 3 coadd = butler.get(‘deepCoadd\_calexp’, coaddId)

/opt/lsst/software/stack/stack/miniconda3-py38\_4.9.2-0.7.0/Linux64/daf\_butler/22.0.1-110-g1427568b+500492d978/python/lsst/daf/butler/\_butler.py in get(self, datasetRefOrType, dataId, parameters, collections, \*\*kwargs)  
1160 “”"  
1161 log.debug(“Butler get: %s, dataId=%s, parameters=%s”, datasetRefOrType, dataId, parameters)  
 → 1162 ref = self.\_findDatasetRef(datasetRefOrType, dataId, collections=collections, \*\*kwargs)  
1163 return self.getDirect(ref, parameters=parameters)  
1164

/opt/lsst/software/stack/stack/miniconda3-py38\_4.9.2-0.7.0/Linux64/daf\_butler/22.0.1-110-g1427568b+500492d978/python/lsst/daf/butler/\_butler.py in \_findDatasetRef(self, datasetRefOrType, dataId, collections, allowUnresolved, \*\*kwargs)  
902 # type instead of letting registry.findDataset do it, so we get the  
903 # result even if no dataset is found.  
 → 904 dataId = DataCoordinate.standardize(dataId, graph=datasetType.dimensions,  
905 defaults=self.registry.defaults.dataId, \*\*kwargs)  
906 # Always lookup the DatasetRef, even if one is given, to ensure it is

/opt/lsst/software/stack/stack/miniconda3-py38\_4.9.2-0.7.0/Linux64/daf\_butler/22.0.1-110-g1427568b+500492d978/python/lsst/daf/butler/core/dimensions/\_coordinate.py in standardize(mapping, graph, universe, defaults, \*\*kwargs)  
234 values = tuple(d[name] for name in graph.required.names)  
235 except KeyError as err:  
 → 236 raise KeyError(f"No value in data ID ({mapping}) for required dimension {err}.") from err  
237 # Some backends cannot handle numpy.int64 type which is a subclass of  
238 # numbers.Integral; convert that to int.

KeyError: “No value in data ID ({‘tract’: 4226, ‘patch’: 17, ‘band’: ‘r’}) for required dimension ‘skymap’.”

---

<div class="post-metadata">

**Author:** ![babel](https://sea2.discourse-cdn.com/flex002/user_avatar/www.rubin.community/babel/32/2085_2.png) [@babel](https://www.rubin.community/u/babel)\
**Post date:** [March 8, 2022, 5:31pm UTC](https://www.rubin.community/t/new-contributed-notebook-on-subtracted-backgrounds/6346/5 "2022-03-08T17:31:44Z")

</div>

Is there a typo towards the end of the subtracted background code? In the second line below, should that be “coadd2Id” instead of “coaddId”? In that case “skymap”: “DC2” needs to be added to the first line.

coadd2Id = {‘tract’: 4226, ‘patch’: 17, ‘band’: ‘r’}  
coadd2 = butler.get(‘deepCoadd’, coaddId)

This code is an excellent contribution to the tutorials - it’s taught me a lot about background subtraction and proper coding techniques - thank you for this!

---

<div class="post-metadata">

**Author:** ![MelissaGraham](https://sea2.discourse-cdn.com/flex002/user_avatar/www.rubin.community/melissagraham/32/2549_2.png) [@MelissaGraham](https://www.rubin.community/u/MelissaGraham)\
**Post date:** [March 10, 2022, 8:53pm UTC](https://www.rubin.community/t/new-contributed-notebook-on-subtracted-backgrounds/6346/6 "2022-03-10T20:53:55Z")

</div>

Hmm, I don’t get the same error when `'skymap':'DC1'` is added to the `coaddId` in that cell (see screenshot below). Is the error message coming from a different cell, or perhaps were other changes made?

 ![Screen Shot 2022-03-10 at 12.48.23 PM](https://us1.discourse-cdn.com/flex002/uploads/rubin/original/2X/9/9e0063d05a9376356f9640f28cdb84d89ae853ff.png)

---

<div class="post-metadata">

**Author:** ![MelissaGraham](https://sea2.discourse-cdn.com/flex002/user_avatar/www.rubin.community/melissagraham/32/2549_2.png) [@MelissaGraham](https://www.rubin.community/u/MelissaGraham)\
**Post date:** [March 10, 2022, 8:55pm UTC](https://www.rubin.community/t/new-contributed-notebook-on-subtracted-backgrounds/6346/7 "2022-03-10T20:55:36Z")

</div>

Yes, there was a typo in the fourth-last cell of this notebook, and in the second line it was `coaddId` and now it is `coadd2Id` – good catch! It is now fixed.

![Screen Shot 2022-03-10 at 12.52.22 PM](https://us1.discourse-cdn.com/flex002/uploads/rubin/original/2X/d/dcce6f99d76be5bb6f2ff3c4acd0148c30b546f5.png)
