Ejemplo n.º 1
0
    def load_experimental_data(self):
        # time correction factor
        c_t = 1 / (10 * 60)  # 1 / (min * s/min)

        # power correction factor
        c_p = 250 / 1E5  # 250 W(th) / 100 kW(th)

        # detector efficiency
        e = 1
        c_e = 1 / e

        # combine into one constant
        c = c_t * c_p * c_e

        spectrum_data = np.loadtxt('nai_gamma_spectrum.tka', skiprows=1) * c
        self.exp_gamma_raw = Spectrum(self.channels, spectrum_data)

        bg_data = np.loadtxt('background.tka', skiprows=1) * c
        self.background = Spectrum(self.channels, bg_data)

        true_data = spectrum_data - bg_data * c
        self.exp_gamma = Spectrum(self.channels, true_data)
Ejemplo n.º 2
0
def main():
    dirname = sys.argv[1]
    data = []
    parameter = []
    for filename in iglob(dirname + '/*.wav'):
        print(filename)
        sig, rate = read(filename, sr=16000)
        sig = (sig - np.mean(sig)) / np.var(sig)
        sp, yphase = Spectrum(sig, 512, 256, 512, 2)
        data.append(sp)
        parameter.append([yphase, filename.replace(dirname, '')])
        with open(sys.argv[2] + '_parameter.txt', 'wb') as fp:
            pickle.dump(parameter, fp)
        with open(sys.argv[2] + '.txt', 'wb') as fp:
            pickle.dump(data, fp)
Ejemplo n.º 3
0
 def __init__(self,
              instrument,
              distance=10.0,
              spec_path=None,
              exozodi_level=1.0,
              spec_reso=1e5):
     self.distance = distance
     self.instrument = instrument
     self.exozodi_level = exozodi_level  #Level of zodi relative to Solar System
     self.spec_path = spec_path
     self.spec_reso = spec_reso
     self.spec_data = self.sumZodiFlux().data
     self.wavelength = self.spec_data["Wavelength"]
     self.flux = self.spec_data["Flux"]
     self.spectrum = Spectrum(self.wavelength,
                              self.flux,
                              spec_reso=self.spec_reso)
Ejemplo n.º 4
0
def main(grammar, failed_tests, passed_tests) :
    """
    """
    prepare = Preparation(grammar,failed_tests, passed_tests)
    # get the represention of rules and tests
    rules = prepare.get_rules()
    failed = prepare.construct_matrix("failed")
    total_failed = failed.shape[1]
    passed = prepare.construct_matrix("passed")
    total_passed = passed.shape[1]
    # print(total_passed)
    failed_counts = prepare.basic_counts(failed)
    passed_counts = prepare.basic_counts(passed)
    # print(passed_counts)
    spectrum = Spectrum(failed_counts, passed_counts, total_failed, total_passed)
    scores = spectrum.compute_suspiciousness()
    spit_csv(rules, scores)
Ejemplo n.º 5
0
    def load_theoretical_data(self):
        # scale56 bin structure
        self.scale56 = np.loadtxt('scale56.txt') * 1E3  # normalize to keV

        # calculate normalization
        tally_area = tally_area = np.pi * (1.27**2)
        tally_area = 1  # for now not considering the tally area
        c = 8.32 / (200 * 1.60218e-13 * tally_area)
        c *= 250  # normalize to 250 W(th)

        # load theoretical gamma data
        data = np.loadtxt('gdata_total.txt')
        data = data.T[1][1:]
        data *= c

        # create spectrum
        self.the_gamma = Spectrum(self.scale56, data)
Ejemplo n.º 6
0
    def interpolateSpectrum(self, pointDict):
        """
        Interpoalte on grid spectra in given parameters
        return spectrum object
        """

        if self.gridParams is None:
            print("First load GRID")
            return None

        surAll = self.findSurronding(pointDict)

        if surAll is None:
            print("Spectrum (%.0f %.2f %.2f %.1f) out of grid" %
                  (pointDict["teff"], pointDict["logg"], pointDict["me"],
                   pointDict["vmic"]))
            return None

        point = [pointDict[k] for k in self.paramsList]
        data = []
        wave = self.wave
        for single in surAll:
            for i, (s, f) in enumerate(zip(self.gridParams, self.allFiles)):
                #print(s ,single)
                if np.array_equal(single, s):
                    spec = pd.read_csv(
                        f,
                        header=None,
                        delim_whitespace=True,
                        comment=self.comments,
                        skiprows=self.skipRows,
                    )
                    flux = spec[self.fluxColumn].values
                    if not self.ifCommonWavelength:  #TODO: this needs to be tested
                        if wave is None:
                            wave = spec[self.waveColumn].values
                        else:
                            flux = np.interp(wave,
                                             spec[self.waveColumn].values,
                                             spec[self.fluxColumn].values)
                    data.append(flux)

        interpFlux = self.multilinearInterpolation(surAll, data, point)

        return Spectrum(wave=self.wave, flux=interpFlux)
Ejemplo n.º 7
0
def loadRawCalibrationSpectrum():
    image_name = "test/test_image_cal.fits"
    try:
        spec = Spectrum(image_name)
    except SpectrumError as e:
        message = "{}\nLoad error".format(e)
        return message, None

    if spec is None:
        return message, None
    extracted_spectrum = np.genfromtxt("test/test_image_cal_extracted.dat",
                                       names=True)
    try:
        spec.setExtractedSpectrum(extracted_spectrum["spectrum"])
    except SpectrumError as e:
        message = "{}\nLoad error".format(e)
        return message, None

    return "Load successful", spec
Ejemplo n.º 8
0
    def _parse_ms_file(self, f_path):
        #        print "Parse file:",f_path
        # read ms/ms file in
        f = open(f_path)
        data = f.read()
        f.close()

        # create Spectrum instance
        spectrum = Spectrum(f_path)

        # set f_name
        spectrum.f_name = f_path
        # set metlin id
        spectrum.metlin_id = f_path[f_path.find("pos") + 3:f_path.find(".")]
        # set precursor
        _precursor = re.findall("parentmass[: ]+([0-9\.]+)", data)
        if len(_precursor) > 0:
            precursor = float(_precursor[0])
        else:
            raise Exception("ERROR: precursor not set for %s!" % f_path)
        spectrum.precursor = precursor
        spectrum.mass = precursor - 1.00794
        # set peaks and intensity
        _peaks = []
        seg = False
        for line in data.split('\n'):
            if line.find("collision") != -1:
                seg = True
                continue
            if not line:
                seg = False
                continue
            if seg:
                words = line.split()
                mass = float(words[0])
                inten = float(words[1])
                _peaks.append((mass, inten))
        spectrum.peaks = _peaks

        return spectrum
Ejemplo n.º 9
0
 def extract_spectra(self, description_field='', verbose=True):
     u"""
     Extract spectra from source imagery and creates well-defined spectrum
     objects.
     """
     # extracting spectra as simple lists
     spectra = gdal_utils.extract_spectra(self.img_src,
                                          self.extract_locations,
                                          neighborhood=self.neighborhood,
                                          verbose=verbose,
                                          bad_bands=self.bad_bands,
                                          scale_factor=self.slope,
                                          nb_type=self.neighborhood_type)
     # converting lists to spectrum objects
     # defining list of all extracted spectra
     # iterating over location/spectrum pairs
     for cp, sp in zip(self.extract_locations, spectra):
         # retrieving location id
         if cp.has_key('attributes'):
             additional_attributes = cp['attributes'].keys()
             additional_attributes.remove(self.loc_id_field)
         loc_id = cp[self.loc_id_field]
         # creating new spectrum object using location id and according coordinates
         spectrum = Spectrum(loc_id, (cp['x'], cp['y']))
         for aa in additional_attributes:
             spectrum.set_attribute(aa, cp['attributes'][aa])
         spectrum.set_neighborhood(self.neighborhood)
         spectrum.set_neighborhood_type(self.neighborhood_type)
         spectrum.set_source(self.img_id)
         if description_field:
             spectrum.set_description(cp[description_field])
         # adding values to spectrum
         for gb, val in zip(self.good_bands, sp):
             spectrum.set_value(gb, val)
         # adding values of bad bands to spectrum
         for bb in self.bad_bands:
             spectrum.set_invalid(bb)
         # adding current spectrum to list of all extracted spectra
         self.spectra.append(spectrum)
