Advertisement
zavyalov79

Untitled

Mar 20th, 2024
702
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Diff 7.75 KB | None | 0 0
  1. diff --git a/app/models/report/renderer/mci_renderer.rb b/app/models/report/renderer/mci_renderer.rb
  2. index 5b2671070..865696494 100644
  3. --- a/app/models/report/renderer/mci_renderer.rb
  4. +++ b/app/models/report/renderer/mci_renderer.rb
  5. @@ -352,7 +352,7 @@ class Report
  6.          @cog_report = cog_report
  7.          @user = @protocol_session.user
  8.          @comparative_group = comparative_group
  9. -        @trends = Report::Trends::V4.new(@user, @cog_report).trend_values
  10. +        @trends = Report::Trends::V4.new(@user, @cog_report, false).trend_values
  11.        end
  12.  
  13.        def render
  14. diff --git a/app/models/report/trends.rb b/app/models/report/trends.rb
  15. index bbe708029..aeca89e76 100644
  16. --- a/app/models/report/trends.rb
  17. +++ b/app/models/report/trends.rb
  18. @@ -3,10 +3,11 @@
  19.  # rubocop:disable Layout/LineLength
  20.  class Report
  21.    class Trends
  22. -    def initialize(user, batch_session)
  23. +    def initialize(user, batch_session, only_valid_scores = true)
  24.        @user = user
  25.        @batch_session = batch_session
  26.        @score_data = {}
  27. +      @only_valid_scores = only_valid_scores
  28.      end
  29.  
  30.      def trend_values
  31. @@ -40,7 +41,7 @@ class Report
  32.      end
  33.  
  34.      def meaningful_changes(scores)
  35. -      TestScore.meaningful_changes(scores)
  36. +      TestScore.meaningful_changes(scores, scores.last, @only_valid_scores)
  37.                 .slice(:previous_detection_date, :first_detection_date)
  38.                 .compact
  39.                 .transform_values { |date| date.short_date_format }
  40. diff --git a/app/models/report/trends/v4.rb b/app/models/report/trends/v4.rb
  41. index 66dc973c9..1f659b148 100644
  42. --- a/app/models/report/trends/v4.rb
  43. +++ b/app/models/report/trends/v4.rb
  44. @@ -43,7 +43,13 @@ class Report
  45.        end
  46.  
  47.        def base_score(scores)
  48. -        calc_trend_value(scores.sort_by(&:created_at).find(&:valid_score?))
  49. +        values = calc_trend_value(scores.sort_by(&:created_at)
  50. +
  51. +        if @only_valid_scores
  52. +          values.find(&:valid_score?))
  53. +        else
  54. +          values.first
  55. +        end
  56.        end
  57.  
  58.        def prev_score(scores)
  59. @@ -85,6 +91,8 @@ class Report
  60.            adjusted_standard: score_data[:adjusted_standard].to_f.round,
  61.            not_rounded_adjusted_percentile: score_data[:adjusted_percentile].to_f * 100,
  62.            adjusted_percentile: (score_data[:adjusted_percentile].to_f * 100).round,
  63. +          # We do not need to change this value.
  64. +          # MciRenderer can ignore these values. And always consider them positive.
  65.            valid: score.valid_score?,
  66.            created_at: score.created_at
  67.          }
  68. diff --git a/app/models/test_score.rb b/app/models/test_score.rb
  69. index de8cf8bc5..fede31446 100644
  70. --- a/app/models/test_score.rb
  71. +++ b/app/models/test_score.rb
  72. @@ -482,36 +482,37 @@ class TestScore < Score
  73.      Experiment::Healthcare.raw_score(self)
  74.    end
  75.  
  76. -  def self.meaningful_change_detected?(t1_score, t2_score, t1_index, t2_index)
  77. -    negative_meaningful_change_detected?(t1_score, t2_score, t1_index, t2_index) ||
  78. -      positive_meaningful_change_detected?(t1_score, t2_score, t1_index, t2_index)
  79. +  def self.meaningful_change_detected?(t1_score, t2_score, t1_index, t2_index, only_valid = true)
  80. +    negative_meaningful_change_detected?(t1_score, t2_score, t1_index, t2_index, only_valid) ||
  81. +      positive_meaningful_change_detected?(t1_score, t2_score, t1_index, t2_index, only_valid)
  82.    end
  83.  
  84. -  def self.negative_meaningful_change_detected?(t1_score, t2_score, t1_index, t2_index)
  85. +  def self.negative_meaningful_change_detected?(t1_score, t2_score, t1_index, t2_index, only_valid = true)
  86.      # TODO for SAR test meaningful_change are not defined yet
  87.      return false if t1_score.test.name == "SAR"
  88.  
  89. -    change = meaningful_change(t1_score, t2_score, t1_index, t2_index)
  90. +    change = meaningful_change(t1_score, t2_score, t1_index, t2_index, only_valid)
  91.      change[:score_change] < change[:thresholds][:low]
  92.    end
  93.  
  94. -  def self.positive_meaningful_change_detected?(t1_score, t2_score, t1_index, t2_index)
  95. +  def self.positive_meaningful_change_detected?(t1_score, t2_score, t1_index, t2_index, only_valid = true)
  96.      # TODO for SAR test meaningful_change are not defined yet
  97.      return false if t1_score.test.name == "SAR"
  98.  
  99. -    change = meaningful_change(t1_score, t2_score, t1_index, t2_index)
  100. +    change = meaningful_change(t1_score, t2_score, t1_index, t2_index, only_valid)
  101.      change[:score_change] > change[:thresholds][:high]
  102.    end
  103.  
  104.    # When dealing with any score after the 10th recorded score for this user, we treat that
  105.    # score as if it is the 10th. Similarly, for any etalon comparison after the 9th, we treat
  106.    # it as the 9th.
  107. -  def self.meaningful_change(t1_score, t2_score, t1_index, t2_index)
  108. +  def self.meaningful_change(t1_score, t2_score, t1_index, t2_index, only_valid = false)
  109.      t1_index = 9 if t1_index > 9
  110.      t2_index = 10 if t2_index > 10
  111.      raise ArgumentError.new("t2_index must be more than t1_index") if t2_index <= t1_index
  112.      raise ArgumentError.new("t1_score and t2_score must have the same subclass") if t1_score.subclass != t2_score.subclass
  113. -    return false if !t1_score.valid_score? || !t2_score.valid_score?
  114. +
  115. +    return false if (!t1_score.valid_score? || !t2_score.valid_score?) && only_valid
  116.  
  117.      meaningful_changes = t2_score.test.meaningful_changes
  118.      score_change       = t2_score.raw_score - t1_score.raw_score
  119. @@ -574,18 +575,18 @@ class TestScore < Score
  120.      end
  121.    end
  122.  
  123. -  def self.meaningful_changes(scores, current_score = scores.last)
  124. -    return no_meaningful_changes unless current_score.valid_score?
  125. +  def self.meaningful_changes(scores, current_score = scores.last, only_valid = true)
  126. +    return no_meaningful_changes if only_valid && !current_score.valid_score?
  127.  
  128. -    valid_scores = scores.select(&:valid_score?)
  129. +    valid_scores = only_valid ? scores.select(&:valid_score?) : scores
  130.      current_score_index = valid_scores.index{ |score| score.id == current_score.id }
  131.  
  132.      return no_meaningful_changes if valid_scores.count < 2 || current_score_index.nil?
  133.  
  134.      current_score_index += 1
  135.  
  136. -    first_mc_data(current_score, current_score_index, valid_scores)
  137. -      .merge!(previous_mc_data(current_score, current_score_index, valid_scores))
  138. +    first_mc_data(current_score, current_score_index, valid_scores, only_valid)
  139. +      .merge!(previous_mc_data(current_score, current_score_index, valid_scores, only_valid))
  140.    end
  141.  
  142.    private
  143. @@ -610,12 +611,12 @@ class TestScore < Score
  144.        previous_detection_positive: false }
  145.    end
  146.  
  147. -  def self.first_mc_data(current_score, current_score_index, valid_scores)
  148. +  def self.first_mc_data(current_score, current_score_index, valid_scores, only_valid = true)
  149.      first_score = valid_scores.first
  150.  
  151.      return no_first_mc if first_score.nil?
  152.  
  153. -    mc_params = [first_score, current_score, 1, current_score_index]
  154. +    mc_params = [first_score, current_score, 1, current_score_index, only_valid]
  155.      mc_detected = TestScore.meaningful_change_detected?(*mc_params)
  156.      if mc_detected
  157.        { first_detection_date: first_score.created_at,
  158. @@ -629,12 +630,12 @@ class TestScore < Score
  159.      end
  160.    end
  161.  
  162. -  def self.previous_mc_data(current_score, current_score_index, valid_scores)
  163. +  def self.previous_mc_data(current_score, current_score_index, valid_scores, only_valid = false)
  164.      previous_score = valid_scores[-2] if valid_scores.count > 2
  165.  
  166.      return no_previous_mc if previous_score.nil?
  167.  
  168. -    mc_params = [previous_score, current_score, current_score_index - 1, current_score_index]
  169. +    mc_params = [previous_score, current_score, current_score_index - 1, current_score_index, only_valid]
  170.      mc_detected = TestScore.meaningful_change_detected?(*mc_params)
  171.      if mc_detected
  172.        { previous_detection_date: previous_score.created_at,
  173.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement