swincas / cookies-n-code

A repo for code review sessions at CAS
http://astronomy.swin.edu.au/
MIT License
31 stars 34 forks source link

Plotting for multiple snapshots for multiple simulations #5

Closed jacobseiler closed 6 years ago

jacobseiler commented 6 years ago

This isn't really an issue more of a "Could I have done it better?". I have multiple simulations, e.g., galaxies_model1 = '/lustre/projects/p004_swin/jseiler/september/galaxies/mysim_IRA_z5.000' galaxies_model2 = '/lustre/projects/p004_swin/jseiler/late_september/galaxies/tiamat_IRA_z1.827' and within each simulation I wish to read in data, perform some calculations (such as the Stellar Mass Function) and then plot at different snapshots (e.g. comparing snapshots [78, 64, 53] with snapshots [73, 61, 40] for each simulation respectively).

My question is then the best way to handle this in Python. I want it to be extendable, so I can plot an arbitrary number of models each with an arbitrary number of snapshots.

My current method (which probably makes Manodeep very disappointed in me) is to abuse nested loops. E.g.,

SMF = [] 
for model_number in range(number_models):
    ## Set up the Arrays ##
    SMF.append([])
    for snapshot_idx in range(len(SnapList[model_number])):
      SMF[model_number].append(np.zeros((NB_gal), dtype = np.int32)) # NB_gal is number of mass bins.
    ## Read in a data file that contains information for a small number of galaxies across all snapshots. ##
    for snapshot_idx in range(len(SnapList[model_number])):
      (counts_local, bin_edges, bin_middle) = AllVars.Calculate_Histogram(mass_gal, bin_width, 0, m_gal_low, m_gal_high) # Bin the Stellar Mass 
      SMF[model_number][snapshot_idx] += counts_local # Update the stellar mass function.

This then produces an array that can be indexed via the model number and the snapshot, SMF[model_number][snapshot_idx], to produce the calculated data. Plotting is then the same process of looping over range(number_models) and SnapList[model_number]. This is useful as well because each simulation may require different normalizations which can be thrown in another nested array.

Any tips on how this complex plotting could be improved?

Cheers

manodeep commented 6 years ago

Think API's. Or functions, if you will...

Can you design an interface where (arbitrary number of) datasets. Or better, groups of datasets...