Ejemplo n.º 10
0
 def __init__(self,
              distance=10.0,
              spec_path=None,
              inclination_deg=90.0,
              rotation_vel=5e3,
              radial_vel=1e4,
              spec_reso=1e5):
     self.distance = distance
     self.spec_path = spec_path
     self.spec_reso = spec_reso
     if self.spec_path != None:
         self.spec_data = pyfits.open(self.spec_path)[1].data
         self.wavelength = self.spec_data["Wavelength"]
         self.flux = self.spec_data["Flux"]
         self.spectrum = Spectrum(self.wavelength,
                                  self.flux,
                                  spec_reso=self.spec_reso)
         self.spec_header = pyfits.open(self.spec_path)[1].header
         self.PHXREFF = self.spec_header["PHXREFF"]
     self.inclination_deg = inclination_deg
     self.rotation_vel = rotation_vel
     self.radial_vel = radial_vel
Ejemplo n.º 11
0
def plot_cf252():
    """Docstring."""

    # load data
    flux = cf252_source()
    eb = energy_groups('scale252')

    # spectrum object for ease of plotting
    spec = Spectrum(eb, flux, 0)

    # setup plotting environment
    fig = plt.figure(0)
    ax = fig.add_subplot(111)
    ax.set_xlim(0, 20)
    #ax.set_yscale('log')
    ax.set_xlabel('Energy $MeV$')
    ax.set_ylabel('Spectrum')

    # plot
    ax.plot(*spec.plot('plot', 'diff'))

    # save
    fig.savefig('plot/cf252.png', dpi=300)
Ejemplo n.º 12
0
def read_spectra(paragraphs, formatter):
    """
    Read the spectra data from a list of .docx paragraphs and return a
    list of `Spectrum` objects.

    :param paragraphs: a list of `Paragraph` objects.
    :param formatter: a `Formatter` object used to parse the raw data.

    :return: a list of `Spectrum` objects.
    """

    r_spectra = []
    for i, paragraph in enumerate(paragraphs):
        if paragraph.text.split(' ')[0] == 'Spectrum:':
            # This keyword signals that the next paragraphs contains the actual
            # spectrum data.

            r_spectra.append(
                (paragraph.text.rstrip().replace('Spectrum: ',
                                                 ''), paragraphs[i + 1]))
            # Store the cypher given after the keyword and the spectrum
            # itself as a tuple of raw data, itself appended to a list of
            # raw data.

    print('Number of located spectra: {}.\n'.format(len(r_spectra)))

    spectra = []  # The list to store processed spectra.
    for r_spectrum in r_spectra:
        spectrum = Spectrum(r_spectrum, formatter)  # Build a `Spectrum`
        # object for each tuple
        # in the list of raw data.
        print(spectrum)
        spectra.append(spectrum)  # Save each `Spectrum` object into a list.
        for signal in spectrum.signals:
            print(signal)
        print('\n')
    return spectra
Ejemplo n.º 13
0
def compose_speclist( speclist: List[ Spectrum ], namestring: str = "" ) -> Spectrum:
    """
    Forms a composite spectrum from the given input spectra

    :param speclist: List of Spectrum to compose
    :param namestring: Namestring to assign to the composite.  Defaults to ""
    :type speclist: list
    :type namestring: str
    :return: Composite Spectrum
    :rtype: Spectrum
    """
    from numpy import std, mean

    joined = { }
    composite = Spectrum( ns=namestring )
    for spec in speclist:
        for wl in spec:
            if wl not in joined:
                joined[ wl ] = list()
            joined[ wl ].append( spec.get( wl ) )

    wavelength_list = [ ]
    fluxlist = [ ]
    errlist = [ ]

    for wl, v in joined.items():
        wavelength_list.append( wl )
        if len( v ) > 1:
            fluxlist.append( mean( [ f[ 0 ] for f in v ] ) )
            errlist.append( std( [ e[ 1 ] for e in v ] ) )
        else:
            fluxlist.append( v[ 0 ][ 0 ] )
            errlist.append( v[ 0 ][ 1 ] )

    composite.setDict( wavelength_list, fluxlist, errlist )

    return composite
Ejemplo n.º 14
0
    ax.set_xticklabels(np.around(all_SNR))
    ax.set_yticklabels(np.around(all_SR))
    # ax.set_title(title)
    plt.savefig('fig/' + filename + '-heatmap.pdf', bbox_inches='tight')


# set the scenarios
weathers = ['clear', 'cloudy']
times = ['0.0', '0.8', '2.0', '3.9']
scenarios = it.product(times, weathers)

# load data from disk, and put into 'spectra'
spectra = []
for scenario in scenarios:
    wl, flux, label = getflux(scenario, convert_to_photons=True)
    new_spectrum = Spectrum(wl, flux, label)
    spectra.append(new_spectrum)

# to increase preformance for high-res original spectra,
# resample them with moderate resolution
for i, spectrum in enumerate(spectra):
    spectrum.resample(lam_min=4.05,
                      lam_max=19.99,
                      n_bins=1000,
                      overwrite_original=True)

spectrum_set = SpectrumSet(spectra)

# set up experiment designs
exptimes = [400, 1000, 4000]
all_n_bins = np.arange(2, 200, 1)
Ejemplo n.º 15
0
    def _parse_massbank_file(self, f_path):
        print "Parse file:", f_path
        # read ms/ms file in
        f = open(f_path)
        data = f.read()
        f.close()

        # create Spectrum instance
        spectrum = Spectrum(f_path)

        # set f_name
        spectrum.f_name = f_path

        # set precursor
        _precursor = re.findall(
            "MS\$FOCUSED_ION: PRECURSOR_M/Z[: ]+([0-9\.]+)", data)
        if len(_precursor) > 0:
            precursor = float(_precursor[0])
        else:
            _basepeak = re.findall("MS\$FOCUSED_ION: BASE_PEAK[: ]+([0-9\.]+)",
                                   data)
            if len(_basepeak) > 0:
                print("WARNING: using base peak as precursor for %s!" % f_path)
                precursor = float(_basepeak[0])
            else:
                raise Exception("ERROR: precursor not set for %s!" % f_path)
        spectrum.precursor = precursor

        # set ion mode
        _mode = re.findall("ION_MODE ([A-Z]+)", data)
        if len(_mode) > 0:
            mode = _mode[0]
        else:
            _mode = re.findall("MODE ([A-Z]+)", data)
            if len(_mode) > 0:
                print("WARNING: ion mode is set by MODE for %s!" % f_path)
                mode = _mode[0]
            else:
                raise Exception("ERROR: mode not set for %s!" % f_path)
        spectrum.mode = mode

        if spectrum.mode == 'POSITIVE':
            spectrum.mass = spectrum.precursor - 1.00794
        else:
            spectrum.mass = spectrum.precursor + 1.00794

        _ppm = re.findall("SE\$SEARCH_PPM[: ]+([0-9\.]+)", data)
        if len(_ppm) > 0:
            ppm = int(_ppm[0])
        else:
            raise Exception("ERROR: PPM not set for %s!" % f_path)
        spectrum.ppm = ppm

        # set peaks
        _peaks = []
        lines = data.split("\n")
        ready = False
        for line in lines:
            if len(line) == 0:
                continue
            if line.find("PK$PEAK") != -1:
                ready = True
                continue
            if ready:
                if line.find("N/A") != -1:
                    raise Exception("ERROR: no peaks in %s" % f_path)
                words = line.split()
                mass = float(words[0])
                inten = float(words[1])
                #mass = mass+numpy.random.normal(0,1e-8,1) # add noise
                #mass = float("%.3f" % mass)
                _peaks.append((mass, inten))
        spectrum.peaks = _peaks

        _ce = re.findall("COLLISION_ENERGY (\w+)", data)
        if len(_ce) > 0:
            ce = _ce[0]
            ce = ce.replace("eV", "")
            if ce.isdigit():
                spectrum.ce = int(ce)
        return spectrum
Ejemplo n.º 16
0
rcParams['ytick.direction'] = 'out'
rcParams['xtick.labelsize'] = 12
rcParams['ytick.labelsize'] = 12
rcParams['lines.linewidth'] = 1.85
rcParams['axes.labelsize'] = 15
rcParams.update({'figure.autolayout': True})

nebp = FluxNEBP(250)

R = response_matrix
f_i = nebp.values
N = unfiltered1.values
sig = unfiltered1.error

sol = iterate(f_i, N, sig, R)
solution = Spectrum(nebp.edges, sol)

from_gravel = unfolded_data['e1_ne_gr']


def sample(responses, errors):
    l = len(responses)
    sampled_response = np.zeros(l)
    for i in range(l):
        resp = responses[i]
        err = errors[i]
        fun = norm(loc=resp, scale=err)
        rho = rand()
        sampled_response[i] = fun.ppf(rho)
    return sampled_response
Ejemplo n.º 17
0
from spectrum import Spectrum

import os

#path to where files are written to
overallpath = '/Users/pohno/Box Sync/Science/Data/SFG/Solstice/11192017'

