<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Example of taking 'views' from simulated populations
Step1:  Get the mutations that are segregating in each population
Step2:  Look at the raw data in the first element of each list
Step3:  Let's make that nicer, and convert each list of dictionaries to a Pandas DataFrame object
Step4:  The columns are
Step5:  We can also take views of gametes
Step6:  The format is really ugly. v Each gamete is a dict with two elements
Step7:  OK, let's clean that up.  We'll focus on the selected mutations for each individual, and turn everything into a pd.DataFrame.
We're only going to do this for the first simulated population.
Step8:  We now have a list of lists stored in 'smuts'.
Step9:  That's much better.  We can use the index to figure out which individual has which mutations, and their effect sizes, etc.
Finally, we can also take views of diploids.  Let's get the first two diploids in each population
Step10:  Again, the format here is ugly.  Each diploid view is a dictionary
<ASSISTANT_TASK:>
Python Code:
from __future__ import print_function
import fwdpy as fp
import pandas as pd
from background_selection_setup import *
Explanation: Example of taking 'views' from simulated populations
End of explanation
mutations = [fp.view_mutations(i) for i in pops]
Explanation: Get the mutations that are segregating in each population:
End of explanation
for i in mutations:
    print(i[0])
Explanation: Look at the raw data in the first element of each list:
End of explanation
mutations2 = [pd.DataFrame(i) for i in mutations]
for i in mutations2:
    print(i.head())
Explanation: Let's make that nicer, and convert each list of dictionaries to a Pandas DataFrame object:
End of explanation
nmuts = [i[i.neutral == True] for i in mutations2]
for i in nmuts:
    print(i.head())
Explanation: The columns are:
g = the generation when the mutation first arose
h = the dominance
n = the number of copies of the mutation in the population.  You can use this to get its frequency.
neutral = a boolean
pos = the position of the mutation
s = selection coefficient/effect size
label = The label assigned to a mutation.  These labels can be associated with Regions and Sregions.  Here, 1 is a mutation from the neutral region, 2 a selected mutation from the 'left' region and 3 a selected mutation from the 'right' regin.
We can do all the usual subsetting, etc., using regular pandas tricks.  For example, let's get the neutral mutations for each population:
End of explanation
gametes = [fp.view_gametes(i) for i in pops]
Explanation: We can also take views of gametes:
End of explanation
for i in gametes:
    print(i[0])
Explanation: The format is really ugly. v Each gamete is a dict with two elements:
'neutral' is a list of mutations not affecting fitness.  The format is the same as for the mutation views above.
'selected' is a list of mutations that do affect fitness. The format is the same as for the mutation views above.
End of explanation
smuts = [i['selected'] for i in gametes[0]]
Explanation: OK, let's clean that up.  We'll focus on the selected mutations for each individual, and turn everything into a pd.DataFrame.
We're only going to do this for the first simulated population.
End of explanation
smutsdf = pd.DataFrame()
ind=0
##Add the non-empty individuals to the df
for i in smuts:
    if len(i)>0:
        smutsdf = pd.concat([smutsdf,pd.DataFrame(i,index=[ind]*len(i))])
    ind += 1
smutsdf.head()
Explanation: We now have a list of lists stored in 'smuts'.
End of explanation
dips = [fp.view_diploids(i,[0,1]) for i in pops]
Explanation: That's much better.  We can use the index to figure out which individual has which mutations, and their effect sizes, etc.
Finally, we can also take views of diploids.  Let's get the first two diploids in each population:
End of explanation
for key in dips[0][0]:
    print(key)
Explanation: Again, the format here is ugly.  Each diploid view is a dictionary:
End of explanation
<END_TASK>