PLATYPOS issueshttps://gitlab.aip.de/lketzer/platypos/-/issues2020-07-02T15:36:58Zhttps://gitlab.aip.de/lketzer/platypos/-/issues/13Cleaning: evove_forward needs refactoring2020-07-02T15:36:58ZYori Fourniery.fournier@aip.deCleaning: evove_forward needs refactoringThere : [https://gitlab.aip.de/lketzer/platypos/-/blob/master/platypos_package/Planet_class_LoFo14.py#L188](https://gitlab.aip.de/lketzer/platypos/-/blob/master/platypos_package/Planet_class_LoFo14.py#L188)
The issue with this function ...There : [https://gitlab.aip.de/lketzer/platypos/-/blob/master/platypos_package/Planet_class_LoFo14.py#L188](https://gitlab.aip.de/lketzer/platypos/-/blob/master/platypos_package/Planet_class_LoFo14.py#L188)
The issue with this function is that it is a mixture of solving stuff and writing output (and inputs?).
So the name of the method does not tell what it actually does. This function should be just:
```python
# call mass_planet_RK4_forward_LO14 to start the integration
t, M, R, Lx = mass_planet_RK4_forward_LO14(
epsilon=epsilon,
K_on=K_on,
beta_on=beta_on,
planet_object=self,
initial_step_size=initial_step_size,
t_final=t_final,
track_dict=evo_track_dict
)
```
or something like that. All the rest should go into `gen_planet_id`, `prepare_folder`, `write_results_to_file` ...
Another issue I see is that you are using DataFrame like:
```python
df = pd.DataFrame({"Time": t, "Mass": M, "Radius": R, "Lx": Lx})
df.to_csv(path_for_saving+self.planet_id+".txt", index=None)
```
That is fine, even though you could create the DataFrame inside the `mass_planet_RK4_forward_LO14` method.
But why not using the same mechanism later? instead of:
```python
# create another file, which contains the final parameters only
if os.path.exists(path_for_saving+\
self.planet_id+"_final.txt"):
pass
else:
p = open(path_for_saving+self.planet_id+"_final.txt", "w")
# get index of last valid entry in the radius array and access
# its value
index_of_last_entry = df["Radius"][df["Radius"].notna()].index[-1]
R_final = df["Radius"].loc[index_of_last_entry]
index_of_last_entry = df["Mass"][df["Mass"].notna()].index[-1]
M_final = df["Mass"].loc[index_of_last_entry]
f_env_final = ((M_final-self.core_mass)/M_final)*100 # %
planet_params = "a,core_mass,fenv,mass,radius,metallicity,track\n"\
+ str(self.distance) + "," + str(self.core_mass)\
+ "," + str(f_env_final) + "," + str(M_final)\
+ "," + str(R_final) + "," + self.metallicity\
+ "," + self.planet_id
p.write(planet_params)
p.close()
```
Here you are actually doing what `df.to_csv()` does but by hand. That is less efficient and wordy.
and must not `index_of_last_entry` be the same for radius and mass ?Fix all of Yori's comments.Laura KetzerLaura Ketzerhttps://gitlab.aip.de/lketzer/platypos/-/issues/12Cleaning: `with` statement can save your life2020-06-30T09:30:36ZYori Fourniery.fournier@aip.deCleaning: `with` statement can save your lifesee [https://gitlab.aip.de/lketzer/platypos/-/blob/master/platypos_package/Planet_class_LoFo14.py#L251](https://gitlab.aip.de/lketzer/platypos/-/blob/master/platypos_package/Planet_class_LoFo14.py#L251)
```python
s = open(path_for_savin...see [https://gitlab.aip.de/lketzer/platypos/-/blob/master/platypos_package/Planet_class_LoFo14.py#L251](https://gitlab.aip.de/lketzer/platypos/-/blob/master/platypos_package/Planet_class_LoFo14.py#L251)
```python
s = open(path_for_saving+"host_star_properties.txt", "w")
star_params = "star_id,mass_star,radius_star,age,Lbol,Lx_age\n"\
+ self.star_id + "," + str(self.mass_star) + ","\
+ str(self.radius_star) + "," + str(self.age)\
+ "," + str(self.Lbol/const.L_sun.cgs.value)\
+ "," + str(self.Lx_age)
s.write(star_params)
s.close()
```
this is dangerous, because if you get an `Error` in the computation of `star_params` and you catch the Error the code won't close the file before the program stops. That is harmless as long as you have one file, wait to have 100, 1000 of files. All opened together, your laptop won't like it.
To prevent that you there were the old-fashion way (don't do it it is just to understand):
```python
s = open(...)
try:
star_params = compute(...)
s.write(star_params)
except ValueError as exception:
# LOG WARNING, ERROR What ever you want
raise exception
finally:
s.close()
```
This construction guaranty that even if you got an exception in the `compute` function the statement in `finally` will be called. That guaranty that the file will be closed what ever happens.
However this formulation was quite wordy and since python 2.x (what ever version, a long time ago anyway) the python developers added the `with` statement!
```python
with open(...) as s:
# compute
```
is equivalent to:
```python
s = open(...)
try:
s.__enter__()
# compute
except:
# treat exception
finally:
s.__exit__()
```
`s` is a `File` object. `File.__enter__()` does nothing fancy, but `File.__exit__()` is nothing else than `File.close()` so you see much easier and shorter to use `with` and you get a full bench of improvements.Fix all of Yori's comments.Laura KetzerLaura Ketzerhttps://gitlab.aip.de/lketzer/platypos/-/issues/15Cleaning: raise NotImplemented Exception2020-06-30T07:57:05ZYori Fourniery.fournier@aip.deCleaning: raise NotImplemented Exceptionthere : [https://gitlab.aip.de/lketzer/platypos/-/blob/master/platypos_package/Planet_class_LoFo14.py#L312](https://gitlab.aip.de/lketzer/platypos/-/blob/master/platypos_package/Planet_class_LoFo14.py#L312)
```python
def evolve_back...there : [https://gitlab.aip.de/lketzer/platypos/-/blob/master/platypos_package/Planet_class_LoFo14.py#L312](https://gitlab.aip.de/lketzer/platypos/-/blob/master/platypos_package/Planet_class_LoFo14.py#L312)
```python
def evolve_backwards(self, t_final, initial_step_size, epsilon, K_on,
beta_on, evo_track_dict, path_for_saving):
print("Not implemented yet.")
```
You could do
```python
def evolve_backwards(self, t_final, initial_step_size, epsilon, K_on,
beta_on, evo_track_dict, path_for_saving):
raise NotImplementedError("Comming soo... :)")
```
There is an Exception for it.Fix all of Yori's comments.Laura KetzerLaura Ketzerhttps://gitlab.aip.de/lketzer/platypos/-/issues/11Cleaning: too wordy pass2020-06-30T07:51:45ZYori Fourniery.fournier@aip.deCleaning: too wordy passDon't use pass if you don't really need it.
```python
if os.path.exists(path_for_saving+"host_star_properties.txt"):
pass
else:
...
```
This can be done like:
```python
if not os...Don't use pass if you don't really need it.
```python
if os.path.exists(path_for_saving+"host_star_properties.txt"):
pass
else:
...
```
This can be done like:
```python
if not os.path.exists(path_for_saving+"host_star_properties.txt"):
...
```Fix all of Yori's comments.Laura KetzerLaura Ketzerhttps://gitlab.aip.de/lketzer/platypos/-/issues/9BUGGY: Never use "None" instead of None2020-06-30T07:47:30ZYori Fourniery.fournier@aip.deBUGGY: Never use "None" instead of NoneTHere it is: [https://gitlab.aip.de/lketzer/platypos/-/blob/master/platypos_package/Planet_class_LoFo14.py#L62](https://gitlab.aip.de/lketzer/platypos/-/blob/master/platypos_package/Planet_class_LoFo14.py#L62)
just try:
```python
print...THere it is: [https://gitlab.aip.de/lketzer/platypos/-/blob/master/platypos_package/Planet_class_LoFo14.py#L62](https://gitlab.aip.de/lketzer/platypos/-/blob/master/platypos_package/Planet_class_LoFo14.py#L62)
just try:
```python
print("None")
print(None)
```
... You see, it will be a nightmare for debugging
```python
a = "None"
if a is None:
print("Yes it is")
else:
print("No it is not it is {}".format(a))
```
good luck ;)Fix all of Yori's comments.Laura KetzerLaura Ketzer