#name where summedTruncatedData is written to
name = 'flowrun2.txt'

#path where the data is stored
path = '/Users/pohno/Box Sync/Science/Data/SFG/Solstice/11192017/caf2_water/run2'

#create object, loads each sample and background DFG
spec = Spectrum(path)

#change directory in case files are written
os.chdir(overallpath)

#plot pre cosmic ray removal
spec.plotDFGs()
spec.plotBGs()

#remove cosmic rays
spec.removeCRs(50)

#plot after cosmic ray removal
spec.plotDFGs()
spec.plotBGs()
Ejemplo n.º 18
0
def __main__():
    #initDict = readInit(init_file="SunEarth_4m.init")
    initDict = readInit(init_file="TMT_SuperEarth_N.init")
    wav_min, wav_max, t_exp = np.float32(initDict["wav_min"]), np.float32(
        initDict["wav_max"]), np.float32(initDict["t_exp"])
    target_pl = Target(distance=np.float32(initDict["distance"]),
                       spec_path=initDict["pl_spec_path"],
                       inclination_deg=np.float32(
                           initDict["pl_inclination_deg"]),
                       rotation_vel=np.float32(initDict["pl_rotation_vel"]),
                       radial_vel=np.float32(initDict["pl_radial_vel"]),
                       spec_reso=np.float32(initDict["spec_reso"]))
    target_st = Target(distance=np.float32(initDict["distance"]),
                       spec_path=initDict["st_spec_path"],
                       inclination_deg=np.float32(
                           initDict["st_inclination_deg"]),
                       rotation_vel=np.float32(initDict["st_rotation_vel"]),
                       radial_vel=np.float32(initDict["st_radial_vel"]),
                       spec_reso=np.float32(initDict["spec_reso"]))
    wav_med = (wav_min + wav_max) / 2.0
    if initDict["spec_tran_path"] != "None":
        atmosphere = Atmosphere(spec_tran_path=initDict["spec_tran_path"],
                                spec_radi_path=initDict["spec_radi_path"])
    else:
        atmosphere = None
    instrument = Instrument(
        wav_med,
        telescope_size=np.float32(initDict["telescope_size"]),
        pl_st_contrast=np.float32(initDict["pl_st_contrast"]),
        spec_reso=np.float32(initDict["spec_reso"]),
        read_noise=np.float32(initDict["read_noise"]),
        dark_current=np.float32(initDict["dark_current"]),
        fiber_size=np.float32(initDict["fiber_size"]),
        pixel_sampling=np.float32(initDict["pixel_sampling"]),
        throughput=np.float32(initDict["throughput"]),
        wfc_residual=np.float32(initDict["wfc_residual"]),
        num_surfaces=np.float32(initDict["num_surfaces"]),
        temperature=np.float32(initDict["temperature"]))
    thermal_background = ThermTarget(instrument,
                                     spec_reso=np.float32(
                                         initDict["spec_reso"]))
    zodi = ZodiTarget(instrument,
                      distance=np.float32(initDict["distance"]),
                      spec_path=initDict["zodi_spec_path"],
                      exozodi_level=np.float32(initDict["exozodi_level"]),
                      spec_reso=np.float32(initDict["spec_reso"]))
    hci_hrs = HCI_HRS_Observation(wav_min,
                                  wav_max,
                                  t_exp,
                                  target_pl,
                                  target_st,
                                  instrument,
                                  thermal_background,
                                  zodi,
                                  atmosphere=atmosphere)
    print(
        "Star flux: {0} \nLeaked star flux: {1}\nPlanet flux: {2}\nPlanet flux per pixel: {3}\nThermal background flux:  {4}\nThermal background flux per pixel:  {5}\nSky flux: {6}\nSky flux per pixel: {7}\nSky transmission: {8}\nTotal pixel number: {9}\n"
        .format(
            hci_hrs.star_total_flux,
            hci_hrs.star_total_flux * instrument.pl_st_contrast,
            hci_hrs.planet_total_flux, hci_hrs.planet_total_flux /
            (len(hci_hrs.obs_spec_resample.flux) + 0.0),
            hci_hrs.therm_total_flux, hci_hrs.therm_total_flux /
            (len(hci_hrs.obs_therm_resample.flux) + 0.0),
            hci_hrs.sky_total_flux, hci_hrs.sky_total_flux /
            (len(hci_hrs.obs_therm_resample.flux) + 0.0),
            hci_hrs.sky_transmission, len(hci_hrs.obs_therm_resample.flux)))
    spec = pyfits.open(initDict["template_path"])
    template = Spectrum(spec[1].data["Wavelength"],
                        spec[1].data["Flux"],
                        spec_reso=np.float32(initDict["spec_reso"]))
    if initDict["spec_tran_path"] != "None":
        hci_hrs_red = HCI_HRS_Reduction(hci_hrs,
                                        template,
                                        save_flag=False,
                                        obj_tag=initDict["obj_tag"],
                                        template_tag=initDict["template_tag"],
                                        speckle_flag=False)
    else:
        hci_hrs_red = HCI_HRS_Reduction(hci_hrs,
                                        template,
                                        save_flag=False,
                                        obj_tag=initDict["obj_tag"],
                                        template_tag=initDict["template_tag"],
                                        speckle_flag=True)
Ejemplo n.º 19
0
n = 1000000
for i in range(n):
    ind1, ind2 = randint(0, 7), randint(0, 7)
    score = loaded[ind1] + loaded[ind2]
    loaded_count[score - 1] += 1

for i in range(n):
    ind1, ind2 = randint(0, 6), randint(0, 6)
    score = fair[ind1] + fair[ind2]
    fair_count[score - 1] += 1

loaded_count = loaded_count / n
fair_count = fair_count / n

edges = np.array(range(13)) + 0.5
loaded_data = Spectrum(edges[1:], loaded_count[1:])
fair_data = Spectrum(edges[1:], fair_count[1:])

var = 0
for i, val in enumerate(loaded_count[1:]):
    var += (7 - (i + 2))**2 * val

print('The variance is:  {}'.format(var))

# plotting
fig = plt.figure(999, figsize=(9.62, 5.08))
ax = fig.add_subplot(111)
ax.plot(loaded_data.step_x,
        loaded_data.step_y,
        label='Loaded Dice',
        color='darkblue')
