<!-- 
RSS generated by JIRA (8.3.4#803005-sha1:1f96e09b3c60279a408a2ae47be3c745f571388b) at Sat Feb 10 16:03:21 JST 2024

It is possible to restrict the fields that are returned in this document by specifying the 'field' parameter in your request.
For example, to request only the issue key and summary append 'field=key&field=summary' to the URL of your request.
-->
<rss version="0.92" >
<channel>
    <title>PFS-JIRA</title>
    <link>https://pfspipe.ipmu.jp/jira</link>
    <description>This file is an XML representation of an issue</description>
    <language>en-us</language>    <build-info>
        <version>8.3.4</version>
        <build-number>803005</build-number>
        <build-date>13-09-2019</build-date>
    </build-info>


<item>
            <title>[PIPE2D-1145] Make fitPfsFluxReference.py faster</title>
                <link>https://pfspipe.ipmu.jp/jira/browse/PIPE2D-1145</link>
                <project id="10002" key="PIPE2D">DRP 2-D Pipeline</project>
                    <description>&lt;p&gt;The current &lt;tt&gt;fitPfsFluxReference.py&lt;/tt&gt; is slow.&lt;br/&gt;
It takes a few hours to process a single visit.&lt;/p&gt;

&lt;p&gt;We are going to aim at 1 hour/visit for now,&lt;br/&gt;
though we are not sure whether it is acceptable or not.&lt;/p&gt;

&lt;p&gt;Here is the output of the profiler profiling the processing of the integration test.&lt;/p&gt;
&lt;div class=&quot;code panel&quot; style=&quot;border-width: 1px;&quot;&gt;&lt;div class=&quot;codeContent panelContent&quot;&gt;
&lt;pre class=&quot;code-java&quot;&gt;
ncalls  tottime  percall  cumtime  percall filename:lineno(function)
     1    0.000    0.000 1981.366 1981.366 cmdLineTask.py:621(parseAndRun)
 12088    1.538    0.000 1360.010    0.113 fitPfsFluxReference.py:427(computeContinuum)
 48326    0.515    0.000  318.018    0.007 fitPfsFluxReference.py:981(convolveLsf)
&lt;/pre&gt;
&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;(Because the integration test contains two visits, we have to divide these things by 2&lt;br/&gt;
to get per-visit values. We then have to multiply them by 10 because &quot;6k&quot; model set is&lt;br/&gt;
used in the integration test, whereas &quot;60k&quot; model set is used in the actual data processing.)&lt;/p&gt;

&lt;p&gt;The two hot spots are:&lt;/p&gt;

&lt;ul&gt;
	&lt;li&gt;computeContinuum (Fit a continuum to a model spectrum)&lt;/li&gt;
	&lt;li&gt;convolveLsf (Convolve a model spectrum with an LSF)&lt;/li&gt;
&lt;/ul&gt;


&lt;p&gt;We already have a mechanism to skip these two calls when they are unnecessary:&lt;br/&gt;
&quot;If &lt;tt&gt;prior&lt;span class=&quot;error&quot;&gt;&amp;#91;model&amp;#93;&lt;/span&gt; / max(prior) &amp;lt;= th&lt;/tt&gt;, then skip computing &lt;tt&gt;likelihood&lt;span class=&quot;error&quot;&gt;&amp;#91;model&amp;#93;&lt;/span&gt;&lt;/tt&gt;.&quot; (th = 1e-8)&lt;br/&gt;
The &lt;tt&gt;prior&lt;/tt&gt; probability distribution is computed from broad-band fluxes.&lt;br/&gt;
Since the integration test contains only a single broad-band flux (i2_hsc),&lt;br/&gt;
the prior is the uniform distribution. Therefore this mechanism does not work&lt;br/&gt;
in the integration test, but it indeed appears to lead to time saving&lt;br/&gt;
in the actual data processing.&lt;/p&gt;

&lt;p&gt;We have 85 prior probability distributions as --debug by-products&lt;br/&gt;
obtained from processing visit=82596. Using these distributions,&lt;br/&gt;
we can count how many calls to the two functions will happen if&lt;br/&gt;
we set the threshold &lt;tt&gt;th&lt;/tt&gt; to various values.&lt;br/&gt;
We can then speculate, from the profiler output, execution time&lt;br/&gt;
of &lt;tt&gt;fitPfsFluxReference&lt;/tt&gt; as a function of the threshold.&lt;/p&gt;

&lt;p&gt;&lt;span class=&quot;image-wrap&quot; style=&quot;&quot;&gt;&lt;img src=&quot;https://pfspipe.ipmu.jp/jira/secure/attachment/15495/15495_time_threshold.png&quot; style=&quot;border: 0px solid black&quot; /&gt;&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;We can see from this plot that we have to set &lt;tt&gt;th=0.01&lt;/tt&gt; or above&lt;br/&gt;
if we want the per-visit execution time to be less than an hour.&lt;/p&gt;

&lt;p&gt;One concern is that the model that would be chosen were it not&lt;br/&gt;
for the threshold can be discarded too hastily if we set the threshold&lt;br/&gt;
to such a large value.&lt;br/&gt;
We can examine whether a FLUXSTD fiber will be affected by the threshold,&lt;br/&gt;
by seeing &lt;tt&gt;prior&lt;span class=&quot;error&quot;&gt;&amp;#91;argmax(posterior)&amp;#93;&lt;/span&gt; / max(prior)&lt;/tt&gt;.&lt;br/&gt;
(The posterior distributions are also --debug by-products obtained from&lt;br/&gt;
processing visit=82596.)&lt;br/&gt;
If &lt;tt&gt;prior&lt;span class=&quot;error&quot;&gt;&amp;#91;argmax(posterior)&amp;#93;&lt;/span&gt; / max(prior)&lt;/tt&gt; is less than the threshold,&lt;br/&gt;
the best model (&lt;tt&gt;argmax(posterior)&lt;/tt&gt;) won&apos;t be selected when we set&lt;br/&gt;
&lt;tt&gt;th&lt;/tt&gt; to the threshold value.&lt;/p&gt;

&lt;p&gt;&lt;span class=&quot;image-wrap&quot; style=&quot;&quot;&gt;&lt;img src=&quot;https://pfspipe.ipmu.jp/jira/secure/attachment/15496/15496_affected_fluxstd.png&quot; style=&quot;border: 0px solid black&quot; /&gt;&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;It appears that the samples below 0.01 are outliers, which can be neglected.&lt;br/&gt;
(More than 40% of FLUXSTD fibers are outliers for now, but it is another problem.)&lt;/p&gt;

&lt;p&gt;In conclusion, we will:&lt;/p&gt;

&lt;ul&gt;
	&lt;li&gt;Modify the program to make the threshold a config parameter.&lt;/li&gt;
	&lt;li&gt;Change the default threshold from 1e-8 to 0.01.&lt;/li&gt;
&lt;/ul&gt;
</description>
                <environment></environment>
        <key id="23207">PIPE2D-1145</key>
            <summary>Make fitPfsFluxReference.py faster</summary>
                <type id="3" iconUrl="https://pfspipe.ipmu.jp/jira/secure/viewavatar?size=xsmall&amp;avatarId=10518&amp;avatarType=issuetype">Task</type>
                                            <priority id="10000" iconUrl="https://pfspipe.ipmu.jp/jira/images/icons/priorities/medium.svg">Normal</priority>
                        <status id="10002" iconUrl="https://pfspipe.ipmu.jp/jira/images/icons/statuses/generic.png" description="The issue is resolved, reviewed, and merged">Done</status>
                    <statusCategory id="3" key="done" colorName="green"/>
                                    <resolution id="10000">Done</resolution>
                                        <assignee username="sogo.mineo">sogo.mineo</assignee>
                                    <reporter username="sogo.mineo">sogo.mineo</reporter>
                        <labels>
                            <label>flux-calibration</label>
                    </labels>
                <created>Tue, 17 Jan 2023 23:50:31 +0000</created>
                <updated>Thu, 27 Apr 2023 03:36:13 +0000</updated>
                            <resolved>Fri, 27 Jan 2023 01:00:57 +0000</resolved>
                                                                        <due></due>
                            <votes>0</votes>
                                    <watches>4</watches>
                                                                <comments>
                            <comment id="32104" author="sogo.mineo" created="Thu, 19 Jan 2023 03:30:46 +0000"  >&lt;p&gt;I copied Yamashita-san&apos;s files and ran &lt;tt&gt;fitPfsFluxReference.py&lt;/tt&gt; on them to get the actual profile of&#160;&lt;tt&gt;fitPfsFluxReference.py&lt;/tt&gt; running on real data. I set the threshold to 0.01.&lt;/p&gt;
&lt;div class=&quot;code panel&quot; style=&quot;border-width: 1px;&quot;&gt;&lt;div class=&quot;codeContent panelContent&quot;&gt;
&lt;pre class=&quot;code-java&quot;&gt;
   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        1    0.000    0.000 10937.974 10937.974 cmdLineTask.py:621(parseAndRun)
    28655    5.009    0.000 4212.784    0.147 fitPfsFluxReference.py:435(computeContinuum)
    57141   18.758    0.000 3688.403    0.065 fluxModelSet.py:120(readSpectrum)
  4849760 1056.633    0.000 1644.092    0.000 pfsFiberArraySet.py:366(extractFiber)
    87918    1.490    0.000  806.743    0.009 fitPfsFluxReference.py:989(convolveLsf)
&lt;/pre&gt;
&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;It still took three hours. &lt;tt&gt;convolveLsf&lt;/tt&gt; is no longer problematic with threshold=0.01, for its cumtime was much less than that of &lt;tt&gt;extractFiber&lt;/tt&gt;.&lt;/p&gt;

&lt;p&gt;A new hot spot is &lt;tt&gt;readSpectrum&lt;/tt&gt;. This is a function to read a model template from the disk storage. I have yet to find why &lt;tt&gt;readSpectrum&lt;/tt&gt; is called twice as many times as &lt;tt&gt;computeContinuum&lt;/tt&gt;.&lt;/p&gt;</comment>
                            <comment id="32107" author="sogo.mineo" created="Fri, 20 Jan 2023 00:26:11 +0000"  >&lt;p&gt;I reduced the number of calls to &lt;tt&gt;readSpectrum&lt;/tt&gt; and &lt;tt&gt;extractFiber&lt;/tt&gt;:&lt;/p&gt;
&lt;div class=&quot;code panel&quot; style=&quot;border-width: 1px;&quot;&gt;&lt;div class=&quot;codeContent panelContent&quot;&gt;
&lt;pre class=&quot;code-java&quot;&gt;
   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        1    0.000    0.000 8893.141 8893.141 cmdLineTask.py:621(parseAndRun)
    28655    4.785    0.000 4416.048    0.154 fitPfsFluxReference.py:435(computeContinuum)
    28655   11.147    0.000 3055.535    0.107 fluxModelSet.py:120(readSpectrum)
    87918    1.432    0.000  874.030    0.010 fitPfsFluxReference.py:995(convolveLsf)
      170    0.086    0.001    0.114    0.001 pfsFiberArraySet.py:366(extractFiber)
&lt;/pre&gt;
&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;Cumtime percall of &lt;tt&gt;readSpectrum&lt;/tt&gt; increased because the storage was in heavy use during the profiling.&lt;br/&gt;
 If the storage had been at leisure, the total execution time would have been 2 hours.&lt;br/&gt;
 I have to reduce it by another hour.&lt;/p&gt;

&lt;p&gt;&lt;tt&gt;computeContinuum&lt;/tt&gt; was called 28655 times because 28655 flux templates out of 57141&lt;br/&gt;
 were possible candidates for any of the 85 FLUXSTD.&lt;br/&gt;
 I think I can do without &lt;tt&gt;computeContinuum&lt;/tt&gt; by pre-computing continua&lt;br/&gt;
 of all the flux templates with several typical Gaussian LSF, saving them in files,&lt;br/&gt;
 and reading the files instead of calling &lt;tt&gt;computeContinuum&lt;/tt&gt;. &lt;br/&gt;
 It may seem hopeless because &lt;tt&gt;readSpectrum&lt;/tt&gt; (file read) took almost the same time as&lt;br/&gt;
 &lt;tt&gt;computeContinuum&lt;/tt&gt;, but we should recall that the storage was in heavy use.&lt;/p&gt;

&lt;p&gt;Another idea is to reduce the interpolation points in the model parameter space.&lt;br/&gt;
 If the number of flux templates is made half, execution time of &lt;tt&gt;fitPfsFluxReference&lt;/tt&gt;&lt;br/&gt;
 will be half.&lt;/p&gt;</comment>
                            <comment id="32113" author="sogo.mineo" created="Tue, 24 Jan 2023 05:39:10 +0000"  >&lt;p&gt;Yamashita-san speaks against sacrificing any accuracy for the sake of speed at this point. Since output does not change but speed increases by the commits in this ticket branch, I want to merge the ticket branch before closing this ticket (in &quot;Won&apos;t Fix&quot;?)&lt;/p&gt;</comment>
                            <comment id="32114" author="takuji.yamashita" created="Tue, 24 Jan 2023 06:59:53 +0000"  >&lt;p&gt;Because, at this point, we do not have any distinct requirements for the processing time, I think we do not need to reduce the processing time so far as affecting accuracy. In this ticket, we have the option of the cutoff threshold and the speed increases without changing outputs as Mineo-san said. I agree to merge this ticket.&#160;&lt;/p&gt;</comment>
                            <comment id="32128" author="sogo.mineo" created="Thu, 26 Jan 2023 00:57:03 +0000"  >&lt;p&gt;To make extra sure, I restored the threshold to 1e-8, keeping the other changes, to take profile again:&lt;/p&gt;
&lt;div class=&quot;code panel&quot; style=&quot;border-width: 1px;&quot;&gt;&lt;div class=&quot;codeContent panelContent&quot;&gt;
&lt;pre class=&quot;code-java&quot;&gt;
ncalls  tottime  percall  cumtime  percall filename:lineno(function)
     1    0.000    0.000 14380.434 14380.434 cmdLineTask.py:621(parseAndRun)
 45006    7.391    0.000 6854.570    0.152 fitPfsFluxReference.py:436(computeContinuum)
 45006   18.192    0.000 4610.827    0.102 fluxModelSet.py:120(readSpectrum)
241737    3.420    0.000 2070.246    0.009 fitPfsFluxReference.py:996(convolveLsf)
&lt;/pre&gt;
&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;Compared to the case of threshold 1e-2, the total execution time was 1.6 times longer.&lt;br/&gt;
 The threshold as high as 1e-2 proved inevitable.&lt;/p&gt;

&lt;p&gt;I&apos;m making a pull request.&lt;/p&gt;</comment>
                            <comment id="32129" author="sogo.mineo" created="Thu, 26 Jan 2023 06:19:05 +0000"  >&lt;p&gt;Could you review this PR? The execution time is almost made half, though the original aim (execution time &amp;lt;~ 1 hour/visit) has not been accomplished. I cannot reduce the execution time any further without sacrificing accuracy. I don&apos;t know which resolution should be made to this issue, &quot;Done&quot; or &quot;Won&apos;t fix&quot;; but I want to merge the branch to master anyway.&lt;/p&gt;</comment>
                            <comment id="32135" author="sogo.mineo" created="Fri, 27 Jan 2023 01:00:57 +0000"  >&lt;p&gt;Merged. Thank you for the review. Let me close this issue in &quot;Done&quot;.&lt;/p&gt;</comment>
                    </comments>
                <issuelinks>
                            <issuelinktype id="10003">
                    <name>Relates</name>
                                            <outwardlinks description="relates to">
                                        <issuelink>
            <issuekey id="23395">PIPE2D-1208</issuekey>
        </issuelink>
                            </outwardlinks>
                                                        </issuelinktype>
                    </issuelinks>
                <attachments>
                            <attachment id="15496" name="affected_fluxstd.png" size="50205" author="sogo.mineo" created="Tue, 17 Jan 2023 23:47:17 +0000"/>
                            <attachment id="15495" name="time_threshold.png" size="107900" author="sogo.mineo" created="Tue, 17 Jan 2023 23:47:17 +0000"/>
                    </attachments>
                <subtasks>
                    </subtasks>
                <customfields>
                                                <customfield id="customfield_10500" key="com.atlassian.jira.plugins.jira-development-integration-plugin:devsummary">
                        <customfieldname>Development</customfieldname>
                        <customfieldvalues>
                            
                        </customfieldvalues>
                    </customfield>
                                                                                                                                                                                                            <customfield id="customfield_10010" key="com.pyxis.greenhopper.jira:gh-lexo-rank">
                        <customfieldname>Rank</customfieldname>
                        <customfieldvalues>
                            <customfieldvalue>0|zzsvzj:</customfieldvalue>

                        </customfieldvalues>
                    </customfield>
                                                                                            <customfield id="customfield_10100" key="com.atlassian.jira.plugin.system.customfieldtypes:userpicker">
                        <customfieldname>Reviewers</customfieldname>
                        <customfieldvalues>
                            <customfieldvalue>price</customfieldvalue>

                        </customfieldvalues>
                    </customfield>
                                                                                                                                                                                </customfields>
    </item>
</channel>
</rss>