Skip to content

Commit 6642114

Browse files
Léonard Micheletleonarf
authored andcommitted
des codes erreurs lors du calcul des indicateurs
1 parent d49a3a5 commit 6642114

5 files changed

Lines changed: 142 additions & 33 deletions

File tree

enthic/ontology.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3158,3 +3158,23 @@ def read_account():
31583158
"function": "compute_data_availability",
31593159
},
31603160
}
3161+
3162+
SCORE_ERROR = {
3163+
-1000: "Montant des salaires incohérent",
3164+
-1100: "Montant des salaires non fournis",
3165+
-1200: "Montant des salaires à zéro",
3166+
-1300: "Montant des salaires négatif",
3167+
-2000: "Montant des cotisations sociales incohérent",
3168+
-2100: "Montant des cotisations sociales non fournis",
3169+
-2200: "Montant des cotisations sociales à zéro",
3170+
-2300: "Montant des cotisations sociales négatif",
3171+
-3100: "Montant des impôts sur les bénéfices non fournis",
3172+
-3200: "Montant des impôts sur les bénéfices négatif",
3173+
-4100: "Effectifs non fournis",
3174+
-4200: "Effectifs à zéro",
3175+
-4300: "Effectifs négatif",
3176+
-5100: "Bénéfice attribué non négligeable",
3177+
-6100: "Montant des charges d’exploitation non fournis",
3178+
-6200: "Montant des charges d’exploitation à zéro",
3179+
-6300: "Montant des charges d’exploitation négatif",
3180+
}

enthic/scoring/compute_stats.py

Lines changed: 55 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ def recursive_fill_tree(tree_item, raw_data):
3838
break
3939

4040

41+
# Accepted amount of difference when checking parent value against it's children
42+
relative_error = 0.008
43+
absolute_error = 10
44+
45+
4146
def check_tree_data(tree_item):
4247
"""
4348
Check and complete data in the given tree_item
@@ -68,10 +73,6 @@ def check_tree_data(tree_item):
6873
for child_name in tree_item["children"]:
6974
check_tree_data(tree_item["children"][child_name])
7075

71-
# Accepted amount of difference when checking parent value against it's children
72-
relative_error = 0.005
73-
absolute_error = 10
74-
7576
# Compute ourself tree_item's value from its children
7677
computed_sum = 0 # Result from official children's value
7778
computed_sum_from_computed = 0 # Result from computed children's value
@@ -122,14 +123,15 @@ def check_tree_data(tree_item):
122123
child_data["value"] = 0
123124

124125
computed_sum = computed_sum_from_computed
125-
# If there is only on value missing from children, set this child's value equal to the computed difference
126+
# If there is only one value missing from children, set this child's value equal to the computed difference
126127
elif child_missing_count == 1:
127128
for child_name in tree_item["children"]:
128129
child_data = tree_item["children"][child_name]["data"]
129130
if math.isnan(child_data["value"]):
130-
child_data["computedValue"] = (value - computed_sum) / child["sign"]
131-
child_data["value"] = child_data["computedValue"]
132-
child_data["status"] = "computed"
131+
doubleCheckComputedValueWithChildren(
132+
tree_item["children"][child_name],
133+
(value - computed_sum) / child["sign"],
134+
)
133135
tree_item["data"]["status"] = "checked"
134136
break
135137

@@ -139,6 +141,7 @@ def check_tree_data(tree_item):
139141
or abs(computed_sum_without_sign - value) < absolute_error
140142
):
141143
tree_item["data"]["status"] = "checked"
144+
tree_item["data"]["computedValue"] = computed_sum_without_sign
142145
# Fix children sign and/or set to zero missing values if any
143146
for child_name in tree_item["children"]:
144147
flip_sign(tree_item["children"][child_name])
@@ -148,10 +151,42 @@ def check_tree_data(tree_item):
148151
else:
149152
tree_item["data"]["status"] = "error"
150153

151-
if computed_sum != tree_item["data"]["value"]:
154+
if (
155+
computed_sum != tree_item["data"]["value"]
156+
and "computedValue" not in tree_item["data"]
157+
):
152158
tree_item["data"]["computedValue"] = computed_sum_from_computed
153159

154160

161+
def computeChildrenSum(item):
162+
computedSum = 0
163+
for childName in item["children"]:
164+
child = item["children"][childName]
165+
if "value" in child["data"]:
166+
computedSum += child["data"]["value"] * child["sign"]
167+
return computedSum
168+
169+
170+
def areOpposite(value, reference):
171+
"""
172+
Check if given value is equal to minus reference
173+
"""
174+
result = value + reference
175+
if abs(result / reference) < relative_error or abs(result) < absolute_error:
176+
return True
177+
return False
178+
179+
180+
def doubleCheckComputedValueWithChildren(item, computedValue):
181+
item["data"]["value"] = computedValue
182+
item["data"]["status"] = "computed"
183+
if "children" in item:
184+
computedSum = computeChildrenSum(item)
185+
if areOpposite(computedSum, item["data"]["value"]):
186+
item["data"]["value"] = -item["data"]["value"]
187+
flip_sign(item)
188+
189+
155190
def set_to_zero_computed(tree_item):
156191
"""
157192
set value of given item and it's children to zero when it hasn't another value
@@ -178,12 +213,23 @@ def flip_sign(item):
178213
if item["sign"] == -1:
179214
item["data"]["value"] = -item["data"]["value"]
180215
item["data"]["status"] = "signFlipped"
216+
if "children" in item:
217+
computedSum = computeChildrenSum(item)
218+
if not areOpposite(computedSum, item["data"]["value"]):
219+
return
220+
for child_name in item["children"]:
221+
child = item["children"][child_name]
222+
child["data"]["value"] = -child["data"]["value"]
223+
child["data"]["status"] = "signFlipped"
181224