Ejemplo n.º 20
0
def main():

    for run_number in range(0, 1):
        print 'Run number {}'.format(run_number)
        sim_data = []
        for scenario_choice in ['a', 'b']:
            for vcs in [True, False]:
                for lambda_a, lambda_c in lambda_vals:
                    print 'Starting with scenario {} for vcs {}. Lambda A = {} and Lambda C = {}.'.format(
                        scenario_choice, vcs, lambda_a, lambda_c)
                    # Initializing scenario A
                    if scenario_choice == 'a':
                        station_a = Station('A', lambda_a, 'Sender',
                                            backoff_range, total_slots,
                                            slot_duration)
                        station_b = Station('B', 0, 'Receiver', backoff_range,
                                            total_slots, slot_duration)
                        station_c = Station('C', lambda_c, 'Sender',
                                            backoff_range, total_slots,
                                            slot_duration)
                        station_d = Station('D', 0, 'Receiver', backoff_range,
                                            total_slots, slot_duration)
                        station_a.set_station_communicating(station_b)
                        station_b.set_station_communicating(station_a)
                        station_c.set_station_communicating(station_d)
                        station_d.set_station_communicating(station_c)
                        station_a.set_collision_domain(
                            [station_b, station_c, station_d])
                        station_b.set_collision_domain(
                            [station_a, station_c, station_d])
                        station_c.set_collision_domain(
                            [station_a, station_b, station_d])
                        station_d.set_collision_domain(
                            [station_a, station_b, station_c])
                        spectrum = Spectrum()
                        scenario = Scenario(
                            [station_a, station_b, station_c, station_d],
                            spectrum, vcs, scenario_choice)

                    # Initializing scenario B
                    if scenario_choice == 'b':
                        station_a = Station('A', lambda_a, 'Sender',
                                            backoff_range, total_slots,
                                            slot_duration)
                        station_b = Station('B', 0, 'Receiver', backoff_range,
                                            total_slots, slot_duration)
                        station_c = Station('C', lambda_c, 'Sender',
                                            backoff_range, total_slots,
                                            slot_duration)
                        station_a.set_station_communicating(station_b)
                        station_b.set_station_communicating(station_a)
                        station_c.set_station_communicating(station_b)
                        station_a.set_collision_domain([station_b])
                        station_b.set_collision_domain([station_a, station_c])
                        station_c.set_collision_domain([station_b])
                        spectrum = Spectrum()
                        scenario = Scenario([station_a, station_b, station_c],
                                            spectrum, vcs, scenario_choice)

                    for slot_num in range(0, total_slots):
                        prepare_transmitting_stations(
                            scenario.sending_stations, slot_num
                        )  # Checking to see if a node is trying to send a packet at a given slot.
                        check_difs_counters(
                            scenario.sending_stations
                        )  # Checking to see if the difs counter for any node is 0 to start the backoff.
                        check_backoff_counters(
                            scenario.sending_stations, scenario.spectrum,
                            scenario.vcs
                        )  # Checking to see if the backoff counter for any node is 0 so we can send a packet.
                        if scenario.vcs:
                            check_RTS_counter(
                                scenario.spectrum, scenario.sending_stations
                            )  # if we are using VCS, check the RTS counter
                            check_CTS_counter(
                                scenario.spectrum, scenario.sending_stations
                            )  # if we are using VCS, check the CTS counter
                        check_data_counters(
                            scenario.spectrum, scenario.sending_stations
                        )  # Checking to see if the data counter is done.
                        check_sifs_counters(
                            scenario.sending_stations, scenario.spectrum
                        )  # Checking to see if the sifs counter for any node is 0 to free the medium.
                        check_ack_counters(
                            scenario.spectrum, scenario.sending_stations
                        )  # Checking to see if the awk counter is done.

                        end_of_slot(
                            scenario
                        )  # Decreasing all counters in the scenario.

                        # DEBUG information

                        # try:
                        # 	print 'On slot {}'.format(slot_num)
                        # 	print 'A next time slot: {}'.format(station_a.time_slots[0])
                        # 	print 'A difs counter: {}'.format(station_a.difs_counter)
                        # 	print 'A backoff counter: {}'.format(station_a.backoff)
                        # 	print 'A data counter: {}'.format(station_a.data_counter)
                        # 	print 'A sifs counter: {}'.format(station_a.sifs_counter)
                        # 	print 'A ack counter: {}'.format(station_a.ack_counter)
                        # 	print 'A rts counter: {}'.format(station_a.rts_counter)
                        # 	print 'A cts counter: {}'.format(station_a.cts_counter)
                        # 	print 'A wait times: {}'.format(station_a.wait_time)

                        # 	print 'C next time slot: {}'.format(station_c.time_slots[0])
                        # 	print 'C difs counter: {}'.format(station_c.difs_counter)
                        # 	print 'C backoff counter: {}'.format(station_c.backoff)
                        # 	print 'C data counter: {}'.format(station_c.data_counter)
                        # 	print 'C sifs counter: {}'.format(station_c.sifs_counter)
                        # 	print 'C ack counter: {}'.format(station_c.ack_counter)
                        # 	print 'C rts counter: {}'.format(station_c.rts_counter)
                        # 	print 'C cts counter: {}'.format(station_c.cts_counter)
                        # 	print 'C wait times: {}'.format(station_c.wait_time)
                        # 	print 'Spectrum List:'
                        # 	for a in spectrum.sending_station:
                        # 		print a.name
                        # 	print 'Spectrum status: {}\n'.format(spectrum.status)
                        # except IndexError, e:
                        # 	print 'No more data to send. Breaking'
                        # 	break

                    # print 'A {}\nC {}\nDIV: {}\n\n'.format(station_a.slots_transmitting, station_c.slots_transmitting, station_a.slots_transmitting / float(station_c.slots_transmitting))

                    single_sim_data = {  # using hash table to record all of the information of a single simulation
                        'lambda_a':
                        lambda_a,
                        'lambda_c':
                        lambda_c,
                        'a_collisions':
                        station_a.num_collisions,
                        'c_collisions':
                        station_c.num_collisions,
                        'a_throughput':
                        station_a.num_data_transmit / float(simulation_time),
                        'c_throughput':
                        station_c.num_data_transmit / float(simulation_time),
                        'a_slots_transmitting':
                        station_a.slots_transmitting,
                        'c_slots_transmitting':
                        station_c.slots_transmitting,
                        'FI':
                        station_a.slots_transmitting /
                        float(station_c.slots_transmitting),
                        'vcs':
                        vcs,
                        'scenario':
                        scenario_choice
                    }
                    sim_data.append(single_sim_data)
                    # print single_sim_data

        for sim in sim_data:
            print sim

        plt.figure(0)
        plt.figure(figsize=(8, 8))
        x_vals = [
            sim['lambda_c'] for sim in sim_data
            if sim['lambda_a'] == sim['lambda_c'] and sim['vcs']
            and sim['scenario'] == 'a'
        ]
        no_vcs_a_y_vals = [
            sim['a_throughput'] for sim in sim_data
            if sim['lambda_a'] == sim['lambda_c'] and not sim['vcs']
            and sim['scenario'] == 'a'
        ]
        vcs_a_y_vals = [
            sim['a_throughput'] for sim in sim_data
            if sim['lambda_a'] == sim['lambda_c'] and sim['vcs']
            and sim['scenario'] == 'a'
        ]
        no_vcs_b_y_vals = [
            sim['a_throughput'] for sim in sim_data
            if sim['lambda_a'] == sim['lambda_c'] and not sim['vcs']
            and sim['scenario'] == 'b'
        ]
        vcs_b_y_vals = [
            sim['a_throughput'] for sim in sim_data
            if sim['lambda_a'] == sim['lambda_c'] and sim['vcs']
            and sim['scenario'] == 'b'
        ]
        plt.plot(x_vals,
                 no_vcs_a_y_vals,
                 '-bo',
                 linewidth=2.0,
                 markersize=10,
                 label='Scenario A CSMA')
        plt.plot(x_vals,
                 vcs_a_y_vals,
                 '-rs',
                 linewidth=2.0,
                 markersize=10,
                 label='Scenario A CSMA w. Virtual Sensing')
        plt.plot(x_vals,
                 no_vcs_b_y_vals,
                 '-g+',
                 linewidth=2.0,
                 markersize=10,
                 label='Scenario B CSMA')
        plt.plot(x_vals,
                 vcs_b_y_vals,
                 '-kx',
                 linewidth=2.0,
                 markersize=10,
                 label='Scenario B CSMA w. Virtual Sensing')
        plt.legend(loc=2, markerscale=0.8, prop={'size': 8})
        plt.xlim((45, 305))
        plt.ylabel(r'$T$ (Kbps)')
        plt.xlabel(r'$\lambda$ (frames/sec)')
        plt.title(
            r'1.a Node A: Throughput $T$ (Kbps) vs Rate $\lambda$ (frames/sec)'
        )
        plt.savefig('fig1-a' + str(run_number) + '.png')

        plt.figure(1)
        plt.figure(figsize=(8, 8))
        x_vals = [
            sim['lambda_c'] for sim in sim_data
            if sim['lambda_a'] == sim['lambda_c'] and sim['vcs']
            and sim['scenario'] == 'a'
        ]
        no_vcs_a_y_vals = [
            sim['c_throughput'] for sim in sim_data
            if sim['lambda_a'] == sim['lambda_c'] and not sim['vcs']
            and sim['scenario'] == 'a'
        ]
        vcs_a_y_vals = [
            sim['c_throughput'] for sim in sim_data
            if sim['lambda_a'] == sim['lambda_c'] and sim['vcs']
            and sim['scenario'] == 'a'
        ]
        no_vcs_b_y_vals = [
            sim['c_throughput'] for sim in sim_data
            if sim['lambda_a'] == sim['lambda_c'] and not sim['vcs']
            and sim['scenario'] == 'b'
        ]
        vcs_b_y_vals = [
            sim['c_throughput'] for sim in sim_data
            if sim['lambda_a'] == sim['lambda_c'] and sim['vcs']
            and sim['scenario'] == 'b'
        ]
        plt.plot(x_vals,
                 no_vcs_a_y_vals,
                 '-bo',
                 linewidth=2.0,
                 markersize=10,
                 label='Scenario A CSMA')
        plt.plot(x_vals,
                 vcs_a_y_vals,
                 '-rs',
                 linewidth=2.0,
                 markersize=10,
                 label='Scenario A CSMA w. Virtual Sensing')
        plt.plot(x_vals,
                 no_vcs_b_y_vals,
                 '-g+',
                 linewidth=2.0,
                 markersize=10,
                 label='Scenario B CSMA')
        plt.plot(x_vals,
                 vcs_b_y_vals,
                 '-kx',
                 linewidth=2.0,
                 markersize=10,
                 label='Scenario B CSMA w. Virtual Sensing')
        plt.legend(loc=2, markerscale=0.8, prop={'size': 8})
        plt.xlim((45, 305))
        plt.ylabel(r'$T$ (Kbps)')
        plt.xlabel(r'$\lambda$ (frames/sec)')
        plt.title(
            r'1.b Node C: Throughput $T$ (Kbps) vs Rate $\lambda$ (frames/sec)'
        )
        plt.savefig('fig1-b' + str(run_number) + '.png')

        plt.figure(2)
        plt.figure(figsize=(8, 8))
        x_vals = [
            sim['lambda_c'] for sim in sim_data if sim['lambda_a'] == (
                2 * sim['lambda_c']) and sim['vcs'] and sim['scenario'] == 'a'
        ]
        no_vcs_a_y_vals = [
            sim['a_throughput'] for sim in sim_data if sim['lambda_a'] ==
            (2 * sim['lambda_c']) and not sim['vcs'] and sim['scenario'] == 'a'
        ]
        vcs_a_y_vals = [
            sim['a_throughput'] for sim in sim_data if sim['lambda_a'] == (
                2 * sim['lambda_c']) and sim['vcs'] and sim['scenario'] == 'a'
        ]
        no_vcs_b_y_vals = [
            sim['a_throughput'] for sim in sim_data if sim['lambda_a'] ==
            (2 * sim['lambda_c']) and not sim['vcs'] and sim['scenario'] == 'b'
        ]
        vcs_b_y_vals = [
            sim['a_throughput'] for sim in sim_data if sim['lambda_a'] == (
                2 * sim['lambda_c']) and sim['vcs'] and sim['scenario'] == 'b'
        ]
        plt.plot(x_vals,
                 no_vcs_a_y_vals,
                 '-bo',
                 linewidth=2.0,
                 markersize=10,
                 label='Scenario A CSMA')
        plt.plot(x_vals,
                 vcs_a_y_vals,
                 '-rs',
                 linewidth=2.0,
                 markersize=10,
                 label='Scenario A CSMA w. Virtual Sensing')
        plt.plot(x_vals,
                 no_vcs_b_y_vals,
                 '-g+',
                 linewidth=2.0,
                 markersize=10,
                 label='Scenario B CSMA')
        plt.plot(x_vals,
                 vcs_b_y_vals,
                 '-kx',
                 linewidth=2.0,
                 markersize=10,
                 label='Scenario B CSMA w. Virtual Sensing')
        plt.legend(loc=2, markerscale=0.8, prop={'size': 8})
        plt.xlim((45, 305))
        plt.ylabel(r'$T$ (Kbps)')
        plt.xlabel(r'$\lambda$ (frames/sec)')
        plt.title(
            r'1.c Node A: Throughput $T$ (Kbps) vs Rate $\lambda$ (frames/sec) when $\lambda$A = 2$\lambda$C'
        )
        plt.savefig('fig1-c' + str(run_number) + '.png')

        plt.figure(3)
        plt.figure(figsize=(8, 8))
        x_vals = [
            sim['lambda_c'] for sim in sim_data if sim['lambda_a'] == (
                2 * sim['lambda_c']) and sim['vcs'] and sim['scenario'] == 'a'
        ]
        no_vcs_a_y_vals = [
            sim['c_throughput'] for sim in sim_data if sim['lambda_a'] ==
            (2 * sim['lambda_c']) and not sim['vcs'] and sim['scenario'] == 'a'
        ]
        vcs_a_y_vals = [
            sim['c_throughput'] for sim in sim_data if sim['lambda_a'] == (
                2 * sim['lambda_c']) and sim['vcs'] and sim['scenario'] == 'a'
        ]
        no_vcs_b_y_vals = [
            sim['c_throughput'] for sim in sim_data if sim['lambda_a'] ==
            (2 * sim['lambda_c']) and not sim['vcs'] and sim['scenario'] == 'b'
        ]
        vcs_b_y_vals = [
            sim['c_throughput'] for sim in sim_data if sim['lambda_a'] == (
                2 * sim['lambda_c']) and sim['vcs'] and sim['scenario'] == 'b'
        ]
        plt.plot(x_vals,
                 no_vcs_a_y_vals,
                 '-bo',
                 linewidth=2.0,
                 markersize=10,
                 label='Scenario A CSMA')
        plt.plot(x_vals,
                 vcs_a_y_vals,
                 '-rs',
                 linewidth=2.0,
                 markersize=10,
                 label='Scenario A CSMA w. Virtual Sensing')
        plt.plot(x_vals,
                 no_vcs_b_y_vals,
                 '-g+',
                 linewidth=2.0,
                 markersize=10,
                 label='Scenario B CSMA')
        plt.plot(x_vals,
                 vcs_b_y_vals,
                 '-kx',
                 linewidth=2.0,
                 markersize=10,
                 label='Scenario B CSMA w. Virtual Sensing')
        plt.legend(loc=2, markerscale=0.8, prop={'size': 8})
        plt.xlim((45, 305))
        plt.ylabel(r'$T$ (Kbps)')
        plt.xlabel(r'$\lambda$ (frames/sec)')
        plt.title(
            r'1.d Node C: Throughput $T$ (Kbps) vs Rate $\lambda$ (frames/sec) when $\lambda$A = 2$\lambda$C'
        )
        plt.savefig('fig1-d' + str(run_number) + '.png')

        plt.figure(4)
        plt.figure(figsize=(8, 8))
        x_vals = [
            sim['lambda_c'] for sim in sim_data
            if sim['lambda_c'] == sim['lambda_a'] and sim['vcs']
            and sim['scenario'] == 'a'
        ]
        no_vcs_a_y_vals = [
            sim['a_collisions'] for sim in sim_data
            if sim['lambda_a'] == sim['lambda_c'] and not sim['vcs']
            and sim['scenario'] == 'a'
        ]
        vcs_a_y_vals = [
            sim['a_collisions'] for sim in sim_data
            if sim['lambda_a'] == sim['lambda_c'] and sim['vcs']
            and sim['scenario'] == 'a'
        ]
        no_vcs_b_y_vals = [
            sim['a_collisions'] for sim in sim_data
            if sim['lambda_a'] == sim['lambda_c'] and not sim['vcs']
            and sim['scenario'] == 'b'
        ]
        vcs_b_y_vals = [
            sim['a_collisions'] for sim in sim_data
            if sim['lambda_a'] == sim['lambda_c'] and sim['vcs']
            and sim['scenario'] == 'b'
        ]
        plt.plot(x_vals,
                 no_vcs_a_y_vals,
                 '-bo',
                 linewidth=2.0,
                 markersize=10,
                 label='Scenario A CSMA')
        plt.plot(x_vals,
                 vcs_a_y_vals,
                 '-rs',
                 linewidth=2.0,
                 markersize=10,
                 label='Scenario A CSMA w. Virtual Sensing')
        plt.plot(x_vals,
                 no_vcs_b_y_vals,
                 '-g+',
                 linewidth=2.0,
                 markersize=10,
                 label='Scenario B CSMA')
        plt.plot(x_vals,
                 vcs_b_y_vals,
                 '-kx',
                 linewidth=2.0,
                 markersize=10,
                 label='Scenario B CSMA w. Virtual Sensing')
        plt.legend(loc=2, markerscale=0.8, prop={'size': 8})
        plt.xlim((45, 305))
        plt.ylabel(r'$N$ (Number of Collisions)')
        plt.xlabel(r'$\lambda$ (frames/sec)')
        plt.title(
            r'2.a Node A: Number of Collisions $N$ vs Rate $\lambda$ (frames/sec)'
        )
        plt.savefig('fig2-a' + str(run_number) + '.png')

        plt.figure(5)
        plt.figure(figsize=(8, 8))
        x_vals = [
            sim['lambda_c'] for sim in sim_data
            if sim['lambda_a'] == sim['lambda_c'] and sim['vcs']
            and sim['scenario'] == 'a'
        ]
        no_vcs_a_y_vals = [
            sim['c_collisions'] for sim in sim_data
            if sim['lambda_a'] == sim['lambda_c'] and not sim['vcs']
            and sim['scenario'] == 'a'
        ]
        vcs_a_y_vals = [
            sim['c_collisions'] for sim in sim_data
            if sim['lambda_a'] == sim['lambda_c'] and sim['vcs']
            and sim['scenario'] == 'a'
        ]
        no_vcs_b_y_vals = [
            sim['c_collisions'] for sim in sim_data
            if sim['lambda_a'] == sim['lambda_c'] and not sim['vcs']
            and sim['scenario'] == 'b'
        ]
        vcs_b_y_vals = [
            sim['c_collisions'] for sim in sim_data
            if sim['lambda_a'] == sim['lambda_c'] and sim['vcs']
            and sim['scenario'] == 'b'
        ]
        plt.plot(x_vals,
                 no_vcs_a_y_vals,
                 '-bo',
                 linewidth=2.0,
                 markersize=10,
                 label='Scenario A CSMA')
        plt.plot(x_vals,
                 vcs_a_y_vals,
                 '-rs',
                 linewidth=2.0,
                 markersize=10,
                 label='Scenario A CSMA w. Virtual Sensing')
        plt.plot(x_vals,
                 no_vcs_b_y_vals,
                 '-g+',
                 linewidth=2.0,
                 markersize=10,
                 label='Scenario B CSMA')
        plt.plot(x_vals,
                 vcs_b_y_vals,
                 '-kx',
                 linewidth=2.0,
                 markersize=10,
                 label='Scenario B CSMA w. Virtual Sensing')
        plt.legend(loc=2, markerscale=0.8, prop={'size': 8})
        plt.xlim((45, 305))
        plt.ylabel(r'$N$ (Number of Collisions)')
        plt.xlabel(r'$\lambda$ (frames/sec)')
        plt.title(
            r'2.b Node C: Number of Collisions $N$ vs Rate $\lambda$ (frames/sec)'
        )
        plt.savefig('fig2-b' + str(run_number) + '.png')

        plt.figure(6)
        plt.figure(figsize=(8, 8))
        x_vals = [
            sim['lambda_c'] for sim in sim_data if sim['lambda_a'] == (
                2 * sim['lambda_c']) and sim['vcs'] and sim['scenario'] == 'a'
        ]
        no_vcs_a_y_vals = [
            sim['a_collisions'] for sim in sim_data if sim['lambda_a'] ==
            (2 * sim['lambda_c']) and not sim['vcs'] and sim['scenario'] == 'a'
        ]
        vcs_a_y_vals = [
            sim['a_collisions'] for sim in sim_data if sim['lambda_a'] == (
                2 * sim['lambda_c']) and sim['vcs'] and sim['scenario'] == 'a'
        ]
        no_vcs_b_y_vals = [
            sim['a_collisions'] for sim in sim_data if sim['lambda_a'] ==
            (2 * sim['lambda_c']) and not sim['vcs'] and sim['scenario'] == 'b'
        ]
        vcs_b_y_vals = [
            sim['a_collisions'] for sim in sim_data if sim['lambda_a'] == (
                2 * sim['lambda_c']) and sim['vcs'] and sim['scenario'] == 'b'
        ]
        plt.plot(x_vals,
                 no_vcs_a_y_vals,
                 '-bo',
                 linewidth=2.0,
                 markersize=10,
                 label='Scenario A CSMA')
        plt.plot(x_vals,
                 vcs_a_y_vals,
                 '-rs',
                 linewidth=2.0,
                 markersize=10,
                 label='Scenario A CSMA w. Virtual Sensing')
        plt.plot(x_vals,
                 no_vcs_b_y_vals,
                 '-g+',
                 linewidth=2.0,
                 markersize=10,
                 label='Scenario B CSMA')
        plt.plot(x_vals,
                 vcs_b_y_vals,
                 '-kx',
                 linewidth=2.0,
                 markersize=10,
                 label='Scenario B CSMA w. Virtual Sensing')
        plt.legend(loc=2, markerscale=0.8, prop={'size': 8})
        plt.xlim((45, 305))
        plt.ylabel(r'$T$ (Kbps)')
        plt.xlabel(r'$\lambda$ (frames/sec)')
        plt.title(
            r'2.c Node A: Number of Collisions $N$ vs Rate $\lambda$ (frames/sec) when $\lambda$A = 2$\lambda$C'
        )
        plt.savefig('fig2-c' + str(run_number) + '.png')

        plt.figure(7)
        plt.figure(figsize=(8, 8))
        x_vals = [
            sim['lambda_c'] for sim in sim_data if sim['lambda_a'] == (
                2 * sim['lambda_c']) and sim['vcs'] and sim['scenario'] == 'a'
        ]
        no_vcs_a_y_vals = [
            sim['c_collisions'] for sim in sim_data if sim['lambda_a'] ==
            (2 * sim['lambda_c']) and not sim['vcs'] and sim['scenario'] == 'a'
        ]
        vcs_a_y_vals = [
            sim['c_collisions'] for sim in sim_data if sim['lambda_a'] == (
                2 * sim['lambda_c']) and sim['vcs'] and sim['scenario'] == 'a'
        ]
        no_vcs_b_y_vals = [
            sim['c_collisions'] for sim in sim_data if sim['lambda_a'] ==
            (2 * sim['lambda_c']) and not sim['vcs'] and sim['scenario'] == 'b'
        ]
        vcs_b_y_vals = [
            sim['c_collisions'] for sim in sim_data if sim['lambda_a'] == (
                2 * sim['lambda_c']) and sim['vcs'] and sim['scenario'] == 'b'
        ]
        plt.plot(x_vals,
                 no_vcs_a_y_vals,
                 '-bo',
                 linewidth=2.0,
                 markersize=10,
                 label='Scenario A CSMA')
        plt.plot(x_vals,
                 vcs_a_y_vals,
                 '-rs',
                 linewidth=2.0,
                 markersize=10,
                 label='Scenario A CSMA w. Virtual Sensing')
        plt.plot(x_vals,
                 no_vcs_b_y_vals,
                 '-g+',
                 linewidth=2.0,
                 markersize=10,
                 label='Scenario B CSMA')
        plt.plot(x_vals,
                 vcs_b_y_vals,
                 '-kx',
                 linewidth=2.0,
                 markersize=10,
                 label='Scenario B CSMA w. Virtual Sensing')
        plt.legend(loc=2, markerscale=0.8, prop={'size': 8})
        plt.xlim((45, 305))
        plt.ylabel(r'$T$ (Kbps)')
        plt.xlabel(r'$\lambda$ (frames/sec)')
        plt.title(
            r'2.d Node C: Number of Collisions $N$ vs Rate $\lambda$ (frames/sec) when $\lambda$A = 2$\lambda$C'
        )
        plt.savefig('fig2-d' + str(run_number) + '.png')

        plt.figure(8)
        plt.figure(figsize=(8, 8))
        x_vals = [
            sim['lambda_c'] for sim in sim_data
            if sim['lambda_a'] == sim['lambda_c'] and sim['vcs']
            and sim['scenario'] == 'a'
        ]
        y_vals_a = [
            sim['FI'] for sim in sim_data if sim['lambda_a'] == sim['lambda_c']
            and not sim['vcs'] and sim['scenario'] == 'a'
        ]
        y_vals_b = [
            sim['FI'] for sim in sim_data if sim['lambda_a'] == sim['lambda_c']
            and not sim['vcs'] and sim['scenario'] == 'b'
        ]
        vcs_y_vals_a = [
            sim['FI'] for sim in sim_data if sim['lambda_a'] == sim['lambda_c']
            and sim['vcs'] and sim['scenario'] == 'a'
        ]
        vcs_y_vals_b = [
            sim['FI'] for sim in sim_data if sim['lambda_a'] == sim['lambda_c']
            and sim['vcs'] and sim['scenario'] == 'b'
        ]
        plt.plot(x_vals,
                 y_vals_a,
                 '-bo',
                 linewidth=2.0,
                 markersize=10,
                 label='Scenario A CSMA')
        plt.plot(x_vals,
                 vcs_y_vals_a,
                 '-rs',
                 linewidth=2.0,
                 markersize=10,
                 label='Scenario A CSMA w. Virtual Sensing')
        plt.plot(x_vals,
                 y_vals_b,
                 '-g+',
                 linewidth=2.0,
                 markersize=10,
                 label='Scenario B CSMA')
        plt.plot(x_vals,
                 vcs_y_vals_b,
                 '-kx',
                 linewidth=2.0,
                 markersize=10,
                 label='Scenario B CSMA w. Virtual Sensing')
        plt.legend(loc=2, markerscale=0.8, prop={'size': 8})
        plt.xlim((45, 305))
        plt.ylabel(r'$FI$ (Fairness Index)')
        plt.xlabel(r'$\lambda$ (frames/sec)')
        plt.title('3.a Fairness Index')
        plt.savefig('fig3-a' + str(run_number) + '.png')

        plt.figure(9)
        plt.figure(figsize=(8, 8))
        x_vals = [
            sim['lambda_c'] for sim in sim_data if sim['lambda_a'] == (
                2 * sim['lambda_c']) and sim['vcs'] and sim['scenario'] == 'a'
        ]
        y_vals_a = [
            sim['FI'] for sim in sim_data if sim['lambda_a'] ==
            (2 * sim['lambda_c']) and not sim['vcs'] and sim['scenario'] == 'a'
        ]
        y_vals_b = [
            sim['FI'] for sim in sim_data if sim['lambda_a'] ==
            (2 * sim['lambda_c']) and not sim['vcs'] and sim['scenario'] == 'b'
        ]
        vcs_y_vals_a = [
            sim['FI'] for sim in sim_data if sim['lambda_a'] == (
                2 * sim['lambda_c']) and sim['vcs'] and sim['scenario'] == 'a'
        ]
        vcs_y_vals_b = [
            sim['FI'] for sim in sim_data if sim['lambda_a'] == (
                2 * sim['lambda_c']) and sim['vcs'] and sim['scenario'] == 'b'
        ]
        plt.plot(x_vals,
                 y_vals_a,
                 '-bo',
                 linewidth=2.0,
                 markersize=10,
                 label='Scenario A CSMA')
        plt.plot(x_vals,
                 vcs_y_vals_a,
                 '-rs',
                 linewidth=2.0,
                 markersize=10,
                 label='Scenario A CSMA w. Virtual Sensing')
        plt.plot(x_vals,
                 y_vals_b,
                 '-g+',
                 linewidth=2.0,
                 markersize=10,
                 label='Scenario B CSMA')
        plt.plot(x_vals,
                 vcs_y_vals_b,
                 '-kx',
                 linewidth=2.0,
                 markersize=10,
                 label='Scenario B CSMA w. Virtual Sensing')
        plt.legend(loc=2, markerscale=0.8, prop={'size': 8})
        plt.xlim((45, 305))
        plt.ylabel(r'$T$ (Kbps)')
        plt.xlabel(r'$\lambda$ (frames/sec)')
        plt.title(r'3.b Fairness Index when $\lambda$A = 2$\lambda$C')
        plt.savefig('fig3-b' + str(run_number) + '.png')
        #plt.show()
        plt.close('all')
        print 'DONE\n'
