in sourcecode/scoring/scorer.py [0:0]
def score_final(self, scoringArgs: FinalScoringArgs) -> ModelResult:
"""
Process ratings to assign status to notes and optionally compute rater properties.
Accepts prescoringNoteModelOutput and prescoringRaterModelOutput as args (fields on scoringArgs)
which are the outputs of the prescore() function. These are used to initialize the final scoring.
It filters the prescoring output to only include the rows relevant to this scorer, based on the
c.scorerNameKey field of those dataframes.
"""
torch.set_num_threads(self._threads)
logger.info(
f"score_final: Torch intra-op parallelism for {self.get_name()} set to: {torch.get_num_threads()}"
)
# Filter unfiltered params to just params for this scorer (with copy).
# Avoid editing the dataframe in FinalScoringArgs, which is shared across scorers.
prescoringNoteModelOutput = scoringArgs.prescoringNoteModelOutput[
scoringArgs.prescoringNoteModelOutput[c.scorerNameKey] == self.get_name()
].drop(columns=c.scorerNameKey, inplace=False)
if scoringArgs.prescoringRaterModelOutput is None:
return self._return_empty_final_scores()
prescoringRaterModelOutput = scoringArgs.prescoringRaterModelOutput[
scoringArgs.prescoringRaterModelOutput[c.scorerNameKey] == self.get_name()
].drop(columns=c.scorerNameKey, inplace=False)
if self.get_name() not in scoringArgs.prescoringMetaOutput.metaScorerOutput:
logger.info(
f"Scorer {self.get_name()} not found in prescoringMetaOutput; returning empty scores from final scoring."
)
return self._return_empty_final_scores()
prescoringMetaScorerOutput = scoringArgs.prescoringMetaOutput.metaScorerOutput[self.get_name()]
# Filter raw input
with self.time_block("Filter input"):
ratings, noteStatusHistory = self._filter_input(
scoringArgs.noteTopics,
scoringArgs.ratings,
scoringArgs.noteStatusHistory,
scoringArgs.userEnrollment,
)
# If there are no ratings left after filtering, then return empty dataframes.
if len(ratings) == 0:
logger.info(
f"No rating left after filtering for Scorer {self.get_name()}, returning empty dataframes."
)
return self._return_empty_final_scores()
try:
noteScores, userScores = self._score_notes_and_users(
ratings=ratings,
noteStatusHistory=noteStatusHistory,
prescoringNoteModelOutput=prescoringNoteModelOutput,
prescoringRaterModelOutput=prescoringRaterModelOutput,
prescoringMetaScorerOutput=prescoringMetaScorerOutput,
)
except EmptyRatingException:
logger.info(f"EmptyRatingException for Scorer {self.get_name()}")
return self._return_empty_final_scores()
with self.time_block("Postprocess output"):
# Only some subclasses do any postprocessing.
# E.g. topic models add confidence bit, group models prune according to authorship filter.
noteScores, userScores = self._postprocess_output(
noteScores,
userScores,
scoringArgs.ratings,
scoringArgs.noteStatusHistory,
scoringArgs.userEnrollment,
)
noteScores = noteScores.rename(columns=self._get_note_col_mapping())
userScores = userScores.rename(columns=self._get_user_col_mapping())
# Process noteScores
noteScores = noteScores.drop(columns=self._get_dropped_note_cols())
assert set(noteScores.columns) == set(
self.get_scored_notes_cols() + self.get_auxiliary_note_info_cols()
), f"""all columns must be either dropped or explicitly defined in an output.
Extra columns that were in noteScores: {set(noteScores.columns) - set(self.get_scored_notes_cols() + self.get_auxiliary_note_info_cols())}
Missing expected columns that should've been in noteScores: {set(self.get_scored_notes_cols() + self.get_auxiliary_note_info_cols()) - set(noteScores.columns)}"""
# Process userScores
userScores = userScores.drop(columns=self._get_dropped_user_cols())
assert set(userScores.columns) == set(self.get_helpfulness_scores_cols()), f"""all columns must be either dropped or explicitly defined in an output.
Extra columns that were in userScores: {set(userScores.columns) - set(self.get_helpfulness_scores_cols())}
Missing expected columns that should've been in userScores: {set(self.get_helpfulness_scores_cols()) - set(userScores.columns)}"""
# Return dataframes with specified columns in specified order
return ModelResult(
scoredNotes=noteScores[self.get_scored_notes_cols()],
helpfulnessScores=userScores[self.get_helpfulness_scores_cols()]
if self.get_helpfulness_scores_cols()
else None,
auxiliaryNoteInfo=noteScores[self.get_auxiliary_note_info_cols()]
if self.get_auxiliary_note_info_cols()
else None,
scorerName=self.get_name(),
metaScores=None,
)