Skip to content

Commit

Permalink
Merge pull request #2 from iml-gddaiss/biometrie_hack
Browse files Browse the repository at this point in the history
Biometrie hack
  • Loading branch information
davidsean authored Sep 11, 2024
2 parents 6cc2587 + 5136c41 commit 7697d7f
Show file tree
Hide file tree
Showing 4 changed files with 367 additions and 22 deletions.
176 changes: 176 additions & 0 deletions andes_migrate/biometrie_petoncle.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@


from andes_migrate.andes_helper import AndesHelper
from andes_migrate.projet_mollusque import ProjetMollusque
from andes_migrate.table_peche_sentinelle import TablePecheSentinelle


class BiometriePetoncle(TablePecheSentinelle):

def __init__(self, andes_db: AndesHelper, proj: ProjetMollusque, collection_name:str, *args, **kwargs):
# super().__init__(*args, **kwargs)
super().__init__(*args, ref=proj.reference_data, **kwargs)

self.andes_db = andes_db
self.proj: ProjetMollusque = proj
self.collection_name:str = collection_name
# self.table_name = "TRAIT_MOLLUSQUE"

# call this form outside
self._init_rows()

def populate_data(self):
"""Populate data: run all getters"""
# secteur trait no taille poids_vif poids_muscle poids_gonade poids_visceres sexe espece comment
if self.collection_name == 'Conserver le spécimen (Biométrie Centre)':
secteur = "Centre"
if self.collection_name == 'Conserver le spécimen (Biométrie Ouest)':
secteur = "Ouest"
# if self.collection_name == 'Conserver un specimen':
# secteur = "16E"
# secteur = "16E"

# print(self.collection_name)
sexe_dict={'0':"I",
'1':'M',
'2':'F',
}
self.data["id_specimen"] = self._get_current_row_pk()
self.data["secteur"] = secteur
self.data["trait"] = self.get_ident_no_trait()
self.data["no"] = self.get_observation("Code Collection coquille").strip().split("-")[-1]
self.data["taille"] = self.get_observation("Longuer (biométrie)").replace(".",",")
self.data["taille (old)"] = self.get_observation("Longueur").replace(".",",")
self.data["poids_vif"] = self.get_observation("Poids vif").replace(".",",")
self.data["poids_muscle"] = self.get_observation("Poids du muscle").replace(".",",")
self.data["poids_gonade"] = self.get_observation("Poids des gonades").replace(".",",")
self.data["poids_visceres"] = self.get_observation("Poids des viscères").replace(".",",")
self.data["poids_gonade"] = self.get_observation("Poids des gonades").replace(".",",")
self.data["sexe"] = self.get_observation("Sexe")
self.data["comment"] = self.get_comment()

def __next__(self):
"""
Increment to focus on next row
"""
# print(self.table_name)
# print(f"{self._row_idx} of {len(self._row_list)}")
# print()
if self._row_idx is not None and self._row_list is not None:
if self._row_idx < len(self._row_list):
# increment first, it'l be adjusted in _get_current_row_pk()
self._row_idx += 1
self.populate_data()

# self.write_row()
return self.data
else:
raise StopIteration
else:
self.logger.error("Row data not initialise, did you run _init_rows()?")
raise ValueError


def _init_rows(self):
"""Initialisation method
This queries the Andes DB and creates a list of row entries to be added to the current table
After running this methods initialises the following attribute:
self._row_list
self._row_idx (hopefully to self._row_idx=0)
self._row_list will be populated with the specimen ids belonging in the collection
self._row_idx will start at 0
"""
query = (
"SELECT specimen_id "
"FROM ecosystem_survey_observation "
"LEFT JOIN ecosystem_survey_specimen "
"ON ecosystem_survey_observation.specimen_id = ecosystem_survey_specimen.id "
"LEFT JOIN ecosystem_survey_basket "
"ON ecosystem_survey_specimen.basket_id = ecosystem_survey_basket.id "
"LEFT JOIN ecosystem_survey_catch "
"ON ecosystem_survey_basket.catch_id=ecosystem_survey_catch.id "
"LEFT JOIN shared_models_set "
"ON shared_models_set.id=ecosystem_survey_catch.set_id "
"LEFT JOIN shared_models_observationtype "
"ON shared_models_observationtype.id=observation_type_id "
f"WHERE shared_models_set.cruise_id = {self.proj._get_current_row_pk()} "
f"AND (shared_models_observationtype.nom ='{self.collection_name}' AND observation_value=1) "
)
result = self.andes_db.execute_query(query)
self._assert_not_empty(result)
self._row_list = [specimen[0] for specimen in result]
self._row_idx = 0

# @validate_int()
# @log_results
def get_ident_no_trait(self) -> int:
"""IDENT_NO_TRAIT INTEGER / NUMBER(5,0)
Numéro séquentiel d'identification du trait
Andes
-----
shared_models.set.set_number
"""
specimen_pk = self._get_current_row_pk()
query = (
"SELECT shared_models_set.set_number "
"FROM ecosystem_survey_specimen "
"LEFT JOIN ecosystem_survey_basket "
"ON ecosystem_survey_specimen.basket_id=ecosystem_survey_basket.id "
"LEFT JOIN ecosystem_survey_catch "
"ON ecosystem_survey_basket.catch_id=ecosystem_survey_catch.id "
"LEFT JOIN shared_models_set "
"ON shared_models_set.id=ecosystem_survey_catch.set_id "
f"WHERE ecosystem_survey_specimen.id={specimen_pk} "
)
result = self.andes_db.execute_query(query)
self._assert_one(result)
to_return = result[0][0]
return to_return


