Skip to content

Commit a7fd059

Browse files
committed
Modification of quick compare to be called in python
1 parent 1f0e2a0 commit a7fd059

File tree

1 file changed

+82
-62
lines changed

1 file changed

+82
-62
lines changed

src/py_eddy_tracker/appli/eddies.py

Lines changed: 82 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,79 @@ def get_twin(j2, j1):
445445
return group1, group2
446446

447447

448+
def run_compare(ref, others, invalid=1, low=20, high=80, intern=False, **kwargs):
449+
groups_ref, groups_other = dict(), dict()
450+
for i, (k, other) in enumerate(others.items()):
451+
print(f"[{i}] {k} -> {len(other)} obs")
452+
gr1, gr2 = get_group(
453+
ref,
454+
other,
455+
*ref.match(other, intern=intern, **kwargs),
456+
invalid=invalid,
457+
low=low,
458+
high=high,
459+
)
460+
groups_ref[k] = gr1
461+
groups_other[k] = gr2
462+
return groups_ref, groups_other
463+
464+
465+
def display_compare(
466+
ref, others, invalid=1, low=20, high=80, area=False, intern=False, **kwargs
467+
):
468+
gr_ref, gr_others = run_compare(
469+
ref, others, invalid=invalid, low=low, high=high, intern=intern, **kwargs
470+
)
471+
472+
def display(value, ref=None):
473+
outs = list()
474+
for v in value:
475+
if ref:
476+
if area:
477+
outs.append(f"{v / ref * 100:.1f}% ({v:.1f}Mkm²)")
478+
else:
479+
outs.append(f"{v/ref * 100:.1f}% ({v})")
480+
else:
481+
outs.append(v)
482+
if area:
483+
return "".join([f"{v:^16}" for v in outs])
484+
else:
485+
return "".join([f"{v:^15}" for v in outs])
486+
487+
def get_values(v, dataset):
488+
if area:
489+
area_ = dataset["speed_area" if intern else "effective_area"]
490+
return [area_[v_].sum() / 1e12 for v_ in v.values()]
491+
else:
492+
return [
493+
v_.sum() if v_.dtype == "bool" else v_.shape[0] for v_ in v.values()
494+
]
495+
496+
labels = dict(
497+
high=f"{high:0.0f} <= high",
498+
low=f"{invalid:0.0f} <= low < {low:0.0f}",
499+
)
500+
501+
keys = [labels.get(key, key) for key in list(gr_ref.values())[0].keys()]
502+
print(" ", display(keys))
503+
if area:
504+
ref_ = ref["speed_area" if intern else "effective_area"].sum() / 1e12
505+
else:
506+
ref_ = len(ref)
507+
for i, v in enumerate(gr_ref.values()):
508+
print(f"[{i:2}] ", display(get_values(v, ref), ref=ref_))
509+
510+
print(" Point of view of study dataset")
511+
print(" ", display(keys))
512+
for i, (k, v) in enumerate(gr_others.items()):
513+
other = others[k]
514+
if area:
515+
ref_ = other["speed_area" if intern else "effective_area"].sum() / 1e12
516+
else:
517+
ref_ = len(other)
518+
print(f"[{i:2}] ", display(get_values(v, other), ref=ref_))
519+
520+
448521
def quick_compare():
449522
parser = EddyParser(
450523
"Tool to have a quick comparison between several identification"
@@ -478,23 +551,17 @@ def quick_compare():
478551

479552
ref = EddiesObservations.load_file(args.ref, **kw)
480553
print(f"[ref] {args.ref} -> {len(ref)} obs")
481-
groups_ref, groups_other = dict(), dict()
482554
others = {other: EddiesObservations.load_file(other, **kw) for other in args.others}
483-
for i, other_ in enumerate(args.others):
484-
other = others[other_]
485-
print(f"[{i}] {other_} -> {len(other)} obs")
486-
gr1, gr2 = get_group(
487-
ref,
488-
other,
489-
*ref.match(other, intern=args.intern, minimal_area=args.minimal_area),
490-
invalid=args.invalid,
491-
low=args.low,
492-
high=args.high,
493-
)
494-
groups_ref[other_] = gr1
495-
groups_other[other_] = gr2
496555

556+
kwargs = dict(
557+
invalid=args.invalid,
558+
low=args.low,
559+
high=args.high,
560+
intern=args.intern,
561+
minimal_area=args.minimal_area,
562+
)
497563
if args.path_out is not None:
564+
groups_ref, groups_other = run_compare(ref, others, **kwargs)
498565
if not exists(args.path_out):
499566
mkdir(args.path_out)
500567
for i, other_ in enumerate(args.others):
@@ -508,51 +575,4 @@ def quick_compare():
508575
basename_ = f"ref_{k}.nc"
509576
ref.index(v).write_file(filename=f"{dirname_}/{basename_}")
510577
return
511-
512-
def display(value, ref=None):
513-
outs = list()
514-
for v in value:
515-
if ref:
516-
if args.area:
517-
outs.append(f"{v / ref * 100:.1f}% ({v:.1f}Mkm²)")
518-
else:
519-
outs.append(f"{v/ref * 100:.1f}% ({v})")
520-
else:
521-
outs.append(v)
522-
if args.area:
523-
return "".join([f"{v:^16}" for v in outs])
524-
else:
525-
return "".join([f"{v:^15}" for v in outs])
526-
527-
def get_values(v, dataset):
528-
if args.area:
529-
area = dataset["speed_area" if args.intern else "effective_area"]
530-
return [area[v_].sum() / 1e12 for v_ in v.values()]
531-
else:
532-
return [
533-
v_.sum() if v_.dtype == "bool" else v_.shape[0] for v_ in v.values()
534-
]
535-
536-
labels = dict(
537-
high=f"{args.high:0.0f} <= high",
538-
low=f"{args.invalid:0.0f} <= low < {args.low:0.0f}",
539-
)
540-
541-
keys = [labels.get(key, key) for key in gr1.keys()]
542-
print(" ", display(keys))
543-
if args.area:
544-
ref_ = ref["speed_area" if args.intern else "effective_area"].sum() / 1e12
545-
else:
546-
ref_ = len(ref)
547-
for i, v in enumerate(groups_ref.values()):
548-
print(f"[{i:2}] ", display(get_values(v, ref), ref=ref_))
549-
550-
print(" Point of view of study dataset")
551-
print(" ", display(keys))
552-
for i, (k, v) in enumerate(groups_other.items()):
553-
other = others[k]
554-
if args.area:
555-
ref_ = other["speed_area" if args.intern else "effective_area"].sum() / 1e12
556-
else:
557-
ref_ = len(other)
558-
print(f"[{i:2}] ", display(get_values(v, other), ref=ref_))
578+
display_compare(ref, others, **kwargs, area=args.area)

0 commit comments

Comments
 (0)