Ejemplo n.º 21
0
def fit_spec_loader(path: str,
                    filename: str,
                    mask_dict: dict = DEF_MASK_DICT) -> Spectrum:
    """
    Loads a FIT spectrum file from SDSS DR 7 or lower.  Converts it into Spectrum type.

    Note: error_dict has the actual mask values as keys.  Loader will iterate through these keys
    and delete any points where these keys are found.  The dict format is an artifact where the values attached
    to each key are the SDSS error names in text.

    :param path: /path/to/file
    :param filename: filename.fits
    :param mask_dict: Defaults to DEF_ERR_DICT defined in this file if not passed
    :type path: str
    :type filename: str
    :type mask_dict: dict
    :rtype: Spectrum
    """
    from astropy.io.fits import getheader, getdata
    from fileio.utils import fileCheck, join
    from catalog import shenCat

    fileCheck(path, filename)

    shenCat.load()
    infile = join(path, filename)

    # Assemble basic info from the header
    # Check if the HW redshift is included in the shenCat.  If so, assign it,
    # otherwise use the one in the file
    header = getheader(infile, 0)
    namestring = "%05i-%04i-%03i" % (header['MJD'], header['PLATEID'],
                                     header['FIBERID'])
    z = shenCat.subkey(namestring, 'z') if namestring in shenCat else float(
        header['z'])
    gmag = float(header['MAG'].split()[1])  # Stored as UGRIZ

    data = getdata(infile, 0)
    flux_data = data[0].tolist(
    )  # first apertrure is the calibrated spectrum flux density
    # data[ 1 ] is the continuum-subtracted spectrum.  Not of interest
    err_data = data[2].tolist()  # third is the +/- of flux denisty
    mask_data = data[3].tolist()  # error mask

    # Wavelength values are not stored in FIT files.  Only three values are available, and these are used to
    # generate the wavelengths which correspond to the pixels
    #   i.e. wl[ pixel 0 ] -> flux density[ 0 ], error[ 0 ], mask[ 0 ], etc
    #
    # Those 3 values are:
    #   naxis1 : number of pixels stored
    #   coeff0 : Log10 of the first wavelength
    #   coeff1 : Log10 of the dispersion coefficient
    #
    # Log10( wavelengths ) are generated by the function:   log_wl_n( n ) = c0 + c1 * n
    # where n is the nth pixel
    # Then the wavelength, in angstroms is given 10^(log_wl_n)
    c0 = header['coeff0']
    c1 = header['coeff1']
    num_pixels = header['naxis1']
    # The actual wavelength generation happens here
    wavelengths = [pow(10, c0 + c1 * n) for n in num_pixels]

    out_spec = Spectrum(namestring=namestring, z=z, gmag=gmag)
    out_spec.setDict(wavelengths, flux_data, err_data)

    # Mask out the errors
    for i in range(len(err_data)):
        if __bit_mask(mask_data[i], mask_dict):
            del out_spec[wavelengths[i]]
    return out_spec