def get_observation(self, name_fr):
specimen_pk = self._get_current_row_pk()
query = (
"SELECT observation_value "
"FROM ecosystem_survey_observation "
"LEFT JOIN shared_models_observationtype "
"ON ecosystem_survey_observation.observation_type_id=shared_models_observationtype.id "
f"WHERE ecosystem_survey_observation.specimen_id={specimen_pk} "
f"AND shared_models_observationtype.nom='{name_fr}' "
)
result = self.andes_db.execute_query(query)
try:
self._assert_one(result)
to_return = result[0][0]
except ValueError:
# print('error:', specimen_pk, name_fr,result)
to_return = None
if to_return is None:
to_return=""
return to_return

def get_comment(self):
specimen_pk = self._get_current_row_pk()
query = (
"SELECT ecosystem_survey_specimen.comment "
"FROM ecosystem_survey_specimen "
f"WHERE ecosystem_survey_specimen.id={specimen_pk} "
)
result = self.andes_db.execute_query(query)
self._assert_one(result)
to_return = result[0][0]
if to_return is None:
to_return=""
to_return.replace("\n", " ")
to_return.replace("\r", " ")
return to_return






3 changes: 2 additions & 1 deletion docs/source/contraintes.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ Doit corréspondre à une entrée de la table `PROJET_MOLLUSQUE` ayant une valeu
Pour les mission pétoncle, un de ces choix:
- `Évaluation de stocks IML - Pétoncle I de M`
- `Évaluation de stocks IML - Pétoncle Minganie`

Pour les missions buccin:
- `Relevé buccin Haute Côte-Nord`

Expand All @@ -43,6 +42,8 @@ Doit corréspondre à une entrée de la table `TRAIT_MOLLUSQUE` ayant une valeur
Pour les mission pétoncle, un de ces choix:
- `Îles-de-la-Madeleine`
- `Côte-Nord`
Pour les missions buccin:
- `Haute Côte-Nord`

Pour les missions buccin:
- `Haute Côte-Nord`
Expand Down
72 changes: 51 additions & 21 deletions run.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import csv
import shutil
import pyodbc
import logging
import logging
from andes_migrate.biometrie_petoncle import BiometriePetoncle

from andes_migrate.capture_mollusque import CaptureMollusque
from andes_migrate.oracle_helper import OracleHelper
Expand All @@ -20,10 +22,10 @@


# INPUT VALUES
no_notification = "IML-2023-011"
zone = "20"
no_notification = "IML-2024-008E"
zone = "16E"
espece = "pétoncle"
SEQ_peche = 151
SEQ_peche = 0

output_fname = f'./{no_notification}.mdb'
shutil.copyfile('andes_migrate/ref_data/access_template.mdb', output_fname)
Expand All @@ -38,28 +40,56 @@
proj = ProjetMollusque(andes_db, output_cur, ref=ref, zone=zone, no_notif=no_notification, espece=espece)



for p in proj:
print(f"Projet: ", p)
trait = TraitMollusque(andes_db, proj, output_cur)
for t in trait:
no_moll = 1
print(f"Trait: ", t)
engin = EnginMollusque(trait, output_cur)
for e in engin:
# print(f"Engin: ", e)
capture = CaptureMollusque(engin, output_cur)
for c in capture:
# print(f"Capture: ", c)

freq = FreqLongMollusque(capture, output_cur, no_moll_init=no_moll)
for f in freq:
# print(f"FreqLong: ", f)
# if (c['COD_ESP_GEN'] == 48 or c['COD_ESP_GEN'] == 50) :
no_moll += 1

collection_name = 'Conserver un specimen'
# observationgroup_name = 'Biometrie 16E extérieur'
biometrie = BiometriePetoncle(andes_db, proj, collection_name, output_cur)
with open('Biometrie_16E.csv','w') as fp:
writer = csv.DictWriter(fp, lineterminator="\n", fieldnames=["id_specimen",
"secteur",
"trait",
"no",
"taille",
"taille (old)",
"poids_vif",
"poids_muscle",
"poids_gonade",
"poids_visceres",
"poids_gonade",
"sexe",
"comment"])
writer.writeheader()
for b in biometrie:
# print(b)
if b is not None:
writer.writerow(b)



exit()
# trait = TraitMollusque(andes_db, proj, output_cur)
# for t in trait:
# no_moll = 1
# print(f"Trait: ", t)
# engin = EnginMollusque(trait, output_cur)
# for e in engin:
# # print(f"Engin: ", e)
# capture = CaptureMollusque(engin, output_cur)
# for c in capture:
# # print(f"Capture: ", c)

# freq = FreqLongMollusque(capture, output_cur, no_moll_init=no_moll)
# for f in freq:
# # print(f"FreqLong: ", f)
# # if (c['COD_ESP_GEN'] == 48 or c['COD_ESP_GEN'] == 50) :
# no_moll += 1


# monolithic commit if no errors are found
output_cur.commit()
# output_cur.commit()



Expand Down
Loading

0 comments on commit 7697d7f

Please sign in to comment.