lundi 23 septembre 2019

Need to make a scatter plot with 5 random data sets from a text file

I need to read in around 5000 data sets with multiple values for each set. From there I need to set conditions on what is considered a "good value". I think I have achieved this and from this new subset sample from the overall population I need to randomly select 5 data sets and plot them wavelength vs flux.

I have tried to use a for loop to read out the 'ids' of the randomly selected data sets, but it always comes back with the same number. Each of the data sets has three conditional values as shown in the for gal in allgal loop which selects galaxies with good data points.

import random
import numpy as np
from numpy.polynomial.polynomial import polyfit
import matplotlib
matplotlib.use('TkAgg')
import matplotlib.pyplot as plt
import csv
import math
from collections import Counter

galnum = []
goodgal = []
xval = [3650, 4450, 5510, 6580, 8060, 9000, 12200, 21900]
yval = []


class Galaxy:
    def __init__(self, id , ra , dec , x , y , hawkiks_tot , k_flag , k_star , k_fluxrad , totmask , hawkiks , ehawkiks , vimosu , evimosu , vimosb , \
        evimosb , vimosv , evimosv , vimosr , evimosr , vimosi , evimosi , decamz , edecamz , fourstarj1 , efourstarj1 , hawkij , ehawkij , \
        irac1 , eirac1 , irac2, eirac2 , irac3 , eirac3 , irac4 , eirac4):
        self.id    = int(id)
        self.hawkiks_tot    = float(hawkiks_tot)
        self.k_flag = int(k_flag)
        self.totmask    = int(totmask)
        self.hawkiks    = float(hawkiks)
        self.vimosu    = float(vimosu)
        self.vimosb    = float(vimosb)
        self.vimosv    = float(vimosv)
        self.vimosr    = float(vimosr)
        self.vimosi    = float(vimosi)
        self.decamz    = float(decamz)
        self.hawkij    = float(hawkij)

    def __str__(self):
        return id + ' ' + hawkiks_tot + ' ' + k_flag + ' ' + totmask + ' ' + hawkiks + ' ' + vimosu + ' ' + vimosb + ' ' \
               + vimosv + ' ' + vimosr + ' ' + vimosi + ' ' + decamz + ' ' + hawkij


allgal = []

with open('/home/jacob/PHOTOMETRY/PHOTOM_CATS/SpARCS-0035_totalall_HAWKIKs.cat', 'r') as magfile:
    magplots = csv.reader(magfile)
    firstmagline = magfile.readline()
    for line in magfile:
        id , ra , dec , x , y , hawkiks_tot , k_flag , k_star , k_fluxrad , totmask , hawkiks , ehawkiks , vimosu , evimosu , vimosb , \
        evimosb , vimosv , evimosv , vimosr , evimosr , vimosi , evimosi , decamz , edecamz , fourstarj1 , efourstarj1 , hawkij , ehawkij , \
        irac1 , eirac1 , irac2, eirac2 , irac3 , eirac3 , irac4 , eirac4 = line.split()

        gal = Galaxy(id , ra , dec , x , y , hawkiks_tot , k_flag , k_star , k_fluxrad , totmask , hawkiks , ehawkiks , vimosu , evimosu , vimosb , \
        evimosb , vimosv , evimosv , vimosr , evimosr , vimosi , evimosi , decamz , edecamz , fourstarj1 , efourstarj1 , hawkij , ehawkij , \
        irac1 , eirac1 , irac2, eirac2 , irac3 , eirac3 , irac4 , eirac4)

        allgal.append(gal)


for gal in allgal:
    if gal.k_flag == 0 and gal.totmask == 0 and -2.5*math.log10(gal.hawkiks_tot) + 25 < 23:
        goodgal.append(gal)


random.seed(21)
sample_list = random.sample(goodgal, k=5)
print(sample_list)      #IDs: 2279, 3914, 3016, 4310, 2638

for goodgal in sample_list:
    print(id)

The final for loop is just making sure that the id's being printed out by the for loop match the ID's of the random number generator, as shown in the comment line in the code. But it always prints out 4889, which is the last data set ID number. I don't understand why this is appearing because the ID's of the assigned galaxies in the random.sample() don't even have this as their ID. It keeps printing out 4889, 4889, 4889, 4889, 4889. While it should print out, '2279, 3914, 3016, 4310, 2638'. This is just a check to make sure that everything is being read right. What I want to happen is basically to read out the different fluxes (given by hawkiks, vimosv, vimosb, etc), append them into yval array and then plot 5 separate graphs for each galaxy (data set) in terms of wavelength (the xval array) and flux (yval array).




Aucun commentaire:

Enregistrer un commentaire