Ejemplo n.º 22
0
def collapse_rfs(edge_indices):
    """Docstring"""

    # get response functions
    responses = response_data()

    # check edge_indices are a list
    assert type(edge_indices) is list

    # check that the indices are in increasing order
    pass

    # check that the indices are in the rfs
    pass

    # add a zero to the front of the edge_indices
    edge_indices.insert(0, 0)

    # loop through rfs
    for name, response in responses.items():

        # pull out the data
        edges, values, error = response.edges, response.int, response.int_error

        # create new structures for the values and errors
        new_edges = np.zeros(len(edge_indices))
        new_values, new_error = [
            np.zeros(len(edge_indices) - 1) for _ in range(2)
        ]

        # calculate the weights of each bin
        weights = response.widths

        # sum over the bounds with the weighting
        for i in range(len(edge_indices) - 1):

            # put the edge in the new edges
            new_edges[i] = edges[edge_indices[i]]

            # find the fraction of the total response that makes up the bin
            total_fraction = np.sum(
                weights[edge_indices[i]:edge_indices[i + 1] + 1])

            # calculate the weighted average and store
            new_values[i] = np.sum(
                values[edge_indices[i]:edge_indices[i + 1] + 1] *
                weights[edge_indices[i]:edge_indices[i + 1] +
                        1]) / total_fraction

            # root sum squared of the errors
            new_error[i] = np.sqrt(
                np.sum((error[edge_indices[i]:edge_indices[i + 1] + 1] *
                        weights[edge_indices[i]:edge_indices[i + 1] + 1])**
                       2)) / total_fraction

        # add the final edge (missed by the looping)
        new_edges[-1] = edges[edge_indices[-1]]

        # store the collapsed data
        responses[name] = Spectrum(new_edges,
                                   new_values,
                                   new_error,
                                   form='int')

    # return the data
    return responses
