update_33.py 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. #!/usr/bin/env python3
  2. # Contest Management System - http://cms-dev.github.io/
  3. # Copyright © 2018 Luca Wehrstedt <luca.wehrstedt@gmail.com
  4. #
  5. # This program is free software: you can redistribute it and/or modify
  6. # it under the terms of the GNU Affero General Public License as
  7. # published by the Free Software Foundation, either version 3 of the
  8. # License, or (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU Affero General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU Affero General Public License
  16. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. """A class to update a dump created by CMS.
  18. Used by DumpImporter and DumpUpdater.
  19. This updater changes the score details so that they provide the score as
  20. a fraction of the max score rather than in absolute terms.
  21. """
  22. import logging
  23. logger = logging.getLogger(__name__)
  24. # The fields that a subtask always has.
  25. SUBTASK_PARTIAL_KEYS = {"idx", "testcases"}
  26. # The fields it has when all its testcases are public or when viewed
  27. # with "elevated privileges" (i.e., as a contestant having played a
  28. # token or as an admin).
  29. SUBTASK_FULL_KEYS = SUBTASK_PARTIAL_KEYS | {"score", "max_score"}
  30. # The fields that a testcase always has.
  31. TESTCASE_PARTIAL_KEYS = {"idx"}
  32. # The fields it has when it is public or when viewed with "elevated
  33. # privileges" (i.e., as a contestant having played a token or as an
  34. # admin).
  35. TESTCASE_FULL_KEYS = \
  36. TESTCASE_PARTIAL_KEYS | {"outcome", "text", "time", "memory"}
  37. # The possible "public outcomes" of a testcase (i.e., human-readable
  38. # descriptions of the numerical outcome).
  39. OUTCOMES = {"Correct", "Not correct", "Partially correct"}
  40. def is_number(v):
  41. return isinstance(v, (int, float))
  42. def is_group_score_details(details):
  43. """Return whether the details were produced by a Group* score type.
  44. details (object): the (possibly public) score details of a
  45. submission result.
  46. return (bool): the answer to the question "do they look like they
  47. were generated by a Group* score type?".
  48. """
  49. if not isinstance(details, list):
  50. return False
  51. for subtask in details:
  52. if not isinstance(subtask, dict):
  53. return False
  54. keys = set(subtask.keys())
  55. # Whether the subtask exposes all its information, which
  56. # requires all its testcases to do the same.
  57. subtask_is_visible = False
  58. if keys == SUBTASK_FULL_KEYS:
  59. if not is_number(subtask["score"]) \
  60. or not is_number(subtask["max_score"]):
  61. return False
  62. subtask_is_visible = True
  63. elif keys != SUBTASK_PARTIAL_KEYS:
  64. return False
  65. if not isinstance(subtask["idx"], int) \
  66. or not isinstance(subtask["testcases"], list):
  67. return False
  68. for testcase in subtask["testcases"]:
  69. if not isinstance(testcase, dict):
  70. return False
  71. keys = set(testcase.keys())
  72. if keys == TESTCASE_FULL_KEYS:
  73. if testcase["outcome"] not in OUTCOMES \
  74. or not isinstance(testcase["text"], list) \
  75. or not all(isinstance(s, str)
  76. for s in testcase["text"]) \
  77. or not (testcase["time"] is None
  78. or is_number(testcase["time"])) \
  79. or not (testcase["memory"] is None
  80. or is_number(testcase["memory"])):
  81. return False
  82. elif keys != TESTCASE_PARTIAL_KEYS or subtask_is_visible:
  83. return False
  84. if not isinstance(testcase["idx"], str):
  85. return False
  86. return True
  87. def convert_score_details(details):
  88. """Convert the details' subtasks from scores to score_fractions.
  89. If the given (possibly public) score details came from a Group*
  90. score type, modify them in-place so that the subtasks, which used
  91. to store the score as an "absolute" value, end up storing it as a
  92. fraction of the maximum score.
  93. details (object): the (possibly public) score details of a
  94. submission result.
  95. return (bool): whether the conversion required an educated guess
  96. which might not have been perfectly accurate, for example
  97. because the source data came from a custom Group* score type
  98. (not from a builtin one), or used partial scores, negative
  99. scores or scores larger than one.
  100. """
  101. inaccurate = False
  102. if not is_group_score_details(details):
  103. return inaccurate
  104. for subtask in details:
  105. if "score" not in subtask:
  106. continue
  107. if subtask["max_score"] == 0:
  108. # Assuming all outcomes are between 0 and 1, all builtin
  109. # Group* score types have the property that the fraction is
  110. # 1 iff all public outcomes are correct and is 0 iff any
  111. # public outcome is not correct.
  112. if all(testcase["outcome"] == "Correct"
  113. for testcase in subtask["testcases"]):
  114. subtask["score_fraction"] = 1.0
  115. elif any(testcase["outcome"] == "Not correct"
  116. for testcase in subtask["testcases"]):
  117. subtask["score_fraction"] = 0.0
  118. else:
  119. # Any value strictly between 0 and 1 would work here.
  120. subtask["score_fraction"] = 0.5
  121. # Warn the admin about our guesswork.
  122. inaccurate = True
  123. else:
  124. subtask["score_fraction"] = subtask["score"] / subtask["max_score"]
  125. del subtask["score"]
  126. return inaccurate
  127. class Updater:
  128. def __init__(self, data):
  129. assert data["_version"] == 32
  130. self.objs = data
  131. def run(self):
  132. # See convert_score_details.
  133. inaccurate = False
  134. for k, v in self.objs.items():
  135. if k.startswith("_"):
  136. continue
  137. if v["_class"] == "SubmissionResult":
  138. # We could perform the conversion based only on the
  139. # dataset's current score type but it isn't necessarily
  140. # the one that generated the score details, hence this
  141. # could lead to false positive and negatives. So instead
  142. # we convert only if it really looks like the details
  143. # came from a group score type.
  144. if convert_score_details(v["score_details"]):
  145. inaccurate = True
  146. if convert_score_details(v["public_score_details"]):
  147. inaccurate = True
  148. if inaccurate:
  149. logger.info("Some subtasks have a maximum score of zero. They used "
  150. "to be reported as successful even if some of their "
  151. "testcases failed. This has now been fixed but the old "
  152. "data may not have been accurately converted. You are "
  153. "advised to check manually or to rescore all affected "
  154. "submissions to be safe.")
  155. return self.objs