Skip to content

Commit

Permalink
should be good to go with #19
Browse files Browse the repository at this point in the history
  • Loading branch information
pernak18 committed Jan 18, 2023
1 parent 7b5a87d commit 5d9d20c
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 53 deletions.
64 changes: 17 additions & 47 deletions band-g-point-reduction.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -536,10 +536,7 @@
"source": [
"Now that one iteration with modified combining is complete, we can proceed similarly to more iterations. This has the potentially to be **very** time consuming, even parallelized on a supercomputer (because of I/O? Python parallelization overhead? unsure why...), so from here on out, we'll try to use the _k_-distribution and cost optimization objects we've already taken the time to generate and only replace trials for the band that contained the winner in the previous iteration. While we're doing this only with modified combining, there is no reason this logic cannot be applied to iterations where the modified combining is not applied.\n",
"\n",
"Spitballing how to do this:\n",
"- `coObjMod` trial lists -- determine indices of winner band, `costFuncComp` on only newly modified trials in said band (should only be one batch), then insert results into appropriate `coObjMod` lists at appropriate indices\n",
"- need a `setupNextIter` analog that doesn't reset everything (`setupNextIterMod`)\n",
"- `fluxInputsAll` might be the most important thing to alter -- that decreases in size by 1 and we need to find all of the guys with `optBand` as their `bandID`; then `costComp0`, `dCostComps0`, `dCost0`, `cost0`, `winnerCost`, `dCost`, `totalCost`, and `costComps` have to be setup, but differently than in `setupNextIter` because we don't wanna reset and rather perserve most of what's in them"
"While code snippets that follow work, they are poorly organized. In the future, one task may be to streamline the snippets a little more."
]
},
{
Expand All @@ -561,9 +558,9 @@
"# print(i, fia['bandID'], coObjMod.optBand, coObj.totalCost[i], coObj.dCost[i], coObjMod.totalCost[i], coObjMod.dCost[i], len(coObjMod.fluxInputsAll[i].keys()))\n",
"\n",
"# also compare initial modified trial costs to subsequent winners for verification\n",
"# metric = np.abs(coObjMod.totalCost-coObj.winnerCost)\n",
"# iSort = np.argsort(metric)\n",
"# for iisort, isort in enumerate(iSort): print(isort, coObjMod.fluxInputsAll[isort]['bandID']+1, metric[isort])"
"metric = np.abs(coObjMod.totalCost-coObj.winnerCost)\n",
"iSort = np.argsort(metric)\n",
"for iisort, isort in enumerate(iSort): print(isort, coObjMod.fluxInputsAll[isort]['bandID']+1, metric[isort])"
]
},
{
Expand All @@ -584,7 +581,6 @@
"for sWgt in ['plus', '2plus']:\n",
" objModCP = DCP(coObjMod)\n",
" bandStr = 'band{:02d}'.format(coObjMod.optBand+1)\n",
" print(bandStr, sWgt)\n",
" kBandDict[sWgt][bandStr], kFiles[sWgt] = MODRED.kModSetupNextIter(\n",
" objModCP, iniWgt, scaleWeight=sWgt)\n",
" coObjMod.distBands[bandStr] = kBandDict[sWgt][bandStr]\n",
Expand All @@ -600,54 +596,28 @@
"metadata": {},
"outputs": [],
"source": [
"for i in range(coObjMod.iCombine, coObjMod.iCombine+2):#NITER+1\n",
"for i in tqdm(range(coObjNew.iCombine, coObjMod.iCombine+12)):#NITER+1\n",
" print('Iteration {}'.format(i))\n",
"\n",
" iBandTrials, bandObj = MODRED.doBandTrials(coObjNew, kFiles)\n",
" coObjNew = MODRED.modOptimal(coObjNew, bandObj, iBandTrials, coObjMod.winnerCost)\n",
"\n",
" # kFiles = {}\n",
" kFiles = {}\n",
" # coObjNew.iCombine = coObjMod.iCombine\n",
" # for sWgt in ['plus', '2plus']:\n",
" # objModCP = DCP(coObjMod)\n",
" # bandStr = 'band{:02d}'.format(coObjMod.optBand+1)\n",
" # print(bandStr, sWgt)\n",
" # kBandDict[sWgt][bandStr], kFiles[sWgt] = MODRED.kModSetupNextIter(\n",
" # objModCP, iniWgt, scaleWeight=sWgt)\n",
" # coObjMod.distBands[bandStr] = kBandDict[sWgt][bandStr]\n",
" # # end scaleWeight loop\n",
" break\n",
"\n",
" coObjNew = MODRED.coModSetupNextIter(coObjMod)\n",
" for sWgt in ['plus', '2plus']:\n",
" objModCP = DCP(coObjNew)\n",
" bandStr = 'band{:02d}'.format(coObjNew.optBand+1)\n",
" print(sWgt, bandStr)\n",
" kBandDict[sWgt][bandStr], kFiles[sWgt] = MODRED.kModSetupNextIter(\n",
" objModCP, iniWgt, scaleWeight=sWgt)\n",
" coObjMod.distBands[bandStr] = kBandDict[sWgt][bandStr]\n",
" # end scaleWeight loop\n",
"\n",
" coObj0 = DCP(coObjNew)\n",
" coObjNew = MODRED.coModSetupNextIter(coObj0)\n",
"# end combination loop"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# at this point, cost0 and dCost0 for coObjMod (mid-iter 95) are of length 93 an 92\n",
"# whereas coJbj at iter 94 (full-iter) is 94 and 93, so i forgot to append somewhere, i think\n",
"# then i have to append the winners\n",
"\n",
"# for comp, cost0 in coObjMod.cost0.items():\n",
"# print(comp)\n",
"# print(len(coObjMod.cost0[comp]), len(coObjMod.dCost0[comp]))\n",
"# # print(len(bandObj.costComps[comp]))\n",
"# # print(len(bandObj.dCostComps[comp]))\n",
"# # print(len(bandObj.costComp0[comp]))\n",
"# # print(len(bandObj.dCostComps0[comp]))\n",
"# # end comp loop\n",
"\n",
"# also have to replace the winner cost:\n",
"# print(coObj.winnerCost, coObjMod.winnerCost)\n",
"\n",
"# when those are fixed, next step should be:\n",
"# MODRED.modOptimal(coObjMod, bandObj, iBandTrials, coObjMod.winnerCost)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
Expand Down
21 changes: 15 additions & 6 deletions modified_reduction.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,8 @@ def scaleWeightRegress(dCostMod):
return newScales, cross
# end scaleWeightRegress()

def whereRecompute(kBandAll, coObjMod, trialZero, scales, weight=0.05):
def whereRecompute(kBandAll, coObjMod, trialZero, scales,
weight=0.05, doBand=None):
"""
Determine where new flux and cost computations need to be made
(i.e., at zero crossings), recombine g-points, then replace
Expand All @@ -146,10 +147,13 @@ def whereRecompute(kBandAll, coObjMod, trialZero, scales, weight=0.05):
coObjMod -- gCombine_Cost object, likely output from costModInit
scales -- float array [nTrial], scale factor used with `weight` in
modified g-point combining
doBand -- int, single unit-offset band number of band to process
if not set, all are processed
"""

gCombineAll = []
for iBand, key in enumerate(kBandAll.keys()):
if doBand is not None and iBand != doBand: continue
band = iBand+1
kNC = kBandAll[key].kInNC

Expand Down Expand Up @@ -251,8 +255,6 @@ def modOptimal(coObjMod, coObjRep, iRedo, winnerCost0):
# end comp loop
# end reprocess loop

# at iter 95, these values were 146, 147, and 146, so dCostComps is a problem
for temp in [coObjMod.totalCost, coObjMod.dCostComps['flux_net'], coObjMod.costComps['flux_net']]: print(len(temp))
coObjMod.findOptimal()
coObjMod.costDiagnostics()

Expand Down Expand Up @@ -347,7 +349,10 @@ def coModSetupNextIter(inObj):
outObj.fluxInputsAll.pop(iRm)
outObj.combinedNC.pop(iRm)
outObj.trialNC.pop(iRm)
for comp in outObj.compNameCF: outObj.costComps[comp].pop(iRm)
for comp in outObj.compNameCF:
outObj.costComps[comp].pop(iRm)
outObj.dCostComps[comp].pop(iRm)
# end comp loop

outObj.optBand = None
outObj.optNC = None
Expand Down Expand Up @@ -426,11 +431,15 @@ def doBandTrials(inObj, kFiles, weight=0.05):
dCostMod[key] = np.array(bandObj.totalCost) - inObj.winnerCost
# end key loop

# band should be the same for each trial in bandObj
iBand = bandObj.fluxInputsAll[0]['bandID']

# dCostMod, coObjNew = MODRED.costModInit(coObj0, kBandDict, diagnostics=True)
newScales, cross = scaleWeightRegress(dCostMod)
bandObjMod = whereRecompute(bandObj.distBands, bandObj, cross,
newScales, weight=weight)
bandObjRep = recompute(bandObj, bandObjMod, cross)
newScales, weight=weight, doBand=iBand)
bandObjRep = recompute(bandObj, bandObjMod, np.where(cross)[0])
# print(np.array(bandObj.totalCost) - inObj.winnerCost)

return iNAN, bandObj
# end doBandTrials()

0 comments on commit 5d9d20c

Please sign in to comment.