Ejemplo n.º 23
0
    def get_spectra(self):
        import util

        xpeaks = self.__find_peaks()

        image_length = self.image_data.shape[1]

        start_pixel = 1000
        yvalues_at_start = xpeaks[start_pixel]
        self.num_spectra = len(yvalues_at_start)
        xthreshold = 5
        ythreshold = 2
        cur_num_spectra = 0

        # Going from right to left
        for num, y in enumerate(yvalues_at_start):
            cur_y = y
            s = Spectrum([], [], self)

            cur_x = start_pixel
            for next_spec_x in range(start_pixel + 1, image_length):

                check_y = xpeaks[next_spec_x]
                # Check for xpixels to see if there exists a y pixel that's less
                # than some value away.
                spec_indices = np.where(abs(cur_y - check_y) <= ythreshold)[0]

                if len(spec_indices) > 0:
                    next_ind = spec_indices[0]
                    nexty = check_y[next_ind]
                    s.add_peak(next_spec_x, nexty)
                    cur_x = next_spec_x
                    cur_y = nexty

                if abs(next_spec_x - cur_x) >= xthreshold:
                    break

            cur_x = start_pixel
            cur_y = y
            for prev_spec_x in range(start_pixel - 1, 0, -1):

                check_y = xpeaks[prev_spec_x]
                spec_indices = np.where(abs(cur_y - check_y) <= ythreshold)[0]

                if len(spec_indices) > 0:
                    prev_ind = spec_indices[0]
                    prevy = check_y[prev_ind]
                    s.add_peak(prev_spec_x, prevy)
                    cur_x = prev_spec_x
                    cur_y = prevy

                if abs(prev_spec_x - cur_x) >= xthreshold:
                    break

            build_prep_success = s.build_prepare()
            if build_prep_success:
                cur_num_spectra += 1
                self.spectra.append(s)
                print("Spectrum %d/%d ready for building..." %
                      (cur_num_spectra, self.num_spectra))
            else:
                self.num_spectra -= 1

        self.__fit_overlap_boundary_parabola()
        self.__update_spectral_boundaries()

        built_spectra = []
        cur_num_spectra = 0

        for spectrum in self.spectra:

            build_success = spectrum.build()

            if build_success:
                cur_num_spectra += 1
                built_spectra.append(spectrum)
                print("Building spectrum %d/%d" %
                      (cur_num_spectra, self.num_spectra))
                print("Min x:", spectrum.xvalues[0], "\nMax x:",
                      spectrum.xvalues[-1])
            else:
                self.num_spectra -= 1

        self.spectra = built_spectra