182225

183226
def gather_data_to_compute(tree, raw):
184227
root = tree["children"]
185228
resultat_avant_impot = root["ResultatAvantImpot"]["children"]
186229
result = {
230+
"benefice_attribue": resultat_avant_impot["BenefAttribueOuPerteTransferee"][
231+
"data"
232+
]["value"],
187233
"participation": root["ParticipationSalariesAuxResultats"]["data"]["value"],
188234
"impot": root["ImpotsSurLesBenefices"]["data"]["value"],
189235
"resultat_exceptionnel": root["ResultatExceptionnel"]["data"]["value"],

enthic/scoring/scoring_functions.py

Lines changed: 65 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,12 @@ def compute_exploitation_share(data):
1414
resultat_exploitation = data["resultat_exploitation"]
1515

1616
# If any values unknown, cannot compute score
17+
if isnan(impot):
18+
return -3100
19+
if impot < 0:
20+
return -3200
1721
if (
18-
any(isnan(value) for value in [impot, resultat_exploitation])
22+
any(isnan(value) for value in [resultat_exploitation])
1923
or resultat_exploitation < 0
2024
):
2125
return float("nan")
@@ -41,19 +45,24 @@ def compute_overall_wages_weight(data):
4145
salaires = data["salaires"]
4246
charges = data["charges"]
4347

44-
if (
45-
isnan(charges)
46-
or charges == 0
47-
or (isnan(salaires) and isnan(cotisations_sociales))
48-
):
48+
if isnan(charges):
49+
return -6100
50+
if charges == 0:
51+
return -6200
52+
if isnan(salaires) and isnan(cotisations_sociales):
4953
return float("nan")
5054

5155
if isnan(salaires):
5256
salaires = 0
5357
elif isnan(cotisations_sociales):
5458
cotisations_sociales = 0
5559

56-
return (salaires + cotisations_sociales) / charges
60+
result = (salaires + cotisations_sociales) / charges
61+
62+
if result < 0 and charges < 0:
63+
return -6300
64+
65+
return result
5766

5867

5968
def compute_wage_quality(data):
@@ -66,15 +75,28 @@ def compute_wage_quality(data):
6675
cotisations_sociales = data["cotisations_sociales"]
6776
salaires = data["salaires"]
6877

69-
if (
70-
isnan(salaires)
71-
or salaires == 0
72-
or isnan(cotisations_sociales)
73-
or cotisations_sociales == 0
74-
):
75-
return float("nan")
76-
77-
return cotisations_sociales / salaires
78+
if isnan(salaires):
79+
return -1100
80+
elif isnan(cotisations_sociales):
81+
return -2100
82+
elif salaires == 0:
83+
return -1200
84+
elif cotisations_sociales == 0:
85+
return -2200
86+
87+
ratio = cotisations_sociales / salaires
88+
if ratio > 1000:
89+
if cotisations_sociales < data["charges_exploitation"]:
90+
ratio = -1000
91+
if cotisations_sociales > data["charges_exploitation"]:
92+
ratio = -2000
93+
94+
if ratio < 0:
95+
if salaires < 0:
96+
return -1300
97+
if cotisations_sociales < 0:
98+
return -2300
99+
return ratio
78100

79101

80102
def compute_average_wage(data):
@@ -87,8 +109,16 @@ def compute_average_wage(data):
87109
salaires = data["salaires"]
88110
effectifs = data["effectifs"]
89111

90-
if isnan(effectifs) or effectifs == 0 or isnan(salaires):
91-
return float("nan")
112+
if isnan(effectifs):
113+
return -4100
114+
if effectifs == 0:
115+
return -4200
116+
if effectifs < 0:
117+
return -4300
118+
if isnan(salaires):
119+
return -1100
120+
if salaires < 0:
121+
return -1300
92122

93123
return salaires / effectifs
94124

@@ -105,27 +135,34 @@ def compute_profit_sharing(data):
105135
resultat_exceptionnel = data["resultat_exceptionnel"]
106136
resultat_financier = data["resultat_financier"]
107137
resultat_exploitation = data["resultat_exploitation"]
138+
benefice_attribue = data["benefice_attribue"]
108139

140+
if isnan(impot):
141+
return -3100
142+
if impot < 0:
143+
return -3200
109144
if any(
110145
isnan(value)
111146
for value in [
112-
impot,
113147
resultat_financier,
114148
resultat_exceptionnel,
115149
resultat_exploitation,
116150
]
117151
):
118152
return float("nan")
119153

120-
if (resultat_financier + resultat_exploitation + resultat_exceptionnel) == 0:
154+
somme_resultats = resultat_financier + resultat_exploitation + resultat_exceptionnel
155+
if somme_resultats == 0:
121156
return float("nan")
122157

123158
if isnan(participation):
124159
participation = 0
125160

126-
return (participation + impot) / (
127-
resultat_financier + resultat_exploitation + resultat_exceptionnel
128-
)
161+
result = (participation + impot) / somme_resultats
162+
if result < 0 and not isnan(benefice_attribue) and benefice_attribue > 0:
163+
return -5100
164+
165+
return result
129166

130167

131168
def compute_exploitation_part(data):
@@ -159,14 +196,18 @@ def compute_exploitation_part(data):
159196
) == 0:
160197
return float("nan")
161198

162-
return (data["produits_exploitation"] + data["charges_exploitation"]) / (
199+
result = (data["produits_exploitation"] + data["charges_exploitation"]) / (
163200
data["produits_exploitation"]
164201
+ data["charges_exploitation"]
165202
+ data["produits_exceptionnel"]
166203
+ data["charges_exceptionnel"]
167204
+ data["produits_financier"]
168205
+ data["charges_financier"]
169206
)
207+
if result < 0:
208+
if data["charges_exploitation"] < 0:
209+
return -6300
210+
return result
170211

171212

172213
def compute_data_availability(data):

references/naf_transition.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
"07.41G": "70.22Z",
2323
"07.22Z": "62.01Z",
2424
"05.24L": "47.54Z",
25+
"05.701": "46.61Z",
2526
"00.12A": "01.41Z",
2627
"01.75G": "13.96Z",
2728
"05.15L": "46.75Z",

requirements/base.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@ lxml
1313
python-dotenv
1414
pandas
1515
requests
16+
pyarrow

0 commit comments

Comments
 (0)