Results: Give access profiles to np table and keep profile global id.

opt-result
Pierre-Antoine 2026-06-19 14:54:26 +02:00
parent 8255fc3c1b
commit 9c66021b53
3 changed files with 38 additions and 9 deletions

View File

@ -22,6 +22,8 @@ import numpy as np
from copy import deepcopy
from datetime import datetime
from tools import timer
from Model.Scenario import Scenario
from Model.Tools.PamhyrDB import SQLSubModel
from Model.Results.River.River import River
@ -167,7 +169,7 @@ class Results(SQLSubModel):
self._name = name
self._study = study
self._river = River(self._study)
self._river = River(self._study, self)
self._solver = solver
self._repertory = repertory
@ -195,6 +197,7 @@ class Results(SQLSubModel):
def study(self):
return self._study
@timer
def set(self, key, value):
self._meta_data[key] = value
@ -314,6 +317,7 @@ class Results(SQLSubModel):
new_results.set("timestamps", ts)
data["timestamps"] = sorted(ts)
data["parent"] = new_results
new_results._river = River._db_load(execute, data)
new_results.set(

View File

@ -31,15 +31,17 @@ logger = logging.getLogger()
class Profile(SQLSubModel):
def __init__(self, profile, study):
def __init__(self, profile, study, parent):
super(Profile, self).__init__(
id=-1, status=study.status,
owner_scenario=study.status.scenario.id
)
self._parent = parent
self._study = study
self._profile = profile # Source profile in the study
self._data = {} # Dict of dict {<ts>: {<key>: <value>, ...}, ...}
self._global_index = -1
def __len__(self):
return len(self._data)
@ -56,6 +58,15 @@ class Profile(SQLSubModel):
def geometry(self):
return self._profile
@property
def global_index(self):
return self._global_index
@global_index.setter
def global_index(self, id):
self._global_index = id
@timer
def set(self, timestamp, key, data):
if timestamp not in self._data:
self._data[timestamp] = {}
@ -129,6 +140,7 @@ class Profile(SQLSubModel):
new = {}
status = data['status']
parent = data['parent']
study = data['study']
reach = data['reach']
profile = data['profile']
@ -156,7 +168,7 @@ class Profile(SQLSubModel):
owner_scenario = next(it)
if profile not in new:
new_data = cls(profile, study)
new_data = cls(profile, study, parent)
new[profile] = new_data
else:
new_data = new[profile]
@ -269,19 +281,20 @@ class Profile(SQLSubModel):
class Reach(SQLSubModel):
_sub_classes = [Profile]
def __init__(self, reach, study, with_init=True):
def __init__(self, reach, study, parent, with_init=True):
super(Reach, self).__init__(
id=-1, status=study.status,
owner_scenario=study.status.scenario.id
)
self._parent = parent
self._study = study
self._reach = reach # Source reach in the study
self._profiles = []
if with_init:
self._profiles = list(
map(
lambda p: Profile(p, self._study),
lambda p: Profile(p, self._study, self._parent),
reach.profiles
)
)
@ -319,6 +332,11 @@ class Reach(SQLSubModel):
def profile(self, id):
return self._profiles[id]
def set_global_index(self, indexs):
for profile, ind in zip(self._profiles, indexs):
profile.global_index = ind
@timer
def set(self, profile_id, timestamp, key, data):
self._profiles[profile_id].set(timestamp, key, data)
@ -347,7 +365,8 @@ class Reach(SQLSubModel):
reach = data["reach"]
new_reach = cls(
data["reach"], data["study"], with_init=False
data["reach"], data["study"], data["parent"],
with_init=False
)
for i, profile in enumerate(reach.profiles):
@ -366,13 +385,14 @@ class Reach(SQLSubModel):
class River(SQLSubModel):
_sub_classes = [Reach]
def __init__(self, study):
def __init__(self, study, parent):
super(River, self).__init__(
id=-1, status=study.status,
owner_scenario=study.status.scenario.id
)
self._study = study
self._parent = parent
# Dict with timestamps as key
self._reachs = []
@ -393,7 +413,7 @@ class River(SQLSubModel):
def add(self, reach_id):
reachs = self._study.river.enable_edges()
new = Reach(reachs[reach_id].reach, self._study)
new = Reach(reachs[reach_id].reach, self._study, self._parent)
self._reachs.append(new)
return new
@ -417,7 +437,9 @@ class River(SQLSubModel):
@classmethod
def _db_load(cls, execute, data=None):
study = data["study"]
new_river = cls(study)
parent = data["parent"]
new_river = cls(study, parent)
for reach in study.river.reachs():
data["reach"] = reach.reach

View File

@ -1098,6 +1098,9 @@ class Mage8(Mage):
i1 = data[2*i] - 1
i2 = data[2*i+1] - 1
# Keep profiles index of all river
r.set_global_index(range(i1, i2 + 1))
# Add profile id correspondance to reach
for key in range(i1, i2 + 1):
iprofiles.append(i)