import argparse
from spectrum import Spectrum
parser = argparse.ArgumentParser(description="A script to demonstrate an SDSS spectrum fits file handler.",\
								usage="handler_demo.py --filename spec-xxxx-xxxxx-xxxx.fits")

parser.add_argument("-f", "--filename", help="The spectrum fits file to read.", default=10, required=True)
args = parser.parse_args()

spectrum_file = args.filename

spectrum = Spectrum(spectrum_file)

object_type = spectrum.object_type
redshift = spectrum.redshift.z
velocity = spectrum.redshift.velocity
distance = spectrum.redshift.distance
print(f"\n-------------\nObject Type\n-------------\n{object_type}")
print(f"\n--------\nRedshift\n--------\n{redshift}")
print(f"\n------------------------\nVelocity (from redshift)\n------------------------\n{velocity}")
print(f"\n------------------------\nDistance (from redshift)\n------------------------\n{distance}")
print("\n-----------\nLuminosity\n-----------")
for l in spectrum.luminosity[:5]:
	print(l)
print("\n----\nInfo\n----")
print(spectrum.display_info())

print("\n---------\nHeader 1\n---------")
print(spectrum.display_headers(1))

<<<<<<< HEAD
#spectrum.plot_spectrum(show=True, plotlines=None) # for no lines
Ejemplo n.º 25
0
 def make_shot_list(self, shot_l):
     for i in range(0, len(shot_l)):
         tmp_spec = Spectrum()
         tmp_spec.populate(int(shot_l[i][0:8]), int(shot_l[i][9:]),
                           'black_comet_200_600', 'txt_file')
         self.shot_list.append(tmp_spec)
else:
    in_field = gaussian(X, Y, 1.0, w, x0=0, y0=0).astype(np.complex64)
in_field /= np.sqrt(power(in_field))

ISL = 3e8 / (2 * (d1 + d2))  # Free Spectral Range

print('Computing fields.')
fields = get_fields(np.sqrt(1 - R) * in_field, cavity_fun,
                    50)  # take at least 2*finesse
print('Fields computed.')
print('Memory used : {:.0f} Mb'.format(fields.nbytes / 1e6))

r = N // 2**7
reduced_fields = np.copy(
    fields[::r, ::r, :]) * r  # We reduce the number of points, so renormalize
s = Spectrum(lambda phase: power_at_phase(reduced_fields, phase)
             )  # faster to compute the spectrum than using
# all the preceeding points

print('Computing resonances.')
resonance_phases = s.compute_resonances(n_res=1, gain_thres=0.3)
print('Resonances computed.')

limits = (resonance_phases[0] - np.pi, resonance_phases[0] + np.pi)
phases, spectrum = s.spectrum()
print('resonances = ', resonance_phases)

# *** Plot some cuts of the cavity fields ***
plt.rcParams['figure.figsize'] = (14.0, 6.0)
plt.rcParams['font.size'] = 18

Ejemplo n.º 27
0
    #sets the y variable as the 4th column and x as the second column of the data
    y = data[1:, 4]
    x = data[:, 1]

    #function that locates the peaks of the data
    peak_indices = signal.find_peaks_cwt(y,
                                         np.arange(1, 150),
                                         min_length=100,
                                         noise_perc=50)
    peak_x = []
    peak_y = []
    for i in peak_indices:
        peak_x.append(x[i])
        peak_y.append(data[i, 4])

    graph = Spectrum(x, y)

    plt.plot(graph.stepu_x, graph.stepu_y)
    plt.plot(peak_x, peak_y, 'ko')
    plt.xlabel('Energy')
    plt.ylabel('Net CR')
    plt.suptitle(foil)
    plt.show()

    #sums the area under each peak and relays that with the given energy peak
    peak_data = []
    for i in peak_indices:
        peak_data.append((x[i], np.sum(y[i - 20:i + 20])))
    for d in peak_data:
        print(d)
Ejemplo n.º 28
0
import matplotlib.pyplot as plt
from spectrum import Spectrum

plt.rc('text', usetex=True)
plt.rc('font', family='serif', size=15)

line = 'H1215'
#line = 'OVI1031'

spectrum_file_uvb = f'/disk04/sapple/cgm/absorption/ml_project/data/normal/m100n1024_s50_151/sample_galaxy_195_{line}_0_deg_0.25r200.h5'
spectrum_file_no_uvb = f'/disk04/sapple/cgm/absorption/ml_project/data/no_uvb/m100n1024_s50_151/sample_galaxy_195_{line}_0_deg_0.25r200.h5'

spec_uvb = Spectrum(spectrum_file_uvb)
spec_no_uvb = Spectrum(spectrum_file_no_uvb)

fig, ax = plt.subplots(2, 1, figsize=(10, 10))
ax = ax.flatten()

ax[0].plot(spec_uvb.velocities, np.log10(spec_uvb.taus), label='Collisional + UVB')
ax[0].legend(loc=1)
ax[0].set_ylabel(r'$\tau $')
ax[1].plot(spec_no_uvb.velocities, np.log10(spec_no_uvb.taus), label='Collisional only')
ax[1].legend(loc=4)
ax[1].set_xlabel('Velocity (km/s)')
ax[1].set_ylabel(r'$\tau $')
if line == 'H1215':
    ax[0].set_ylim(-6, 6)
    ax[1].set_ylim(-6, 6)
elif line == 'OVI1031':
    ax[0].set_ylim(-15, 2)
    ax[1].set_ylim(-15, 2)
Ejemplo n.º 29
0
#!/usr/bin/python env

import sys
sys.path.append('../')
from spectrum import Spectrum

example_spectrum = '../test/spectrum.fits'

a = Spectrum(example_spectrum)

###
# Begin

print('\n\nThis is a demo for the Spectrum class.')

print('\n\n###################')
print('# Initialisation  #')
print('###################')

print(f'\nThe SDSS spectrum fits file "{example_spectrum}" \n'+ \
          'has been loaded into the Spectrum class and \n'+ \
          'initiated using the variable "a":\n\n'+ \
          f'>>>a = Spectrum("{example_spectrum}")')

print('\nSeveral variables from the header are already \n'+ \
         'initialised and can be called as follows:\n'+ \
         '>>>a.ra\n'+ \
         '>>>a.dec\n'+ \
         '>>>a.mjd')

print('')
Ejemplo n.º 30
0
def multi_readout_analyze(folder, ccd_height=100., plot=True, freq=None):
    """Analyze several readout measurements in different files for readout 
    diagnosys
    
    The readout files in dm3 format must be contained in a folder, preferentely 
    numered in the order of acquisition.
    
    Parameters
    ----------
    folder : string
        Folder where the dm3 readout files are stored
    ccd_heigh : float
    plot : bool
    freq : float
        Frequency of the camera
    
    Returns
    -------
    Dictionary
    """
    from spectrum import Spectrum
    files = glob.glob1(folder, '*.nc')
    if not files:
        files = glob.glob1(folder, '*.dm3')
    spectra = []
    variances = []
    binnings = []
    for f in files:
        print os.path.join(folder, f)
        s = Spectrum(os.path.join(folder, f))
        variance, channel_mean, norm_time_mean = analyze_readout(s)
        s.readout_analysis = {}
        s.readout_analysis['variance'] = variance.mean()
        s.readout_analysis['pattern'] = channel_mean
        s.readout_analysis['time'] = norm_time_mean
        if not hasattr(s, 'binning'):
            s.binning = float(os.path.splitext(f)[0][1:])
            if freq:
                s.readout_frequency = freq
                s.ccd_height = ccd_height
            s.save(f)
        spectra.append(s)
        binnings.append(s.binning)
        variances.append(variance.mean())
    pixels = ccd_height / np.array(binnings)
    plt.scatter(pixels, variances, label='data')
    fit = np.polyfit(pixels, variances, 1, full=True)
    if plot:
        x = np.linspace(0, pixels.max(), 100)
        y = x * fit[0][0] + fit[0][1]
        plt.plot(x, y, label='linear fit')
        plt.xlabel('number of pixels')
        plt.ylabel('variance')
        plt.legend(loc='upper left')

    print "Variance = %s * pixels + %s" % (fit[0][0], fit[0][1])
    dictio = {
        'pixels': pixels,
        'variances': variances,
        'fit': fit,
        'spectra': spectra
    }
    return dictio