From 97d4497485cabf7aa8cfff954201abf67edae9f8 Mon Sep 17 00:00:00 2001
From: Lionel GUEZ <guez@lmd.ens.fr>
Date: Tue, 26 Apr 2022 17:23:38 +0200
Subject: [PATCH] Do not compute properties of isolated segments

---
 cost_function.py | 130 +++++++++++++++++++++++++----------------------
 1 file changed, 68 insertions(+), 62 deletions(-)

diff --git a/cost_function.py b/cost_function.py
index ab15b403..cd5b4a56 100755
--- a/cost_function.py
+++ b/cost_function.py
@@ -139,68 +139,74 @@ num_of_days_to_avg = 7 # number of days to average
 print("Iterating on vertices...")
 
 for n in g.vertices():
-    segment = g.vp.inst_eddies[n]
-    num_of_days = len(segment)
-
-    # Calculate the date index, the eddy index and the SHPC index of
-    # the first instantaneous eddy in the segment, then grab the
-    # position of the extremum and store it in a vertex property:
-    date_index, eddy_index = report_graph.node_to_date_eddy(segment[0],
-                                                            e_overestim)
-    i_SHPC = get_SHPC(array_d_init, date_index)
-    ishape = util_eddies.comp_ishape(handlers[i_SHPC], date_index, eddy_index)
-    g.vp.pos_first[n] = handlers[i_SHPC]["readers"]["extremum"].shape(ishape)\
-                                                    .points[0] # [in degrees]
-
-    # Same for last instantaneous eddy in the segment:
-    date_index, eddy_index = report_graph.node_to_date_eddy(segment[-1],
-                                                            e_overestim)
-    i_SHPC = get_SHPC(array_d_init, date_index)
-    ishape = util_eddies.comp_ishape(handlers[i_SHPC], date_index, eddy_index)
-    g.vp.pos_last[n] = handlers[i_SHPC]["readers"]["extremum"].shape(ishape)\
-                                                    .points[0] # [in degrees]
-
-    if (num_of_days > num_of_days_to_avg):
-        # The segment is longer than the number of days over which to average
-
-        # First 7 days calculation
-        first_res = calculate_radii_rossby(segment[:num_of_days_to_avg],
-                                           e_overestim, handlers, array_d_init)
-
-        # Average and assign the first radii:
-        g.vp.first_av_rad[n] = first_res['radii']
-
-        if first_res['rossby'] is not None:
-            # Average and assign the rossbies:
-            g.vp.first_av_ros[n] = first_res['rossby']
-
-        # Last 7 days calculation:
-        last_res = calculate_radii_rossby(segment[- num_of_days_to_avg:],
-                                          e_overestim, handlers, array_d_init)
-
-        # Average and assign the last radii
-        g.vp.last_av_rad[n] = last_res['radii']
-
-        if last_res['rossby'] is not None:
-            # Average and assign the rossbies:
-            g.vp.last_av_ros[n] = last_res['rossby']
-    else:
-        # The number of eddies in a segment is lower than the number
-        # of days over which to average. The values will be the same
-        # except for the positions.
-        res = calculate_radii_rossby(segment, e_overestim, handlers,
-                                     array_d_init)
-
-        if res['rossby'] is not None:
-            # Average and assign the rossbies:
-            rossby = res['rossby']
-            g.vp.first_av_ros[n] = rossby
-            g.vp.last_av_ros[n] = rossby
-
-        # Average and assign the radii
-        radii = res['radii']
-        g.vp.first_av_rad[n] = radii
-        g.vp.last_av_rad[n] = radii
+    if n.in_degree() != 0 or n.out_degree() != 0:
+        segment = g.vp.inst_eddies[n]
+        num_of_days = len(segment)
+
+        # Calculate the date index, the eddy index and the SHPC index of
+        # the first instantaneous eddy in the segment, then grab the
+        # position of the extremum and store it in a vertex property:
+        date_index, eddy_index = report_graph.node_to_date_eddy(segment[0],
+                                                                e_overestim)
+        i_SHPC = get_SHPC(array_d_init, date_index)
+        ishape = util_eddies.comp_ishape(handlers[i_SHPC], date_index,
+                                         eddy_index)
+        g.vp.pos_first[n] = handlers[i_SHPC]["readers"]["extremum"]\
+            .shape(ishape).points[0] # [in degrees]
+
+        # Same for last instantaneous eddy in the segment:
+        date_index, eddy_index = report_graph.node_to_date_eddy(segment[-1],
+                                                                e_overestim)
+        i_SHPC = get_SHPC(array_d_init, date_index)
+        ishape = util_eddies.comp_ishape(handlers[i_SHPC], date_index,
+                                         eddy_index)
+        g.vp.pos_last[n] = handlers[i_SHPC]["readers"]["extremum"]\
+            .shape(ishape).points[0] # [in degrees]
+
+        if (num_of_days > num_of_days_to_avg):
+            # The segment is longer than the number of days over which
+            # to average
+
+            # First 7 days calculation
+            first_res = calculate_radii_rossby(segment[:num_of_days_to_avg],
+                                               e_overestim, handlers,
+                                               array_d_init)
+
+            # Average and assign the first radii:
+            g.vp.first_av_rad[n] = first_res['radii']
+
+            if first_res['rossby'] is not None:
+                # Average and assign the rossbies:
+                g.vp.first_av_ros[n] = first_res['rossby']
+
+            # Last 7 days calculation:
+            last_res = calculate_radii_rossby(segment[- num_of_days_to_avg:],
+                                              e_overestim, handlers,
+                                              array_d_init)
+
+            # Average and assign the last radii
+            g.vp.last_av_rad[n] = last_res['radii']
+
+            if last_res['rossby'] is not None:
+                # Average and assign the rossbies:
+                g.vp.last_av_ros[n] = last_res['rossby']
+        else:
+            # The number of eddies in a segment is lower than the number
+            # of days over which to average. The values will be the same
+            # except for the positions.
+            res = calculate_radii_rossby(segment, e_overestim, handlers,
+                                         array_d_init)
+
+            if res['rossby'] is not None:
+                # Average and assign the rossbies:
+                rossby = res['rossby']
+                g.vp.first_av_ros[n] = rossby
+                g.vp.last_av_ros[n] = rossby
+
+            # Average and assign the radii
+            radii = res['radii']
+            g.vp.first_av_rad[n] = radii
+            g.vp.last_av_rad[n] = radii
 
 t1 = time.perf_counter()
 timings.write(f"iterating on vertices: {t1 - t0:.0f} s\n")
-- 
GitLab