# Synthetic source injection on multiple visits

**URL:** <https://www.rubin.community/t/synthetic-source-injection-on-multiple-visits/8480>\
**Category:** Support\
**Tags:** dp0\
**Created:** [April 16, 2024, 11:32pm UTC](https://www.rubin.community/t/synthetic-source-injection-on-multiple-visits/8480 "2024-04-16T23:32:48Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![alxogm](https://sea2.discourse-cdn.com/flex002/user_avatar/www.rubin.community/alxogm/32/1751_2.png) [@alxogm](https://www.rubin.community/u/alxogm)\
**Post date:** [April 16, 2024, 11:32pm UTC](https://www.rubin.community/t/synthetic-source-injection-on-multiple-visits/8480/1 "2024-04-16T23:32:48Z")

</div>

Hi, I had been working on something along the lines demonstrated in the DP02\_14\_Injecting\_Synthetic\_Sources.ipynb tutorial, but applied for strong Lensing purposes, however, I have a few questions/issues that I hope I can get some help with.

1. From the tutorial point of view, i.e. from the Jupyter notebook, is it possible to write the injected exposures back to a butler so that further analysis on the injected visits can be done?

2. About the injection of the same synthetic image into different visits to coadd them afterwards, my impression is that the notebook tutorial would already take into account for the correct rotation of the injected image, this is specially important if its an extended image, like a simulated lens. But to verify this I would like to coadd the images, so it takes me back to point 1. Is there another way I could check the alignment is correct?

3. Furthermore, I’ve tried to inject the same synthetic image, a lens, to several visits from the command line using the pipeline tasks, and I get the injection right but the wrong alignment. I haven’t found a way to tell the injection task how to align the image to a given visit. This is the command line I’ve used to produce the two images below, as you can see in both I get exactly the same orientation of the injected lens, therefore, if I coadd them I’ll get all the injected visits with the image injected in the exact same position.

An additional issue I just faced when trying to produce a fresh example is that the command I used to run seems not to be working, no injected image is saved, but I don’t see any error in the log. I’ll try again later to see if I can figure out what happen.

This is the command I have run in the past

` pipetask --long-log --log-file inject.log run --register-dataset-types -b dp02 -i 2.2i/runs/DP0.2,u/alxogm/inject_SL_t0 -o /runs/DP0.2,u/alxogm/inject_SL -p /home/alxogm/WORK/source_injection/pipelines/inject_visit.yaml -d "instrument='LSSTCam-imSim' AND skymap='DC2' AND visit=656444 AND detector=38 AND band='r' "`

This is following documentation in [https://pipelines.lsst.io/v/daily/modules/lsst.source.injection/reference/21\_inject\_synthetic\_sources.html#injecting-postage-stamps](https://pipelines.lsst.io/v/daily/modules/lsst.source.injection/reference/21_inject_synthetic_sources.html#injecting-postage-stamps)  
Thanks for your help.

---

<div class="post-metadata">

**Author:** ![ChristinaAdair](https://sea2.discourse-cdn.com/flex002/user_avatar/www.rubin.community/christinaadair/32/2884_2.png) [@ChristinaAdair](https://www.rubin.community/u/ChristinaAdair)\
**Post date:** [April 17, 2024, 7:05pm UTC](https://www.rubin.community/t/synthetic-source-injection-on-multiple-visits/8480/2 "2024-04-17T19:05:17Z")

</div>

Just wanted to let you know that a demo is being prepared, but in the meantime, anyone else can join the conversation!  
Thanks!

---

<div class="post-metadata">

**Author:** ![alxogm](https://sea2.discourse-cdn.com/flex002/user_avatar/www.rubin.community/alxogm/32/1751_2.png) [@alxogm](https://www.rubin.community/u/alxogm)\
**Post date:** [April 25, 2024, 4:10pm UTC](https://www.rubin.community/t/synthetic-source-injection-on-multiple-visits/8480/3 "2024-04-25T16:10:15Z")

</div>

Hi @ChristinaAdair is there a repository where I can see a preliminary version or in development of this demos? Thanks!

---

<div class="post-metadata">

**Author:** ![ChristinaAdair](https://sea2.discourse-cdn.com/flex002/user_avatar/www.rubin.community/christinaadair/32/2884_2.png) [@ChristinaAdair](https://www.rubin.community/u/ChristinaAdair)\
**Post date:** [April 25, 2024, 8:26pm UTC](https://www.rubin.community/t/synthetic-source-injection-on-multiple-visits/8480/4 "2024-04-25T20:26:23Z")

</div>

Hello! I think we will have more information on this soon. I am not sure if there is a repository, but I will ask.

---

<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:** [April 26, 2024, 11:29pm UTC](https://www.rubin.community/t/synthetic-source-injection-on-multiple-visits/8480/5 "2024-04-26T23:29:08Z")

</div>

Hi @alxogm,

I can provide a partial answer for your first question, about how to write the injected exposure back to the butler.

Follow DP0.2 tutorial notebook 14 on synthetic source injection up to and including Section 4.3.2. Then use the `writeable_butler` to put the `injected_exposure` into the butler, using the line below. In this case I’ve used the `INJECTION_CATALOG_COLLECTION` defined in the tutorial for the run name.

```auto
writeable_butler.put(injected_exposure, 'calexp', dataId=dataId_g, run=INJECTION_CATALOG_COLLECTION)

```

Then instantiate a new temporary butler (`tempButler`) that points to the collection where the injected exposure was put. Retrieve it and plot it to prove to yourself it is your image with the sources injected.

```auto
tempButler = Butler(butler_repo, collections=[INJECTION_CATALOG_COLLECTION])
my_calexp = tempButler.get('calexp', dataId_g)
fig = plt.figure()
display = afwDisplay.Display(frame=fig)
display.scale('asinh', 'zscale')
display.mtv(my_calexp.image)
plt.show()
del tempButler

```

**Caveats**  
Although this is a way to write to the butler, I am not sure that this is a path to being able to coadd. In DP0.2 tutorial notebook 9a, it is explained how coadding DP0.2 images has to be done with an older version of the pipelines, because the pipelines have evolved substantially since DP0.2 was processed two years ago. The source injection functionality was only added recently. So at the moment, I don’t _think_ there is a way to [make an injection pipeline](https://pipelines.lsst.io/v/daily/modules/lsst.source.injection/reference/11_make_injection_pipeline.html) that can both inject sources into, and then coadd, DP0.2 images. But this is not the final word! We will keep investigating and report back, on this and about your other reported issues.

---

<div class="post-metadata">

**Author:** ![jeffcarlin](https://sea2.discourse-cdn.com/flex002/user_avatar/www.rubin.community/jeffcarlin/32/491_2.png) [@jeffcarlin](https://www.rubin.community/u/jeffcarlin)\
**Post date:** [April 30, 2024, 4:54pm UTC](https://www.rubin.community/t/synthetic-source-injection-on-multiple-visits/8480/6 "2024-04-30T16:54:12Z")

</div>

Hi Alma - Unfortunately, Melissa’s point in the “Caveats” above is correct. The tools in the pipelines to coadd images currently don’t work with the DP0.2 data products. Thus, even if you are able to write the injected images to a Butler, you cannot use (to my knowledge) the pipelines tools to coadd them. We are exploring whether there are other ways to create coadds from the images, but it may take a while.

Regarding the other part of your question (aligning the images) – if the image you are injecting has a WCS associated with it, the `source_injection` task should inject it with the correct orientation. However, because `calexp` images have different orientations, you will want to “register” (or “warp”) them so that they are oriented in the same direction. There is a notebook in [this DP0.2 repo](https://github.com/rubin-dp0/delegate-contributions-dp02/tree/main/source_injection) called “aligning\_injected\_images.ipynb” that shows how to do this. (That notebook also does a simple “sum” of the resulting images at the end, which is definitely _not_ the scientifically best way to combine the images. It’s just an illustration that they are aligned.)

Hope this is helpful!

---

<div class="post-metadata">

**Author:** ![jeffcarlin](https://sea2.discourse-cdn.com/flex002/user_avatar/www.rubin.community/jeffcarlin/32/491_2.png) [@jeffcarlin](https://www.rubin.community/u/jeffcarlin)\
**Post date:** [April 30, 2024, 5:13pm UTC](https://www.rubin.community/t/synthetic-source-injection-on-multiple-visits/8480/7 "2024-04-30T17:13:40Z")

</div>

Also, regarding writing injected images back to the Butler – @lskelvin added a new section to the FAQ for the `source_injection` package that explains how to do this. See [this link](https://pipelines.lsst.io/v/d_2024_04_29/modules/lsst.source.injection/faqs/index.html#is-it-possible-to-write-injected-exposures-back-into-a-data-butler) for details.

---

<div class="post-metadata">

**Author:** ![alxogm](https://sea2.discourse-cdn.com/flex002/user_avatar/www.rubin.community/alxogm/32/1751_2.png) [@alxogm](https://www.rubin.community/u/alxogm)\
**Post date:** [April 30, 2024, 7:15pm UTC](https://www.rubin.community/t/synthetic-source-injection-on-multiple-visits/8480/8 "2024-04-30T19:15:15Z")

</div>

Thanks @MelissaGraham and @jeffcarlin , yes I am aware of the caveats, but it’s ok for now. I will try your recommendations and see if I can achieve my goal.

Best,  
Alma

---

<div class="post-metadata">

**Author:** ![lskelvin](https://sea2.discourse-cdn.com/flex002/user_avatar/www.rubin.community/lskelvin/32/2537_2.png) [@lskelvin](https://www.rubin.community/u/lskelvin)\
**Post date:** [May 7, 2024, 6:12pm UTC](https://www.rubin.community/t/synthetic-source-injection-on-multiple-visits/8480/9 "2024-05-07T18:12:52Z")

</div>

Hi @alxogm, I just wanted to follow up here to ask if there’s anything else we can do to help?

As @jeffcarlin mentioned above, we now have a [new section on our FAQs page which addresses how best to ingest injected data products into a data butler repository](https://pipelines.lsst.io/v/daily/modules/lsst.source.injection/faqs/index.html#is-it-possible-to-write-injected-exposures-back-into-a-data-butler) if you wish to continue onward processing. As @MelissaGraham outlined above, there are other tools available which will allow you to ingest data into the butler from a Python/notebook environment as well, however, it may be easier and perhaps safer for you to call on the **SimplePipelineExecutor** to call a pipeline YAML directly. That will ensure that all output data products from the task get correctly registered with the correct dimensions.

With regards points 2 and 3 from your original post, and just to re-iterate what has already been said here, synthetic sources that you inject should be rotated/sheared with respect to the WCS attached to the image. E.g., the injection of a Sérsic source with a given position angle “`beta`” should always be sheared with respect to North.

I’m very keen to hear of your progress, so please do reach out if you need any extra assistance on this.

---

<div class="post-metadata">

**Author:** ![lskelvin](https://sea2.discourse-cdn.com/flex002/user_avatar/www.rubin.community/lskelvin/32/2537_2.png) [@lskelvin](https://www.rubin.community/u/lskelvin)\
**Post date:** [May 7, 2024, 6:17pm UTC](https://www.rubin.community/t/synthetic-source-injection-on-multiple-visits/8480/10 "2024-05-07T18:17:27Z")

</div>

As a brief example, here I’ve injected a simple single Sérsic into two different simulated LSSTCam-imSim detectors which overlap the same pointing (visit 204708, detector 38, and visit 211100, detector 174, both in the `i`-band). The Sérsic source is the large extended source in the ~center of the frame:

 ![lsstcam-imsim-injection](https://us1.discourse-cdn.com/flex002/uploads/rubin/original/2X/1/1884963975229b8787ba4c384303fffc932a8039.jpeg)

These two detectors have boresight rotation angles of 120 and 215, respectively. As you can see, the applied shear appears to be being applied with respect to North, producing a consistently injected source on the sky.
