Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a moveFitterStep funtion for reordering #25

Merged
merged 2 commits into from
Aug 26, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 35 additions & 2 deletions src/scaffoldfitter/fitter.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,39 @@ def getInitialFitterStepConfig(self):
"""
return self._fitterSteps[0]

def moveFitterStep(self, prevRow, newRow, modelFileNameStem):
"""
Move fitterStep to run after afterFitterStep.
Model is reloaded and no fitter steps after initial config are run.
:param prevRow: Previous index of step needs to be moved.
:param newRow: Index of moved step after moving.
:param modelFileNameStem: File name stem for writing intermediate model files.
:return: True if model is reloaded, False if not.
:return: index of end step.
"""
endStep = self._fitterSteps[0]
for step in self._fitterSteps:
if step.hasRun():
endStep = step
fitterStep = self._fitterSteps[prevRow]
prevBeforeFitterStep = self._fitterSteps[prevRow - 1]
# Switch position
self._fitterSteps.insert(newRow, self._fitterSteps.pop(prevRow))
beforeFitterStep = self._fitterSteps[newRow - 1]
afterFitterStep = self._fitterSteps[newRow + 1] if newRow < len(self._fitterSteps) - 1 else None
isMovingStepRun = fitterStep.hasRun()
isAfterStepRun = afterFitterStep.hasRun() if afterFitterStep else False
isBeforeStepRun = beforeFitterStep.hasRun()
if not isMovingStepRun and not isAfterStepRun:
return False, self._fitterSteps.index(endStep)
elif not isMovingStepRun and isAfterStepRun:
endStep = beforeFitterStep
elif isMovingStepRun and (isAfterStepRun or not isBeforeStepRun) and fitterStep == endStep:
endStep = prevBeforeFitterStep
elif isMovingStepRun and not isAfterStepRun and isBeforeStepRun:
endStep = fitterStep
return self.run(endStep, modelFileNameStem, True), self._fitterSteps.index(endStep)

def getInheritFitterStep(self, refFitterStep: FitterStep):
"""
Get last FitterStep of same type as refFitterStep or None if
Expand Down Expand Up @@ -421,7 +454,7 @@ def _loadData(self):
self._discoverDataCoordinatesField()
self._discoverMarkerGroup()

def run(self, endStep=None, modelFileNameStem=None):
def run(self, endStep=None, modelFileNameStem=None, reorder = False):
"""
Run either all remaining fitter steps or up to specified end step.
:param endStep: Last fitter step to run, or None to run all.
Expand All @@ -432,7 +465,7 @@ def run(self, endStep=None, modelFileNameStem=None):
endStep = self._fitterSteps[-1]
endIndex = self._fitterSteps.index(endStep)
# reload only if necessary
if endStep.hasRun() and (endIndex < (len(self._fitterSteps) - 1)) and self._fitterSteps[endIndex + 1].hasRun():
if endStep.hasRun() and (endIndex < (len(self._fitterSteps) - 1)) and self._fitterSteps[endIndex + 1].hasRun() or reorder:
# re-load to get back to current state
self.load()
for index in range(1, endIndex + 1):
Expand Down