<commit_before>import networkx as nx
import matplotlib.pyplot as plt
import re

class ProgramSection:
    def __init__(self, label):
        self.label = label
        self.code_lines = []

    def add_code_line(self, line):
        self.code_lines.append(line)

    def __hash__(self):
        return self.label.__hash__()

"""
Three methods:
    add_label('label')
    place_pointer('label')
    split_node_connect('label') -> Splits node that pointer is at into nodes A and B, and creates edge from A to B. A is original node; then it adds a connection from A to 'label' and moves the pointer to B.
    add_edge('label') adds an edge from the node that pointer is currently at to label
    add_code_line(line) adds a line of code to the node that the pointer points to.
"""

class ProgramSectionGraph:
    def __init__(self):
        self.graph = nx.DiGraph()
        self.pointer = None
        #maps label to ProgramSection instance
        self.sectionMap = dict()
    def place_pointer(self, label):
        self.pointer = label

    #takes instance of ProgramSection
    def add_section(self, label):
        section = ProgramSection(label)
        self.sectionMap[label] = section
        self.graph.add_node(section)

    def split_node_connect(self, label):
        #this is B, the result of spliting the node.
        #this is usually in the context of branches.
        child_section = ProgramSection(self.pointer + '*')
        self.sectionMap[self.pointer + '*'] = child_section
        self.graph.add_node(child_section)
        self.graph.add_edge(self.pointer, self.pointer + '*')
        self.graph.add_edge(self.pointer, label)
        self.pointer = self.pointer + '*'

    def add_edge(self, label):
        self.graph.add_edge(self.pointer, label)

    def add_code_line(self, line):
        print(line)
        print(self.pointer)
        print(self.sectionMap)
        self.sectionMap[self.pointer].add_code_line(line)
    #return networkx object
    def getNetworkXGraph(self):
        return self.graph
#pass in text of assembly
#returns an instance of the ProgramSectionGraph class.
def getGraph(mipsCode):

    #first, remove all whitespace from the start and end of the lines
    mipsLines = map(lambda x: x.strip(), mipsCode.split('\n'))

    #for each line, remove any section that comes after a '#'.
    comment = re.compile('#')
    mipsLines = map(lambda x: comment.split(x)[0], mipsLines)
    #remove the lines that are empty
    mipsLines = filter(lambda x: len(x) > 0, mipsLines)
    #for each line, remove
    #now we want to go through the lines, and group them by labels
    labeledCode = []
    currentLabel = None
    currentCode = []
    #assume only one label per line -- I'm not sure if this is in the MIPS specs, but if it becomes a problem, I will deal with it later.
    for line in mipsLines:
        if ':' in line:
            if currentLabel != None:
                labeledCode.append((currentLabel, currentCode))

            splitLine = line.split(':')
            currentLabel = splitLine[0]
            currentCode = []
            if len(splitLine) > 1:
                currentCode.append(splitLine[1])

        else:
            if currentLabel != None:
                currentCode.append(line)
    if currentLabel != None:
        labeledCode.append((currentLabel, currentCode))

    #add the labels as nodes to the graph
    graph = ProgramSectionGraph()
    for (label, code) in labeledCode:
        graph.add_section(label)

    #now that we have a labels, we need to go through, and detect branches and jumps
    #start from the main label.
    bjRegex = re.compile('(?:(?:beq|bne)\s+[^,-]*,[^,-]*,\s+(.*))|(?:(?:bgez|bgtz|blez|bltz)\s+[^,-]*,\s+(.*)]|(j|jal|jr|jalr|)\s+(.*))')
    for (label, code) in labeledCode:
        graph.place_pointer(label)
        for line in code:
            match = bjRegex.match(line)
            if match == None:
                #then not a jump or branch
                graph.add_code_line(line)
            else:
                #so if it's a branch, then do the split_node_connect
                if re.match('\s+b', line) != None:
                    graph.split_node_connect(match.group(1))
                elif re.match('\s+j', line) != None:
                    graph.add_edge(match.group(1))

    return graph

with open('simple.asm', 'r') as asmFile:
    asmText = asmFile.read()

    graph = getGraph(asmText).getNetworkXGraph()
    nx.draw(graph)

    plt.savefig('simple.png')

<commit_msg>It's outputting a reasonable graph, but I'm not sure if it's correct. Need to add labels next to nodes<commit_after>import networkx as nx
import matplotlib.pyplot as plt
import re

class ProgramSection:
    def __init__(self, label):
        self.label = label
        self.code_lines = []

    def add_code_line(self, line):
        self.code_lines.append(line)

    def __hash__(self):
        return self.label.__hash__()

"""
Three methods:
    add_label('label')
    place_pointer('label')
    split_node_connect('label') -> Splits node that pointer is at into nodes A and B, and creates edge from A to B. A is original node; then it adds a connection from A to 'label' and moves the pointer to B.
    add_edge('label') adds an edge from the node that pointer is currently at to label
    add_code_line(line) adds a line of code to the node that the pointer points to.
"""

class ProgramSectionGraph:
    def __init__(self):
        self.graph = nx.DiGraph()
        self.pointer = None
        #maps label to ProgramSection instance
        self.sectionMap = dict()
    def place_pointer(self, label):
        self.pointer = label

    #takes instance of ProgramSection
    def add_section(self, label):
        section = ProgramSection(label)
        self.sectionMap[label] = section
        self.graph.add_node(section)

    def split_node_connect(self, label):
        #this is B, the result of spliting the node.
        #this is usually in the context of branches.
        child_section = ProgramSection(self.pointer + '*')
        self.sectionMap[self.pointer + '*'] = child_section
        self.graph.add_node(child_section)
        self.graph.add_edge(self.pointer, self.pointer + '*')
        self.graph.add_edge(self.pointer, label)
        self.pointer = self.pointer + '*'

    def add_edge(self, label):
        self.graph.add_edge(self.pointer, label)

    def add_code_line(self, line):
        self.sectionMap[self.pointer].add_code_line(line)
    #return networkx object
    def getNetworkXGraph(self):
        return self.graph
#pass in text of assembly
#returns an instance of the ProgramSectionGraph class.
def getGraph(mipsCode):

    #first, remove all whitespace from the start and end of the lines
    mipsLines = map(lambda x: x.strip(), mipsCode.split('\n'))

    #for each line, remove any section that comes after a '#'.
    comment = re.compile('#')
    mipsLines = map(lambda x: comment.split(x)[0], mipsLines)
    #remove the lines that are empty
    mipsLines = filter(lambda x: len(x) > 0, mipsLines)
    #for each line, remove
    #now we want to go through the lines, and group them by labels
    labeledCode = []
    currentLabel = None
    currentCode = []
    #assume only one label per line -- I'm not sure if this is in the MIPS specs, but if it becomes a problem, I will deal with it later.
    for line in mipsLines:
        if ':' in line:
            if currentLabel != None:
                labeledCode.append((currentLabel, currentCode))

            splitLine = line.split(':')
            currentLabel = splitLine[0]
            currentCode = []
            if len(splitLine) > 1:
                currentCode.append(splitLine[1])

        else:
            if currentLabel != None:
                currentCode.append(line)
    if currentLabel != None:
        labeledCode.append((currentLabel, currentCode))

    #add the labels as nodes to the graph
    graph = ProgramSectionGraph()
    for (label, code) in labeledCode:
        graph.add_section(label)

    #now that we have a labels, we need to go through, and detect branches and jumps
    #start from the main label.
    bjRegex = re.compile('(?:(?:beq|bne)\s+[^,-]*,[^,-]*,\s+(.*))|(?:(?:bgez|bgtz|blez|bltz)\s+[^,-]*,\s+(.*)]|(j|jal|jr|jalr|)\s+(.*))')

    for (label, code) in labeledCode:
        graph.place_pointer(label)

        for line in code:

            match = bjRegex.match(line)
            if match == None:
                #then not a jump or branch
                graph.add_code_line(line)
            else:
                #so if it's a branch, then do the split_node_connect
                if line.startswith('b'):
                    graph.split_node_connect(match.group(1))
                elif line.startswith('j'):
                    print('jumping')
                    print(line)
                    graph.add_edge(match.group(1))

    return graph

with open('simple.asm', 'r') as asmFile:
    asmText = asmFile.read()

    graph = getGraph(asmText).getNetworkXGraph()
    nx.draw(graph)

    plt.savefig('simple.png')
    print('graph')

<|endoftext|>


<commit_before>import smbus
import time
import math

class MPU:
    """ Class that get the Gyro, Acell and temperature from a MPU6050
    this module is based on:
    1) https://www.filipeflop.com/blog/tutorial-acelerometro-mpu6050-arduino/
    2) https://www.sunfounder.com/learn/sensor-kit-v2-0-for-raspberry-pi-b-plus
    /lesson-32-mpu6050-gyro-acceleration-sensor-sensor-kit-v2-0-for-b-plus.html
    """

    def __init__(self):
        self.bus = smbus.SMBus(1)
        self.address = 0x68  # i2cdetect -y 1
        self.bus.write_byte_data(self.address, 0x6b, 0)  # "Wake sensor"
        # https://playground.arduino.cc/Main/MPU-6050

    def __getXYZ(self, adr):
        xyz = []
        for i in range(3):
            high = self.bus.read_byte_data(self.address, adr + i * 2)
            low = self.bus.read_byte_data(self.address, adr + i * 2 + 1)
            val = (high << 8) + low
            if (val >= 0x8000):
                val = -((65535 - val) + 1)
            xyz.append(val)
        return xyz

    def getGyro(self):
        adr = 0x43
        gyro = self.__getXYZ(adr)
        return gyro

    def getAccel(self):
        adr = 0x3B
        acell = self.__getXYZ(adr)
        return acell

    def getTemp(self):
        high = self.bus.read_byte_data(self.address, 0x41)
        low = self.bus.read_byte_data(self.address, 0x42)
        val = (high << 8) + low
        val2 = (val / 340.00) + 36.53
        return val2, val, high, low

mpu = MPU()

while True:
    print "Acell: ", mpu.getAccel()
    print "Temp: ", mpu.getTemp()
    time.sleep(0.5)

<commit_msg>Update temperature and Accel scale<commit_after>import smbus
import time

class MPU:
    """ Class that get the Gyro, Acell and temperature from a MPU6050
    this module is based on:
    1) https://www.filipeflop.com/blog/tutorial-acelerometro-mpu6050-arduino/
    2) https://www.sunfounder.com/learn/sensor-kit-v2-0-for-raspberry-pi-b-plus
    3) https://github.com/Tijndagamer/mpu6050/blob/master/mpu6050/mpu6050.py
    /lesson-32-mpu6050-gyro-acceleration-sensor-sensor-kit-v2-0-for-b-plus.html
    """
    ACCEL_SCALE_MODIFIER_2G = 16384.0
    ACCEL_SCALE_MODIFIER_4G = 8192.0
    ACCEL_SCALE_MODIFIER_8G = 4096.0
    ACCEL_SCALE_MODIFIER_16G = 2048.0
    GRAVITIY_MS2 = 9.80665

    def __init__(self):
        self.bus = smbus.SMBus(1)
        self.address = 0x68  # i2cdetect -y 1
        self.bus.write_byte_data(self.address, 0x6b, 0)  # "Wake sensor"
        # https://playground.arduino.cc/Main/MPU-6050
        self.accelConfig = 0x1C
        self.accelRang = self.bus.read_byte_data(self.address, self.accelConfig)

    def __getXYZ(self, adr):
        xyz = []
        for i in range(3):
            high = self.bus.read_byte_data(self.address, adr + i * 2)
            low = self.bus.read_byte_data(self.address, adr + i * 2 + 1)
            val = (high << 8) + low
            if (val >= 0x8000):
                val = -((65535 - val) + 1)
            xyz.append(val)
        return xyz

    def getGyro(self):
        adr = 0x43
        gyro = self.__getXYZ(adr)
        return gyro

    def getAccel(self):
        adr = 0x3B
        acell = self.__getXYZ(adr)
        acell[0] = (self.GRAVITIY_MS2 * acell[0]) / self.ACCEL_SCALE_MODIFIER_2G
        acell[1] = (self.GRAVITIY_MS2 * acell[1]) / self.ACCEL_SCALE_MODIFIER_2G
        acell[2] = (self.GRAVITIY_MS2 * acell[2]) / self.ACCEL_SCALE_MODIFIER_2G
        return acell

    def getTemp(self):
        high = self.bus.read_byte_data(self.address, 0x41)
        low = self.bus.read_byte_data(self.address, 0x42)
        val = (high << 8) + low
        if (val >= 0x8000):
            val = -((65535 - val) + 1)
        val2 = (val / 340.00) + 36.53

        return val2

mpu = MPU()

while True:
    print "Acell: ", mpu.getAccel()
    print "Temp: ", mpu.getTemp()
    time.sleep(0.5)
<|endoftext|>


<commit_before>"""
SAGA package version
"""
__version__ = '0.10.19'
<commit_msg>version increment<commit_after>"""
SAGA package version
"""
__version__ = '0.10.20'
<|endoftext|>


<commit_before>import pathlib2 as pl
import shutil
import numpy as np
import sys
import tarfile
import PolyLibScan.Tools.config as cfg
import PolyLibScan.Database.db as DB
import parser
import compute

class JobSave(object):

    __git_hash__ = sys.modules['PolyLibScan'].__git_hash__

    def __init__(self, paths, db_name='jobdata.h5', overwrite=False):
        self.path = self._set_paths(paths)
        setup_path = self.path['root'].joinpath('config_with_setup.yml')
        self.config = cfg.JobConfig(setup_path.as_posix())
        self.parse = parser.Parser()
        self.db_path = self.path['root'].joinpath(db_name)
        if overwrite:
            self.db = DB.JobDataBase(self.db_path, mode='w')
        else:
            self.db = DB.JobDataBase(self.db_path, mode='a')
        self.db.create_groups()
        self.saved = False
        self.runs = self.read_runs()

    def _set_paths(self, input_paths):
        path = {dir_name: pl.Path(input_paths[dir_name])
                        for dir_name in ['input', 'output', 'logs',
                                         'fifo', 'root', 'local_root']}
        path['meta'] = path['root'].joinpath('config_with_setup.yml')
        path['p_list'] = path.joinpath('particle_list.npy')
        return path

    def save(self):
        self.save_meta_data(self.config)
        try:
            self.save_trajectory_meta(self.runs[0].path['full_traj'].as_posix())
        except:
            print self.path
            raise Exception('we better stop here.')
        self.save_particle_list(self.path['p_list'].as_posix())
        self.save_runs(self.runs)
        self.save_endstates(self.runs)
        self.saved = True

    def save_trajectory_meta(self, path):
        type_list, step_size = self.parse.trajectory_meta(path)
        traj_info = self.parse.traj_info(path)
        self.db.traj_type_order = type_list
        self.db.traj_info = traj_info

    def save_meta_data(self, config):
        meta_data = self.parse.meta(config)
        self.db.sequence = meta_data['sequence']
        self.db.weights = meta_data['weights']
        self.db.misc = meta_data['misc']
        self.db.active_site = meta_data['active_site']
        self.db.parameter = meta_data['parameter']

    def save_particle_list(self, path):
        particle_data = self.parse.particle_list(path)
        self.db.particle_list = particle_data

    def save_runs(self, runs):
        polymer_ids = np.array(np.unique(self.db.sequence['ID']))
        active_site_pos = self.db.active_site['xyz']
        for run in runs:
            self.db.start_trajectories_save(run.start_traj, run.Id)
            self.db.end_trajectories_save(run.end_traj, run.Id)
            if 'full_traj' in run.path:
                data = self.parse.trajectory(run.path['full_traj'].as_posix())
                self.db.trajectory_save(data['xyz'], run.Id)
            # Save Energy timeseries
            self.db.energie_ts_save(run.energy, run.Id)
            # Save Distance Timeseries
            if 'distance' in run.path:
                self.db.distance_ts_save(run.distance, run.Id)

    def save_endstates(self, runs):
        polymer_ids = np.array(np.unique(self.db.sequence['ID']))
        active_site_pos = self.db.active_site['xyz']
        endstates = np.zeros(len(runs), dtype=[('ID', '>i2'),
                                               ('Energy', '>f4'),
                                               ('TotalEnergy', '>f4'),
                                               ('Distance', '>f4')])
        for run in runs:
            distance = compute.distance_to_active_site(run.end_traj, polymer_ids,
                                                       active_site_pos)
            endstates[run.Id] = (run.Id, run.energy[-1,0], run.energy[-1,1], distance)
        self.db.end_states = endstates

    def read_runs(self):
        runs = []
        input_files = self.path['input'].glob('*')
        return [Run.from_id(self, self.path, input_file.name)
                for input_file in input_files]

    def get_Error(self):
        if self.has_error():
            with open(self.path['logs'].joinpath('slurm.err').as_posix()) as f:
                return f.read()
        else:
            return 'No error occurred.'

    def has_error(self):
        '''Check if the job ran through correctly by looking at
        last line.
        '''
        slurm_error_file = self.path['logs'].joinpath('slurm.err')
        #Check last line
        if slurm_error_file.exists():
            with open(slurm_error_file.as_posix()) as f:
                last_line = f.readlines()[-1].strip()
            if last_line.endswith('Finished simulations. Starting to clean up.Not'):
                return True
        return False

    def save_versions(self, lmp_version=None):
        versions = {'PolyLibScan': str(self.__git_hash__)}
        if lmp_version:
            versions['LAMMPS'] = str(lmp_version)
        else:
            versions['LAMMPS'] = ''
        data = self.parse.version(versions)
        self.db.versions = data

    def remove_local(self):
        shutil.rmtree(self.config.lmp_path['local_root'])

    def clean_up(self, force=False):
        if self.saved or force:
            for run in self.runs:
                run.cleanup()

        if not self.has_error():
            for file_ in ['log.lammps', 'particle_list.npy']:
                if self.path['root'].joinpath(file_).exists():
                    self.path['root'].joinpath(file_).unlink()
            for file_ in ['slurm.out','slurm.err']:
                if self.path['logs'].joinpath(file_).exists():
                    self.path['logs'].joinpath(file_).unlink()
            for dir_ in ['input', 'output', 'logs', 'fifo']:
                if self.path[dir_].exists():
                    self.path[dir_].rmdir()
            if self.config.sim_parameter['local'] == 1:
                self.remove_local()

class Run(object):

    @classmethod
    def from_id(cls, job, path, str_ID):
        run_id = int(str_ID)
        out_path = path['output']
        in_path = path['input']
        start_file = in_path.joinpath(str_ID)
        energy = out_path.joinpath('Energy'+str_ID)
        start_traj = out_path.joinpath('trajectoryS%s.xyz' % str_ID)
        end_traj = out_path.joinpath('trajectoryE%s.xyz' % str_ID)
        distance_file = out_path.joinpath('distance_as_polymer%s' % str_ID)
        if not distance_file.exists():
            distance_file = None
        traj_file = out_path.joinpath('full_trajectory%s.xyz.gz' % str_ID)
        if not traj_file.exists():
            traj_file = None
        return cls(job, run_id, start_file, energy, start_traj, end_traj, distance_file, traj_file)

    def __init__(self, job, ID, start_file, energy, start_traj, end_traj, distance=None, full_traj=None):
        self.parent = job
        self.Id = ID
        self.path = {}
        self.path['start_file'] = start_file
        self.path['energy'] =  energy
        self.path['start_traj'] = start_traj
        self.path['end_traj'] = end_traj
        if distance:
            self.path['distance'] = distance
            self.distance = self.parent.parse.distance(self.path['distance'].as_posix())
        if full_traj:
            self.path['full_traj'] = full_traj

        self.start_traj = self.parent.parse.xyz(self.path['start_traj'].as_posix())
        self.end_traj = self.parent.parse.xyz(self.path['end_traj'].as_posix())
        self.energy = self.parent.parse.energy(self.path['energy'].as_posix())

    def cleanup(self):
        for path in self.path.values():
            if path.exists():
                path.unlink()

    def __repr__(self):
        return 'LammpsRun - id %d' % self.Id
<commit_msg>Save: fixed bug...hopefully the last<commit_after>import pathlib2 as pl
import shutil
import numpy as np
import sys
import tarfile
import PolyLibScan.Tools.config as cfg
import PolyLibScan.Database.db as DB
import parser
import compute

class JobSave(object):

    __git_hash__ = sys.modules['PolyLibScan'].__git_hash__

    def __init__(self, paths, db_name='jobdata.h5', overwrite=False):
        self.path = self._set_paths(paths)
        setup_path = self.path['root'].joinpath('config_with_setup.yml')
        self.config = cfg.JobConfig(setup_path.as_posix())
        self.parse = parser.Parser()
        self.db_path = self.path['root'].joinpath(db_name)
        if overwrite:
            self.db = DB.JobDataBase(self.db_path, mode='w')
        else:
            self.db = DB.JobDataBase(self.db_path, mode='a')
        self.db.create_groups()
        self.saved = False
        self.runs = self.read_runs()

    def _set_paths(self, input_paths):
        path = {dir_name: pl.Path(input_paths[dir_name])
                        for dir_name in ['input', 'output', 'logs',
                                         'fifo', 'root', 'local_root']}
        path['meta'] = path['root'].joinpath('config_with_setup.yml')
        path['p_list'] = path['root'].joinpath('particle_list.npy')
        return path

    def save(self):
        self.save_meta_data(self.config)
        try:
            self.save_trajectory_meta(self.runs[0].path['full_traj'].as_posix())
        except:
            print self.path
            raise Exception('we better stop here.')
        self.save_particle_list(self.path['p_list'].as_posix())
        self.save_runs(self.runs)
        self.save_endstates(self.runs)
        self.saved = True

    def save_trajectory_meta(self, path):
        type_list, step_size = self.parse.trajectory_meta(path)
        traj_info = self.parse.traj_info(path)
        self.db.traj_type_order = type_list
        self.db.traj_info = traj_info

    def save_meta_data(self, config):
        meta_data = self.parse.meta(config)
        self.db.sequence = meta_data['sequence']
        self.db.weights = meta_data['weights']
        self.db.misc = meta_data['misc']
        self.db.active_site = meta_data['active_site']
        self.db.parameter = meta_data['parameter']

    def save_particle_list(self, path):
        particle_data = self.parse.particle_list(path)
        self.db.particle_list = particle_data

    def save_runs(self, runs):
        polymer_ids = np.array(np.unique(self.db.sequence['ID']))
        active_site_pos = self.db.active_site['xyz']
        for run in runs:
            self.db.start_trajectories_save(run.start_traj, run.Id)
            self.db.end_trajectories_save(run.end_traj, run.Id)
            if 'full_traj' in run.path:
                data = self.parse.trajectory(run.path['full_traj'].as_posix())
                self.db.trajectory_save(data['xyz'], run.Id)
            # Save Energy timeseries
            self.db.energie_ts_save(run.energy, run.Id)
            # Save Distance Timeseries
            if 'distance' in run.path:
                self.db.distance_ts_save(run.distance, run.Id)

    def save_endstates(self, runs):
        polymer_ids = np.array(np.unique(self.db.sequence['ID']))
        active_site_pos = self.db.active_site['xyz']
        endstates = np.zeros(len(runs), dtype=[('ID', '>i2'),
                                               ('Energy', '>f4'),
                                               ('TotalEnergy', '>f4'),
                                               ('Distance', '>f4')])
        for run in runs:
            distance = compute.distance_to_active_site(run.end_traj, polymer_ids,
                                                       active_site_pos)
            endstates[run.Id] = (run.Id, run.energy[-1,0], run.energy[-1,1], distance)
        self.db.end_states = endstates

    def read_runs(self):
        runs = []
        input_files = self.path['input'].glob('*')
        return [Run.from_id(self, self.path, input_file.name)
                for input_file in input_files]

    def get_Error(self):
        if self.has_error():
            with open(self.path['logs'].joinpath('slurm.err').as_posix()) as f:
                return f.read()
        else:
            return 'No error occurred.'

    def has_error(self):
        '''Check if the job ran through correctly by looking at
        last line.
        '''
        slurm_error_file = self.path['logs'].joinpath('slurm.err')
        #Check last line
        if slurm_error_file.exists():
            with open(slurm_error_file.as_posix()) as f:
                last_line = f.readlines()[-1].strip()
            if last_line.endswith('Finished simulations. Starting to clean up.Not'):
                return True
        return False

    def save_versions(self, lmp_version=None):
        versions = {'PolyLibScan': str(self.__git_hash__)}
        if lmp_version:
            versions['LAMMPS'] = str(lmp_version)
        else:
            versions['LAMMPS'] = ''
        data = self.parse.version(versions)
        self.db.versions = data

    def remove_local(self):
        shutil.rmtree(self.config.lmp_path['local_root'])

    def clean_up(self, force=False):
        if self.saved or force:
            for run in self.runs:
                run.cleanup()

        if not self.has_error():
            for file_ in ['log.lammps', 'particle_list.npy']:
                if self.path['root'].joinpath(file_).exists():
                    self.path['root'].joinpath(file_).unlink()
            for file_ in ['slurm.out','slurm.err']:
                if self.path['logs'].joinpath(file_).exists():
                    self.path['logs'].joinpath(file_).unlink()
            for dir_ in ['input', 'output', 'logs', 'fifo']:
                if self.path[dir_].exists():
                    self.path[dir_].rmdir()
            if self.path['local_root'] == 1:
                self.remove_local()

class Run(object):

    @classmethod
    def from_id(cls, job, path, str_ID):
        run_id = int(str_ID)
        out_path = path['output']
        in_path = path['input']
        start_file = in_path.joinpath(str_ID)
        energy = out_path.joinpath('Energy'+str_ID)
        start_traj = out_path.joinpath('trajectoryS%s.xyz' % str_ID)
        end_traj = out_path.joinpath('trajectoryE%s.xyz' % str_ID)
        distance_file = out_path.joinpath('distance_as_polymer%s' % str_ID)
        if not distance_file.exists():
            distance_file = None
        traj_file = out_path.joinpath('full_trajectory%s.xyz.gz' % str_ID)
        if not traj_file.exists():
            traj_file = None
        return cls(job, run_id, start_file, energy, start_traj, end_traj, distance_file, traj_file)

    def __init__(self, job, ID, start_file, energy, start_traj, end_traj, distance=None, full_traj=None):
        self.parent = job
        self.Id = ID
        self.path = {}
        self.path['start_file'] = start_file
        self.path['energy'] =  energy
        self.path['start_traj'] = start_traj
        self.path['end_traj'] = end_traj
        if distance:
            self.path['distance'] = distance
            self.distance = self.parent.parse.distance(self.path['distance'].as_posix())
        if full_traj:
            self.path['full_traj'] = full_traj

        self.start_traj = self.parent.parse.xyz(self.path['start_traj'].as_posix())
        self.end_traj = self.parent.parse.xyz(self.path['end_traj'].as_posix())
        self.energy = self.parent.parse.energy(self.path['energy'].as_posix())

    def cleanup(self):
        for path in self.path.values():
            if path.exists():
                path.unlink()

    def __repr__(self):
        return 'LammpsRun - id %d' % self.Id
<|endoftext|>


<commit_before>from __future__ import print_function, absolute_import, unicode_literals
import copy
import logging

import sys
import argparse
from osbs import set_logging
from osbs.api import OSBS
from osbs.conf import Configuration
from osbs.constants import BUILD_JSON_STORE, DEFAULT_CONFIGURATION_FILE, DEFAULT_CONFIGURATION_SECTION

try:
    # py2
    from urllib2 import HTTPError
except ImportError:
    # py3
    from urllib.error import HTTPError

logger = logging.getLogger('osbs')

def graceful_chain_get(d, *args):
    t = copy.deepcopy(d)
    for arg in args:
        try:
            t = t[arg]
        except (AttributeError, KeyError):
            return None
    return t

def cmd_list_builds(args, osbs):
    builds = osbs.list_builds(namespace=args.namespace)
    format_str = "{name:48} {status:16} {image:64}"
    print(format_str.format(**{"name": "BUILD NAME", "status": "STATUS", "image": "IMAGE NAME"}), file=sys.stderr)
    for build in builds['items']:
        image = graceful_chain_get(build, 'parameters', 'output', 'imageTag')
        if args.USER:
            if not image.startswith(args.USER + "/"):
                continue
        b = {
            "name": build['metadata']['name'],
            "status": build['status'],
            "image": image,
        }
        print(format_str.format(**b))

def cmd_get_build(args, osbs):
    build_json = osbs.get_build(args.BUILD_ID[0], namespace=args.namespace).json
    # FIXME: pretty printing json could be a better idea
    template = """\
BUILD ID: {build_id}
STATUS: {status}
IMAGE: {image}
DATE: {date}

DOCKERFILE

{dockerfile}

BUILD LOGS

{logs}

PACKAGES

{packages}"""
    context = {
        "build_id": build_json['metadata']['name'],
        "status": build_json['status'],
        "image": graceful_chain_get(build_json, 'parameters', 'output', 'imageTag'),
        "date": build_json['metadata']['creationTimestamp'],
        "dockerfile": graceful_chain_get(build_json, 'metadata', 'labels', 'dockerfile'),
        "logs": graceful_chain_get(build_json, 'metadata', 'labels', 'logs'),
        "packages": graceful_chain_get(build_json, 'metadata', 'labels', 'rpm-packages'),
    }
    print(template.format(**context))

def cmd_build(args, osbs):
    build = osbs.create_build(
        git_uri=args.git_url,
        git_ref=args.git_commit,
        user=args.user,
        component=args.component,
        target=args.target,
        namespace=args.namespace
    )
    build_id = build.build_id
    print("Build submitted (%s), watching logs (feel free to interrupt)" % build_id)
    for line in osbs.get_build_logs(build_id, follow=True):
        print(line)

def cmd_build_logs(args, osbs):
    build_id = args.BUILD_ID[0]
    follow = args.follow

    if follow:
        for line in osbs.get_build_logs(build_id, follow=True, namespace=args.namespace):
            print(line)
    else:
        logs = osbs.get_build_logs(build_id, follow=False, namespace=args.namespace)
        print(logs, end="")

def cmd_watch_build(args, osbs):
    build_json = osbs.wait_for_build_to_finish(args.BUILD_ID[0], namespace=args.namespace)

def cli():
    parser = argparse.ArgumentParser(
        description="OpenShift Build Service client"
    )
    exclusive_group = parser.add_mutually_exclusive_group()
    exclusive_group.add_argument("--verbose", action="store_true", default=None)
    exclusive_group.add_argument("-q", "--quiet", action="store_true")

    subparsers = parser.add_subparsers(help='commands')

    list_builds_parser = subparsers.add_parser('list-builds', help='list builds in OSBS')
    list_builds_parser.add_argument("USER", help="list builds only for specified username",
                                    nargs="?")
    list_builds_parser.set_defaults(func=cmd_list_builds)

    watch_build_parser = subparsers.add_parser('watch-build', help='wait till build finishes')
    watch_build_parser.add_argument("BUILD_ID", help="build ID", nargs=1)
    watch_build_parser.set_defaults(func=cmd_watch_build)

    get_build_parser = subparsers.add_parser('get-build', help='get info about build')
    get_build_parser.add_argument("BUILD_ID", help="build ID", nargs=1)
    get_build_parser.set_defaults(func=cmd_get_build)

    build_logs_parser = subparsers.add_parser('build-logs', help='get or follow build logs')
    build_logs_parser.add_argument("BUILD_ID", help="build ID", nargs=1)
    build_logs_parser.add_argument("-f", "--follow", help="follow logs as they come", action="store_true",
                                   default=False)
    build_logs_parser.set_defaults(func=cmd_build_logs)

    build_parser = subparsers.add_parser('build', help='build an image in OSBS')
    build_parser.add_argument("--build-type", "-T", action="store", metavar="BUILD_TYPE",
                                   help="build type (prod, simple)")
    build_parser.add_argument("--build-json-dir", action="store", metavar="PATH",
                                   help="directory with build jsons")
    build_parser.add_argument("-g", "--git-url", action='store', metavar="URL",
                                   required=True, help="URL to git repo")
    build_parser.add_argument("--git-commit", action='store', default="master",
                                   help="checkout this commit")
    build_parser.add_argument("-c", "--component", action='store', required=True,
                                   help="name of component")
    build_parser.add_argument("-t", "--target", action='store', required=True,
                                   help="koji target name")
    build_parser.add_argument("-u", "--user", action='store', required=True,
                                   help="username (will be image prefix)")
    build_parser.set_defaults(func=cmd_build)

    parser.add_argument("--openshift-uri", action='store', metavar="URL",
                        help="openshift URL to remote API")
    parser.add_argument("--kubelet-uri", action='store', metavar="URL",
                        help="kubelet URL to remote API")
    parser.add_argument("--registry-uri", action='store', metavar="URL",
                        help="registry where images should be pushed")
    parser.add_argument("--config", action='store', metavar="PATH",
                        help="path to configuration file", default=DEFAULT_CONFIGURATION_FILE)
    parser.add_argument("--instance", "-i", action='store', metavar="SECTION_NAME",
                        help="section within config for requested instance", default=DEFAULT_CONFIGURATION_SECTION)
    parser.add_argument("--username", action='store',
                        help="username within OSBS")
    parser.add_argument("--password", action='store',
                        help="password within OSBS")
    parser.add_argument("--use-kerberos", action='store_true',
                        help="use kerberos for authentication")
    parser.add_argument("--verify-ssl", action='store_true', default=True,
                        help="verify CA on secure connections")
    parser.add_argument("--namespace", help="list builds of specific namespace "
                                            "(use --namespace=\"\" to list all builds)",
                        metavar="NAMESPACE", action="store", default="default")
    args = parser.parse_args()
    return parser, args

def main():
    parser, args = cli()
    os_conf = Configuration(conf_file=args.config, conf_section=args.instance, cli_args=args)
    build_conf = Configuration(conf_file=args.config, conf_section=args.instance, cli_args=args)

    if os_conf.get_verbosity():
        set_logging(level=logging.DEBUG)
        logger.debug("Logging level set to debug")
    elif args.quiet:
        set_logging(level=logging.WARNING)
    else:
        set_logging(level=logging.INFO)

    osbs = OSBS(os_conf, build_conf)

    try:
        args.func(args, osbs)
    except AttributeError as ex:
        if hasattr(args, 'func'):
            raise
        else:
            parser.print_help()
    except KeyboardInterrupt:
        print("Quitting on user request.")
        return -1
    except HTTPError as ex:
        logger.error("HTTP error: %d", ex.getcode())
        return -1
    except Exception as ex:
        if args.verbose:
            raise
        else:
            logger.error("Exception caught: %s", repr(ex))
            return -1

if __name__ == '__main__':
    sys.exit(main())
<commit_msg>cli: use conf's is_verbose, not args'<commit_after>from __future__ import print_function, absolute_import, unicode_literals
import copy
import logging

import sys
import argparse
from osbs import set_logging
from osbs.api import OSBS
from osbs.conf import Configuration
from osbs.constants import BUILD_JSON_STORE, DEFAULT_CONFIGURATION_FILE, DEFAULT_CONFIGURATION_SECTION

try:
    # py2
    from urllib2 import HTTPError
except ImportError:
    # py3
    from urllib.error import HTTPError

logger = logging.getLogger('osbs')

def graceful_chain_get(d, *args):
    t = copy.deepcopy(d)
    for arg in args:
        try:
            t = t[arg]
        except (AttributeError, KeyError):
            return None
    return t

def cmd_list_builds(args, osbs):
    builds = osbs.list_builds(namespace=args.namespace)
    format_str = "{name:48} {status:16} {image:64}"
    print(format_str.format(**{"name": "BUILD NAME", "status": "STATUS", "image": "IMAGE NAME"}), file=sys.stderr)
    for build in builds['items']:
        image = graceful_chain_get(build, 'parameters', 'output', 'imageTag')
        if args.USER:
            if not image.startswith(args.USER + "/"):
                continue
        b = {
            "name": build['metadata']['name'],
            "status": build['status'],
            "image": image,
        }
        print(format_str.format(**b))

def cmd_get_build(args, osbs):
    build_json = osbs.get_build(args.BUILD_ID[0], namespace=args.namespace).json
    # FIXME: pretty printing json could be a better idea
    template = """\
BUILD ID: {build_id}
STATUS: {status}
IMAGE: {image}
DATE: {date}

DOCKERFILE

{dockerfile}

BUILD LOGS

{logs}

PACKAGES

{packages}"""
    context = {
        "build_id": build_json['metadata']['name'],
        "status": build_json['status'],
        "image": graceful_chain_get(build_json, 'parameters', 'output', 'imageTag'),
        "date": build_json['metadata']['creationTimestamp'],
        "dockerfile": graceful_chain_get(build_json, 'metadata', 'labels', 'dockerfile'),
        "logs": graceful_chain_get(build_json, 'metadata', 'labels', 'logs'),
        "packages": graceful_chain_get(build_json, 'metadata', 'labels', 'rpm-packages'),
    }
    print(template.format(**context))

def cmd_build(args, osbs):
    build = osbs.create_build(
        git_uri=args.git_url,
        git_ref=args.git_commit,
        user=args.user,
        component=args.component,
        target=args.target,
        namespace=args.namespace
    )
    build_id = build.build_id
    print("Build submitted (%s), watching logs (feel free to interrupt)" % build_id)
    for line in osbs.get_build_logs(build_id, follow=True):
        print(line)

def cmd_build_logs(args, osbs):
    build_id = args.BUILD_ID[0]
    follow = args.follow

    if follow:
        for line in osbs.get_build_logs(build_id, follow=True, namespace=args.namespace):
            print(line)
    else:
        logs = osbs.get_build_logs(build_id, follow=False, namespace=args.namespace)
        print(logs, end="")

def cmd_watch_build(args, osbs):
    build_json = osbs.wait_for_build_to_finish(args.BUILD_ID[0], namespace=args.namespace)

def cli():
    parser = argparse.ArgumentParser(
        description="OpenShift Build Service client"
    )
    exclusive_group = parser.add_mutually_exclusive_group()
    exclusive_group.add_argument("--verbose", action="store_true", default=None)
    exclusive_group.add_argument("-q", "--quiet", action="store_true")

    subparsers = parser.add_subparsers(help='commands')

    list_builds_parser = subparsers.add_parser('list-builds', help='list builds in OSBS')
    list_builds_parser.add_argument("USER", help="list builds only for specified username",
                                    nargs="?")
    list_builds_parser.set_defaults(func=cmd_list_builds)

    watch_build_parser = subparsers.add_parser('watch-build', help='wait till build finishes')
    watch_build_parser.add_argument("BUILD_ID", help="build ID", nargs=1)
    watch_build_parser.set_defaults(func=cmd_watch_build)

    get_build_parser = subparsers.add_parser('get-build', help='get info about build')
    get_build_parser.add_argument("BUILD_ID", help="build ID", nargs=1)
    get_build_parser.set_defaults(func=cmd_get_build)

    build_logs_parser = subparsers.add_parser('build-logs', help='get or follow build logs')
    build_logs_parser.add_argument("BUILD_ID", help="build ID", nargs=1)
    build_logs_parser.add_argument("-f", "--follow", help="follow logs as they come", action="store_true",
                                   default=False)
    build_logs_parser.set_defaults(func=cmd_build_logs)

    build_parser = subparsers.add_parser('build', help='build an image in OSBS')
    build_parser.add_argument("--build-type", "-T", action="store", metavar="BUILD_TYPE",
                                   help="build type (prod, simple)")
    build_parser.add_argument("--build-json-dir", action="store", metavar="PATH",
                                   help="directory with build jsons")
    build_parser.add_argument("-g", "--git-url", action='store', metavar="URL",
                                   required=True, help="URL to git repo")
    build_parser.add_argument("--git-commit", action='store', default="master",
                                   help="checkout this commit")
    build_parser.add_argument("-c", "--component", action='store', required=True,
                                   help="name of component")
    build_parser.add_argument("-t", "--target", action='store', required=True,
                                   help="koji target name")
    build_parser.add_argument("-u", "--user", action='store', required=True,
                                   help="username (will be image prefix)")
    build_parser.set_defaults(func=cmd_build)

    parser.add_argument("--openshift-uri", action='store', metavar="URL",
                        help="openshift URL to remote API")
    parser.add_argument("--kubelet-uri", action='store', metavar="URL",
                        help="kubelet URL to remote API")
    parser.add_argument("--registry-uri", action='store', metavar="URL",
                        help="registry where images should be pushed")
    parser.add_argument("--config", action='store', metavar="PATH",
                        help="path to configuration file", default=DEFAULT_CONFIGURATION_FILE)
    parser.add_argument("--instance", "-i", action='store', metavar="SECTION_NAME",
                        help="section within config for requested instance", default=DEFAULT_CONFIGURATION_SECTION)
    parser.add_argument("--username", action='store',
                        help="username within OSBS")
    parser.add_argument("--password", action='store',
                        help="password within OSBS")
    parser.add_argument("--use-kerberos", action='store_true',
                        help="use kerberos for authentication")
    parser.add_argument("--verify-ssl", action='store_true', default=True,
                        help="verify CA on secure connections")
    parser.add_argument("--namespace", help="list builds of specific namespace "
                                            "(use --namespace=\"\" to list all builds)",
                        metavar="NAMESPACE", action="store", default="default")
    args = parser.parse_args()
    return parser, args

def main():
    parser, args = cli()
    os_conf = Configuration(conf_file=args.config, conf_section=args.instance, cli_args=args)
    build_conf = Configuration(conf_file=args.config, conf_section=args.instance, cli_args=args)

    is_verbose = os_conf.get_verbosity()

    if is_verbose:
        set_logging(level=logging.DEBUG)
        logger.debug("Logging level set to debug")
    elif args.quiet:
        set_logging(level=logging.WARNING)
    else:
        set_logging(level=logging.INFO)

    osbs = OSBS(os_conf, build_conf)

    try:
        args.func(args, osbs)
    except AttributeError as ex:
        if hasattr(args, 'func'):
            raise
        else:
            parser.print_help()
    except KeyboardInterrupt:
        print("Quitting on user request.")
        return -1
    except HTTPError as ex:
        logger.error("HTTP error: %d", ex.getcode())
        return -1
    except Exception as ex:
        if is_verbose:
            raise
        else:
            logger.error("Exception caught: %s", repr(ex))
            return -1

if __name__ == '__main__':
    sys.exit(main())
<|endoftext|>


<commit_before>#!/usr/bin/python

"""Brown Dwarf Flux ratio calculator:

Inputs :
Star name:
Mass of companion in Jupiter masses
Age of star: """

from __future__ import division, print_function
import argparse
import numpy as np
from astroquery.simbad import Simbad
import pandas as pd
from PyAstronomy import pyasl
# import matplotlib.pyplot as plt

def _parser():
    """Take care of all the argparse stuff.

    :returns: the args
    """
    parser = argparse.ArgumentParser(description='Determine flux ratio of stellar companion')
    parser.add_argument('star_name', help='Name of host star.', type=str)
    parser.add_argument('flux_ratio', help='Flux ratio between host and companion - (F_comp/F_host)', type=float)
    parser.add_argument('age', help='Star age (Gyr)',type=float)
    parser.add_argument("-b", "--band", help='Magnitude band of the flux ratio', choices=["J", "K"], default="K", type=str)
    args = parser.parse_args()
    return args

def main(star_name, flux_ratio, stellar_age, band="K"):
    """Compute flux ratio of star to companion """

    # Obtain Stellar parameters from astroquery
    star_params = get_stellar_params(star_name)    # returns a astroquesry result table

    # Find companion parameters that match these magnitudes
    companion_params = get_BD_from_flux_ratio(magnitudes, stellar_age, band=band)

    # Print flux ratios using a generator
    print("Magnitude Calculation\n")
    [print("{0} band star/companion Flux ratio = {1} >>> companion/star Flux ratio {2}".format(key, val[0], 1./val[0])) for key, val in Flux_ratios.items()]

    print("\nRadius Calculation")
    print("Star radius      = {} R_sun".format(Rstar[0]))
    print("Planet radius    = {} R_sun".format(np.round(companion["R"], 4)))
    print("Radius Ratio of companion/star    = {} ".format(Rcomp_Rstar[0]))
    print("Area Ratio of companion/star      = {} ".format(Rcomp_Rstar[0]**2))

##############################################################################
# Access Databases
##############################################################################

def get_stellar_params(star_name):
    """ Astro query search """

    #return Magnitudes, parralax, Temp
    customSimbad = Simbad()
    # Can add more fluxes here if need to extend to more flux ranges. although K is the limit for simbad.
    # if want higher need to search for Wise band in VISIER probably.
    customSimbad.add_votable_fields('parallax', 'sp', 'fluxdata(B)', 'fluxdata(V)', 'fluxdata(J)', 'fluxdata(K)', 'fe_h')

    result_table =  customSimbad.query_object(star_name)

    #print("Table colums", result_table.colnames)

    return result_table

def get_brown_dwarf_information(companion_mass, age):
    """ baraffe 2003 table search
    Need the tables in a file somewhere"""

    mass_solar = companion_mass / 1047.56   # covert to solar mass
    BD_parameters = dict()

    # Find closest age model
    modelages = ["0.001", "0.005", "0.010", "0.050", "0.100", "0.120", "0.500", "1.000", "5.000", "10.000"]
    model_age = min(modelages, key=lambda x:abs(float(x)-age)) # Closest one
    model_id = "p".join(str(model_age).split("."))

    model_name = "./Baraffe2003/BaraffeCOND2003-" +  model_id + "Gyr.dat"
    model_data = np.loadtxt(model_name, skiprows=18, unpack=False)
    model_data = model_data.T

    cols = ["M/Ms", "Teff", "L/Ls", "g", "R", "Mv", "Mr", "Mi", "Mj", "Mh", "Mk", "Mll", "Mm"]
    for col, data in zip(cols, model_data):
        # Interpolate columns to mass of companion
        BD_parameters[col] = np.interp(mass_solar, model_data[0], data)

    return BD_parameters  # as a dictionary

def get_BD_from_flux_ratio(magnitudes, age, band="K"):
    """ Baraffe 2003 table search for given magnitude.
    """
    # mass_solar = companion_mass / 1047.56   # covert to solar mass
    BD_parameters = dict()

    # Find closest age model
    modelages = ["0.001", "0.005", "0.010", "0.050", "0.100", "0.120", "0.500", "1.000", "5.000", "10.000"]
    model_age = min(modelages, key=lambda x:abs(float(x)-age)) # Closest one
    model_id = "p".join(str(model_age).split("."))

    model_name = "./Baraffe2003/BaraffeCOND2003-" +  model_id + "Gyr.dat"
    model_data = np.loadtxt(model_name, skiprows=18, unpack=False)
    model_data = model_data.T

    cols = ["M/Ms", "Teff", "L/Ls", "g", "R", "Mv", "Mr", "Mi", "Mj", "Mh", "Mk", "Mll", "Mm"]

    if "M"+band.lower() not in cols:
        raise ValueError("Band {} is not in Baraffe tables.".format(band))
    if band not in magnitudes.keys():
        raise ValueError("The band {} given is not in the given magnitudes".format(band))

    band_index = [i for i, c in enumerate(cols) if "M"+band.lower() == c][0]
    # print("band_index", band_index)

    print("Band mag", magnitudes[band])
    print(model_data)
    print("model data", model_data[band_index])
    print("all data")
    for col, data in zip(cols, model_data):
        # Interpolate columns to magnitude of companion
        print("\nThis col =", col)
        print("Band mag", magnitudes[band])
        #print(model_data)
        print("model data", model_data[band_index])
        print("this col data ", data)

        BD_parameters[col] = np.interp(magnitudes[band], model_data[band_index], data)
        print("This col interp value =", BD_parameters[col] )

    return  BD_parameters  # as a dictionary

def get_sweet_cat_temp(star_name):
    sc = pyasl.SWEETCat()
    data = sc.data
    #print(data.head())
    #print(data.keys())

    # Assuming given as hd******
    hd_number = star_name[2:]
    #print("hd number ", hd_number)
    if hd_number in sc.data.hd.values:
        hd_entry = data[data.hd == hd_number]

        if hd_entry.empty:
            return False
        else:
        # Sweet-cat has temperature of zero
        #if it does not have a temperature value for the star
            return hd_entry.iloc[0]["teff"]
    else:
        print("This star not in SWEET-Cat")
        return False

##############################################################################
# Calculations
##############################################################################
def calculate_flux_ratios(star_params, companion_params):
    """ Flux ratios for the different bands """
    f = 2.512
    Flux_ratios = dict()
    Flux_ratios["J"] = f ** (companion_params["Mj"]-star_params["FLUX_J"])
    #Flux_ratios["H"] = f ** (companion_params["Mh"]-star_params["Hmag"])
    Flux_ratios["K"] = f ** (companion_params["Mk"]-star_params["FLUX_K"])
    return Flux_ratios

def get_temperature(star_name, star_params):
    """ Try get temperature of star multiple ways

    1st - Try Fe_H_Teff param from Simbad.
    2nd - Try SweetCat but the star might not be there
    3rd - Calculate from B-V and interpolation"""
    good_temp = False
    # This is not the best way to do this but work atm
    if "Fe_H_Teff" in star_params.keys(): # need table and interpolate to this B-V
        #print("star_params['Fe_H_Teff'] =", star_params["Fe_H_Teff"])
        teff = star_params["Fe_H_Teff"][0]
        if teff == 0 or teff == [0]:
            # No teff given by Simbad
            print("Simbad Temperature was zero")
            teff = None
        else:
            good_temp = True
            print("Temperature obtained from Fe_H_Teff", good_temp)
            return teff

    if not good_temp:
        try:
            teff = get_sweet_cat_temp(star_name)

            if teff == 0 or np.isnan(teff):  # temp from sweet-cat
                print("No SWEET-Cat temperature, teff was", teff)
                teff = None
            else:
                print("SWEET-Cat teff =", teff)
                good_temp = True
                return teff
        except:
            print("Failed to get Sweetcat_temp")
            good_temp = False
            raise

    if not good_temp:
        """Then use the B-V method as last resort"""
        BminusV = star_params["FLUX_B"] - star_params["FLUX_V"]
        #print(BminusV, "b-v")
        #Interpolate from B-V
        bminusvs = np.array([-0.31, -0.24, -0.20, -0.12, 0.0, 0.15, 0.29, 0.42, 0.58, 0.69, 0.85, 1.16, 1.42, 1.61])
        temps = np.array([34000, 23000, 18500, 13000, 9500, 8500, 7300, 6600, 5900, 5600, 5100, 4200, 3700, 3000])
        #teff_star = (4200-5100)/(1.16-0.85) * (BminusV-0.85) + 5100    # Linear interpolation
        teff = np.interp(BminusV, bminusvs, temps)[0]
        print("Temperature of star was calculated from b-v = {} K".format(teff))
    return teff

def calculate_stellar_radius(star_name, star_params):
    """ Based on R/Rs = (Ts/T)^2(L/Ls)^(1/2) equation"""

    teff_star = get_temperature(star_name, star_params)

    Ts_T = 5800. / teff_star              # Temperature ratio
    Dm =   4.83 - star_params["FLUX_V"]   # Differnce of aboslute magnitude
    L_Ls = 2.51 ** Dm                     # Luminosity ratio
    R_Rs = (Ts_T)**2 * np.sqrt(L_Ls)      # Raidus of Star in Solar Radii

    return R_Rs   # R star in solar radii

if __name__ == '__main__':
    args = vars(_parser())
    star_name = args.pop('star_name')
    flux_ratio = args.pop('flux_ratio')
    age = args.pop('age')
    opts = {k: args[k] for k in args}

    main(star_name, flux_ratio, age, **opts)
<commit_msg>Calculate magnitude of companion from flux ratio and host star params.<commit_after>#!/usr/bin/python

"""Brown Dwarf Flux ratio calculator:

Inputs :
Star name:
Mass of companion in Jupiter masses
Age of star: """

from __future__ import division, print_function
import argparse
import numpy as np
from astroquery.simbad import Simbad
import pandas as pd
from PyAstronomy import pyasl
# import matplotlib.pyplot as plt

def _parser():
    """Take care of all the argparse stuff.

    :returns: the args
    """
    parser = argparse.ArgumentParser(description='Determine flux ratio of stellar companion')
    parser.add_argument('star_name', help='Name of host star.', type=str)
    parser.add_argument('flux_ratio', help='Flux ratio between host and companion - (F_comp/F_host)', type=float)
    parser.add_argument('age', help='Star age (Gyr)',type=float)
    parser.add_argument("-b", "--band", help='Magnitude band of the flux ratio', choices=["J", "K"], default="K", type=str)
    args = parser.parse_args()
    return args

def main(star_name, flux_ratio, stellar_age, band="K"):
    """Compute flux ratio of star to companion """

    # Obtain Stellar parameters from astroquery
    star_params = get_stellar_params(star_name)    # returns a astroquesry result table

    # Calculate Magnitude J and K band magnitude for this flux ratio
    magnitudes = calculate_companion_magnitude(star_params, flux_ratio)
    print("Magnitude calculate for companion", magnitudes)
    # Find companion parameters that match these magnitudes
    companion_params = get_BD_from_flux_ratio(magnitudes, stellar_age, band=band)

    # Print flux ratios using a generator
    print("Magnitude Calculation\n")
    [print("{0} band star/companion Flux ratio = {1} >>> companion/star Flux ratio {2}".format(key, val[0], 1./val[0])) for key, val in Flux_ratios.items()]

    print("\nRadius Calculation")
    print("Star radius      = {} R_sun".format(Rstar[0]))
    print("Planet radius    = {} R_sun".format(np.round(companion["R"], 4)))
    print("Radius Ratio of companion/star    = {} ".format(Rcomp_Rstar[0]))
    print("Area Ratio of companion/star      = {} ".format(Rcomp_Rstar[0]**2))

##############################################################################
# Access Databases
##############################################################################

def get_stellar_params(star_name):
    """ Astro query search """

    #return Magnitudes, parralax, Temp
    customSimbad = Simbad()
    # Can add more fluxes here if need to extend to more flux ranges. although K is the limit for simbad.
    # if want higher need to search for Wise band in VISIER probably.
    customSimbad.add_votable_fields('parallax', 'sp', 'fluxdata(B)', 'fluxdata(V)', 'fluxdata(J)', 'fluxdata(K)', 'fe_h')

    result_table =  customSimbad.query_object(star_name)

    #print("Table colums", result_table.colnames)

    return result_table

def get_brown_dwarf_information(companion_mass, age):
    """ baraffe 2003 table search
    Need the tables in a file somewhere"""

    mass_solar = companion_mass / 1047.56   # covert to solar mass
    BD_parameters = dict()

    # Find closest age model
    modelages = ["0.001", "0.005", "0.010", "0.050", "0.100", "0.120", "0.500", "1.000", "5.000", "10.000"]
    model_age = min(modelages, key=lambda x:abs(float(x)-age)) # Closest one
    model_id = "p".join(str(model_age).split("."))

    model_name = "./Baraffe2003/BaraffeCOND2003-" +  model_id + "Gyr.dat"
    model_data = np.loadtxt(model_name, skiprows=18, unpack=False)
    model_data = model_data.T

    cols = ["M/Ms", "Teff", "L/Ls", "g", "R", "Mv", "Mr", "Mi", "Mj", "Mh", "Mk", "Mll", "Mm"]
    for col, data in zip(cols, model_data):
        # Interpolate columns to mass of companion
        BD_parameters[col] = np.interp(mass_solar, model_data[0], data)

    return BD_parameters  # as a dictionary

def get_BD_from_flux_ratio(magnitudes, age, band="K"):
    """ Baraffe 2003 table search for given magnitude.
    """
    # mass_solar = companion_mass / 1047.56   # covert to solar mass
    BD_parameters = dict()

    # Find closest age model
    modelages = ["0.001", "0.005", "0.010", "0.050", "0.100", "0.120", "0.500", "1.000", "5.000", "10.000"]
    model_age = min(modelages, key=lambda x:abs(float(x)-age)) # Closest one
    model_id = "p".join(str(model_age).split("."))

    model_name = "./Baraffe2003/BaraffeCOND2003-" +  model_id + "Gyr.dat"
    model_data = np.loadtxt(model_name, skiprows=18, unpack=False)
    model_data = model_data.T

    cols = ["M/Ms", "Teff", "L/Ls", "g", "R", "Mv", "Mr", "Mi", "Mj", "Mh", "Mk", "Mll", "Mm"]

    if "M"+band.lower() not in cols:
        raise ValueError("Band {} is not in Baraffe tables.".format(band))
    if band not in magnitudes.keys():
        raise ValueError("The band {} given is not in the given magnitudes".format(band))

    band_index = [i for i, c in enumerate(cols) if "M"+band.lower() == c][0]
    # print("band_index", band_index)

    print("Band mag", magnitudes[band])
    print(model_data)
    print("model data", model_data[band_index])
    print("all data")
    for col, data in zip(cols, model_data):
        # Interpolate columns to magnitude of companion
        print("\nThis col =", col)
        print("Band mag", magnitudes[band])
        #print(model_data)
        print("model data", model_data[band_index])
        print("this col data ", data)

        BD_parameters[col] = np.interp(magnitudes[band], model_data[band_index], data)
        print("This col interp value =", BD_parameters[col] )

    return  BD_parameters  # as a dictionary

def get_sweet_cat_temp(star_name):
    sc = pyasl.SWEETCat()
    data = sc.data
    #print(data.head())
    #print(data.keys())

    # Assuming given as hd******
    hd_number = star_name[2:]
    #print("hd number ", hd_number)
    if hd_number in sc.data.hd.values:
        hd_entry = data[data.hd == hd_number]

        if hd_entry.empty:
            return False
        else:
        # Sweet-cat has temperature of zero
        #if it does not have a temperature value for the star
            return hd_entry.iloc[0]["teff"]
    else:
        print("This star not in SWEET-Cat")
        return False

##############################################################################
# Calculations
##############################################################################
def calculate_flux_ratios(star_params, companion_params):
    """ Flux ratios for the different bands """
    f = 2.512
    Flux_ratios = dict()
    Flux_ratios["J"] = f ** (companion_params["Mj"]-star_params["FLUX_J"])
    #Flux_ratios["H"] = f ** (companion_params["Mh"]-star_params["Hmag"])
    Flux_ratios["K"] = f ** (companion_params["Mk"]-star_params["FLUX_K"])
    return Flux_ratios

def calculate_companion_magnitude(star_params, flux_ratio, band="K"):
    """ Calculte companion magnitude from flux ratio

    Using the equation m - n = -2.5 * log_10(F_m / F_n)

    Parameters
    ----------
    star_params: dict
        Parameters for the host star.
    flux_ratio: float
        Flux ratio for the system (F_comp/F_host).
    band: str
        Band to use. default = "K"

    Returns
    -------
    magnitudes: dict
        Magnitudes of the companion in J and K bands.

    """
    magnitudes = dict()
    band = band.upper()
    if band in ["J", "K"]:
        magnitudes[band] = star_params["FLUX_{}".format(band)] - 2.5 * np.log10(flux_ratio)
    else:
        return ValueError("Band is not available atm.")
    return magnitudes

def get_temperature(star_name, star_params):
    """ Try get temperature of star multiple ways

    1st - Try Fe_H_Teff param from Simbad.
    2nd - Try SweetCat but the star might not be there
    3rd - Calculate from B-V and interpolation"""
    good_temp = False
    # This is not the best way to do this but work atm
    if "Fe_H_Teff" in star_params.keys(): # need table and interpolate to this B-V
        #print("star_params['Fe_H_Teff'] =", star_params["Fe_H_Teff"])
        teff = star_params["Fe_H_Teff"][0]
        if teff == 0 or teff == [0]:
            # No teff given by Simbad
            print("Simbad Temperature was zero")
            teff = None
        else:
            good_temp = True
            print("Temperature obtained from Fe_H_Teff", good_temp)
            return teff

    if not good_temp:
        try:
            teff = get_sweet_cat_temp(star_name)

            if teff == 0 or np.isnan(teff):  # temp from sweet-cat
                print("No SWEET-Cat temperature, teff was", teff)
                teff = None
            else:
                print("SWEET-Cat teff =", teff)
                good_temp = True
                return teff
        except:
            print("Failed to get Sweetcat_temp")
            good_temp = False
            raise

    if not good_temp:
        """Then use the B-V method as last resort"""
        BminusV = star_params["FLUX_B"] - star_params["FLUX_V"]
        #print(BminusV, "b-v")
        #Interpolate from B-V
        bminusvs = np.array([-0.31, -0.24, -0.20, -0.12, 0.0, 0.15, 0.29, 0.42, 0.58, 0.69, 0.85, 1.16, 1.42, 1.61])
        temps = np.array([34000, 23000, 18500, 13000, 9500, 8500, 7300, 6600, 5900, 5600, 5100, 4200, 3700, 3000])
        #teff_star = (4200-5100)/(1.16-0.85) * (BminusV-0.85) + 5100    # Linear interpolation
        teff = np.interp(BminusV, bminusvs, temps)[0]
        print("Temperature of star was calculated from b-v = {} K".format(teff))
    return teff

def calculate_stellar_radius(star_name, star_params):
    """ Based on R/Rs = (Ts/T)^2(L/Ls)^(1/2) equation"""

    teff_star = get_temperature(star_name, star_params)

    Ts_T = 5800. / teff_star              # Temperature ratio
    Dm =   4.83 - star_params["FLUX_V"]   # Differnce of aboslute magnitude
    L_Ls = 2.51 ** Dm                     # Luminosity ratio
    R_Rs = (Ts_T)**2 * np.sqrt(L_Ls)      # Raidus of Star in Solar Radii

    return R_Rs   # R star in solar radii

if __name__ == '__main__':
    args = vars(_parser())
    star_name = args.pop('star_name')
    flux_ratio = args.pop('flux_ratio')
    age = args.pop('age')
    opts = {k: args[k] for k in args}

    main(star_name, flux_ratio, age, **opts)
<|endoftext|>


<commit_before>from plex import Plex
from plex.core.helpers import synchronized
from plex_activity import Activity
from plex_metadata.core.defaults import DEFAULT_TYPES
from plex_metadata.core.helpers import urlparse

from threading import Condition
import logging

log = logging.getLogger(__name__)

# TODO automatic invalidated object removal (+ manual trigger)
class Metadata(object):
    def __init__(self, types=None):
        if types is not None:
            self.types = types
        else:
            self.types = DEFAULT_TYPES

        self.cache = None
        self.client = None

        # Private
        self._lock = Condition()

        # Bind activity events
        Activity.on('websocket.timeline.created', self.timeline_created)
        Activity.on('websocket.timeline.deleted', self.timeline_deleted)
        Activity.on('websocket.timeline.finished', self.timeline_finished)

    def configure(self, cache=None, client=None):
        self.cache = self.cache or cache
        self.client = self.client or client

    @synchronized
    def get(self, key):
        try:
            # Try retrieve item from cache (if it exists)
            value = self._cache_get(key)
        except Exception as ex:
            log.warn('Unable to retrieve item "%s" from cache - %s', key, ex, exc_info=True)
            value = None

        if value is None:
            # Item not available in cache / no cache enabled
            value = self.refresh(key)

        return value

    @synchronized
    def invalidate(self, key):
        try:
            # Invalidate item in cache
            self._cache_invalidate(key)
        except Exception as ex:
            log.warn('Unable to invalidate item "%s" from cache - %s', key, ex, exc_info=True)

    @synchronized
    def refresh(self, key):
        # Item not available in cache / no cache enabled
        value = self.fetch(key)

        try:
            # Store in cache
            self._cache_store(key, value)
        except Exception as ex:
            log.warn('Unable to store item "%s" in cache - %s', key, ex, exc_info=True)

        return value

    #
    # Cache methods
    #

    def _cache_get(self, key):
        if self.cache is None:
            return None

        value = self.cache.get(key)

        if value is None:
            return None

        # Validate item
        if not self._valid(value.guid):
            self._cache_invalidate(key)
            return None

        # Update `client` attribute
        value.client = self.client

        return value

    def _cache_invalidate(self, key):
        if self.cache is None:
            return

        del self.cache[key]

    def _cache_store(self, key, value):
        if self.cache is None:
            return

        if not value or not self._valid(value.guid):
            return

        self.cache[key] = value

    @staticmethod
    def _valid(guid):
        if not guid:
            return False

        agent, uri = urlparse(guid)

        if agent in ['local', 'none']:
            return False

        return True

    #
    # Event handlers
    #

    def fetch(self, key):
        # Request metadata from server
        container = Plex['library'].metadata(key)

        if not container:
            log.warn('Metadata request for item "%s" failed', key)
            return None

        # Cast to `list` (resolve iterators)
        items = list(container)

        if not items:
            log.warn('Unable to retrieve item, container empty')
            return None

        item = items[0]

        # Ignore if this is an unsupported media type
        if item.type not in self.types:
            # TODO set flag to ignore future refresh requests
            log.warn('Item %s with type "%s" has been ignored', key, item.type)
            return None

        return item

    #
    # Timeline event handlers
    #

    def timeline_created(self, item):
        pass

    def timeline_deleted(self, item):
        # TODO self.cache.remove(str(item['itemID']))
        pass

    def timeline_finished(self, item):
        # TODO self.cache.invalidate(str(item['itemID']), refresh=True, create=True)
        pass

# Global object
Default = Metadata()
<commit_msg>[metadata] cache unsupported items as `False` (internally) to avoid refreshing on every request<commit_after>from plex import Plex
from plex.core.helpers import synchronized
from plex_activity import Activity
from plex_metadata.core.defaults import DEFAULT_TYPES
from plex_metadata.core.helpers import urlparse

from threading import Condition
import logging

log = logging.getLogger(__name__)

# TODO automatic invalidated object removal (+ manual trigger)
class Metadata(object):
    def __init__(self, types=None):
        if types is not None:
            self.types = types
        else:
            self.types = DEFAULT_TYPES

        self.cache = None
        self.client = None

        # Private
        self._lock = Condition()

        # Bind activity events
        Activity.on('websocket.timeline.created', self.timeline_created)
        Activity.on('websocket.timeline.deleted', self.timeline_deleted)
        Activity.on('websocket.timeline.finished', self.timeline_finished)

    def configure(self, cache=None, client=None):
        self.cache = self.cache or cache
        self.client = self.client or client

    @synchronized
    def get(self, key):
        try:
            # Try retrieve item from cache (if it exists)
            value = self._cache_get(key)
        except Exception as ex:
            log.warn('Unable to retrieve item "%s" from cache - %s', key, ex, exc_info=True)
            value = None

        if value is None:
            # Item not available in cache / no cache enabled
            value = self.refresh(key)

        return value if value else None

    @synchronized
    def invalidate(self, key):
        try:
            # Invalidate item in cache
            self._cache_invalidate(key)
        except Exception as ex:
            log.warn('Unable to invalidate item "%s" from cache - %s', key, ex, exc_info=True)

    @synchronized
    def refresh(self, key):
        # Item not available in cache / no cache enabled
        value = self.fetch(key)

        try:
            # Store in cache
            self._cache_store(key, value)
        except Exception as ex:
            log.warn('Unable to store item "%s" in cache - %s', key, ex, exc_info=True)

        return value

    #
    # Cache methods
    #

    def _cache_get(self, key):
        if self.cache is None:
            return None

        value = self.cache.get(key)

        if value is None:
            return None

        if not value:
            # Unsupported media (False)
            return value

        # Validate item
        if not self._valid(value.guid):
            self._cache_invalidate(key)
            return None

        # Update `client` attribute
        value.client = self.client

        return value

    def _cache_invalidate(self, key):
        if self.cache is None:
            return

        del self.cache[key]

    def _cache_store(self, key, value):
        if self.cache is None:
            return

        if value is None:
            return

        if value and not self._valid(value.guid):
            return

        self.cache[key] = value

    @staticmethod
    def _valid(guid):
        if not guid:
            return False

        agent, uri = urlparse(guid)

        if agent in ['local', 'none']:
            return False

        return True

    #
    # Event handlers
    #

    def fetch(self, key):
        # Request metadata from server
        container = Plex['library'].metadata(key)

        if not container:
            log.warn('Metadata request for item "%s" failed', key)
            return None

        # Cast to `list` (resolve iterators)
        items = list(container)

        if not items:
            log.warn('Unable to retrieve item, container empty')
            return None

        item = items[0]

        # Ignore if this is an unsupported media type
        if item.type not in self.types:
            # TODO set flag to ignore future refresh requests
            log.warn('Item %s with type "%s" has been ignored', key, item.type)
            return False

        return item

    #
    # Timeline event handlers
    #

    def timeline_created(self, item):
        pass

    def timeline_deleted(self, item):
        # TODO self.cache.remove(str(item['itemID']))
        pass

    def timeline_finished(self, item):
        # TODO self.cache.invalidate(str(item['itemID']), refresh=True, create=True)
        pass

# Global object
Default = Metadata()
<|endoftext|>


<commit_before>from rest_framework import serializers
from .models import *

#Serializers for Author
class UserSerializer(serializers.ModelSerializer):
    class Meta:
        model = User
        fields = ('first_name', 'last_name', 'email')

class AuthorFriendSerializer(serializers.HyperlinkedModelSerializer):
    #hyperlinkedModelSerializer uses hyperlinks instead of p-keys
    id = serializers.UUIDField(source='author_id')
    class Meta:
        model = Friend
        fields = ('id', 'host', 'display_name', 'url')

class ViewAuthorSerializer(serializers.ModelSerializer):

    displayname = serializers.CharField(source='user.username')
    first_name = serializers.CharField(source='user.first_name')
    last_name = serializers.CharField(source='user.last_name')
    email = serializers.CharField(source='user.email')

    friends = AuthorFriendSerializer(many=True, read_only=True)

    class Meta:
        model = Author
        fields = ('id', 'host', 'displayname', 'url', 'friends', 'github',
          'first_name', 'last_name', 'email', 'bio')

# Used to serailize response for a GET to /api/friends/<auth id>
class FriendDetailSerializer(serializers.ModelSerializer):

    query = serializers.SerializerMethodField('getQuery')
    authors = serializers.SerializerMethodField('getFriends')

    class Meta:
        model = Author
        fields = ('query','authors')

    def getFriends(self,obj):
        query = obj.friends.all().values('author_id')
        res = []
        for item in query:
            res.append(item.values()[0])
        return res

    def getQuery(self,obj):
        return "friends"

# Used to serailize response for a POST to /api/friends/<auth id>
class FriendVerifySerializer(serializers.ModelSerializer):

    query = serializers.SerializerMethodField('getQuery')
    author = serializers.SerializerMethodField('getAuthor')
    authors = serializers.SerializerMethodField('parseFriendList')

    class Meta:
        model = Author
        fields = ('query','author','authors')

    def getQuery(self,obj):
        return self.context.get('query')

    def getAuthor(self,obj):
        return self.context.get('author')  # for consistency

    def parseFriendList(self,obj):
        # get all friends
        friendList = obj.friends.all().values('author_id')
        friends = []
        for item in friendList:
            friends.append(str(item.values()[0]))

        result = []
        for author in self.context.get('authors'):
            author = str(author)
            if author in friends:
                result.append(author)
        return result

# Used to tell if two authors are friends
class FriendQuerySerializer(serializers.ModelSerializer):

    query = serializers.SerializerMethodField('getQuery')
    authors = serializers.SerializerMethodField('getAuthors')
    friends = serializers.SerializerMethodField('getFriendship')

    class Meta:
        model = Author
        fields = ('query','authors','friends')

    def getQuery(self,obj):
        return "friends"

    def getAuthors(self,obj):
        res = []
        for author in obj:
            res.append(author.id)
        return res

    def getFriendship(self,obj):
        author1 = obj[0]
        author2 = obj[1]

        for friend in author1.friends.all():
            if friend.author_id == author2.id:
                return True
        return False

class UpdateAuthorSerializer(serializers.ModelSerializer):

    first_name = serializers.CharField(source='user.first_name')
    last_name = serializers.CharField(source='user.last_name')
    email = serializers.CharField(source='user.email')

    class Meta:
        model = Author
        fields = ('first_name', 'last_name', 'email', 'github', 'bio')

    def update(self, instance, validated_data):
        #save the user info
        user_data = validated_data.pop('user')
        user =  self.context['request'].user
        user_serializer = UserSerializer(data=user_data)
        if user_serializer.is_valid():
            user_serializer.update(user, user_data)
        #save the author info
        instance.github = validated_data.get('github', instance.github)
        instance.bio = validated_data.get('bio', instance.bio)
        instance.save()

        return instance

#Serializers for Posts
#This serializer is to show the nested author object in a GET request
class PostAuthorSerializer(serializers.HyperlinkedModelSerializer):

    displayName = serializers.CharField(source='user.username')

    class Meta:
        model = Author
        fields = ('id', 'host', 'displayName', 'url', 'github')

#This serializer is to show comment author object
class PostCommentAuthorSerializer(serializers.HyperlinkedModelSerializer)

    id = serializers.UUIDField(source='author_id')

    class Meta:
        model = CommentAuthor
        fields = ('id', 'host', 'displayName', 'url', 'github')

#This serializer is to show the nested comment objects in a GET request
class PostCommentSerializer(serializers.HyperlinkedModelSerializer):

    author = PostCommentAuthorSerializer(read_only=True)
    published = serializers.DateTimeField(source='date_created')

    class Meta:
        model = Comment
        fields = ('author', 'comment', 'contentType', 'published', 'id')

#This serializer is for displaying posts in the API
class ViewPostsSerializer(serializers.ModelSerializer):

    count = serializers.SerializerMethodField()
    comments = PostCommentSerializer(many=True, read_only=True)
    #For GET request show the author object
    author = PostAuthorSerializer(read_only=True)
    published = serializers.DateTimeField(source='date_created')

    class Meta:
        model = Post
        fields = ('title', 'source', 'origin', 'description', 'contentType',
          'content', 'author', 'categories', 'count', 'comments', 'published',
          'id', 'visibility')

    def get_count(self, obj):
        if obj.comments == None:
            return 0
        return obj.comments.count()

#This serializer is for creating or editing posts in the API
class UpdatePostsSerializer(serializers.ModelSerializer):
    class Meta:
        model = Post
        fields = ('title', 'source', 'origin', 'description', 'date_created','contentType',
              'content', 'author', 'categories', 'visibility')

#Serializer for Comments
#This serializer is for viewing author of Comments
class ViewCommentAuthorSerializer(serializers.ModelSerializer):

    class Meta:
        model = CommentAuthor
        fields = ('id', 'host', 'displayName')

#This serializer is for viewing comments of a post
class ViewCommentSerializer(serializers.ModelSerializer):

    guid = serializers.UUIDField(source='id')
    author = ViewCommentAuthorSerializer(read_only=True)
    pubDate = serializers.DateTimeField(source='date_created')

    class Meta:
        model = Comment
        fields = ('author', 'comment', 'pubDate', 'guid')

#This serialzer is specifically creating author objects for a comment
class CommentAuthorSerializer(serializers.ModelSerializer):
    id = serializers.UUIDField(source='author_id')

    class Meta:
        model = CommentAuthor
        fields = ('id', 'host', 'displayName', 'url', 'github')

#This serializer is for creating and editing a comment of a post
class UpdateCommentSerializer(serializers.ModelSerializer):

    author = CommentAuthorSerializer()

    class Meta:
        model = Comment
        fields = ('author', 'comment', 'contentType')

    def create(self, validated_data):
        #create the comment author object
        commentAuthorData = validated_data.pop('author')
        author = CommentAuthor.objects.create(**commentAuthorData)
        post = Post.objects.filter(id=self.context['post_pk'])
        comment = Comment.objects.create(post=post[0], author=author, **validated_data)
        return comment

class AuthorPostSerializer(serializers.ModelSerializer):
    count = serializers.SerializerMethodField()
    comments = PostCommentSerializer(many=True, read_only=True)
    #For GET request show the author object
    #author = PostAuthorSerializer(read_only=True)
    published = serializers.DateTimeField(source='date_created')

    class Meta:
        model = Post
        fields = ('title', 'source', 'origin', 'description', 'contentType',
          'content', 'categories', 'count', 'comments', 'published',
          'id', 'visibility')

    def get_count(self, obj):
        if obj.comments == None:
            return 0
        return obj.comments.count()

class ViewFriendSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = Friend
        fields = ('author_id', 'host', 'display_name', 'url')

class ViewFriendRequestsSerializer(serializers.ModelSerializer):
    friendrequests = ViewFriendSerializer(source='pendingFriends', many=True, read_only=True)

    class Meta:
        model = Author
        fields = ('id', 'friendrequests')

class ViewFollowingSerializer(serializers.ModelSerializer):
    following = ViewFriendSerializer(many=True, read_only=True)

    class Meta:
        model = Author
        fields = ('id', 'following')

class ConnectedNodeSerializer(serializers.ModelSerializer):
    class Meta:
        model = Node
        fields = ('url', 'username', 'password')
<commit_msg>fixed error<commit_after>from rest_framework import serializers
from .models import *

#Serializers for Author
class UserSerializer(serializers.ModelSerializer):
    class Meta:
        model = User
        fields = ('first_name', 'last_name', 'email')

class AuthorFriendSerializer(serializers.HyperlinkedModelSerializer):
    #hyperlinkedModelSerializer uses hyperlinks instead of p-keys
    id = serializers.UUIDField(source='author_id')
    class Meta:
        model = Friend
        fields = ('id', 'host', 'display_name', 'url')

class ViewAuthorSerializer(serializers.ModelSerializer):

    displayname = serializers.CharField(source='user.username')
    first_name = serializers.CharField(source='user.first_name')
    last_name = serializers.CharField(source='user.last_name')
    email = serializers.CharField(source='user.email')

    friends = AuthorFriendSerializer(many=True, read_only=True)

    class Meta:
        model = Author
        fields = ('id', 'host', 'displayname', 'url', 'friends', 'github',
          'first_name', 'last_name', 'email', 'bio')

# Used to serailize response for a GET to /api/friends/<auth id>
class FriendDetailSerializer(serializers.ModelSerializer):

    query = serializers.SerializerMethodField('getQuery')
    authors = serializers.SerializerMethodField('getFriends')

    class Meta:
        model = Author
        fields = ('query','authors')

    def getFriends(self,obj):
        query = obj.friends.all().values('author_id')
        res = []
        for item in query:
            res.append(item.values()[0])
        return res

    def getQuery(self,obj):
        return "friends"

# Used to serailize response for a POST to /api/friends/<auth id>
class FriendVerifySerializer(serializers.ModelSerializer):

    query = serializers.SerializerMethodField('getQuery')
    author = serializers.SerializerMethodField('getAuthor')
    authors = serializers.SerializerMethodField('parseFriendList')

    class Meta:
        model = Author
        fields = ('query','author','authors')

    def getQuery(self,obj):
        return self.context.get('query')

    def getAuthor(self,obj):
        return self.context.get('author')  # for consistency

    def parseFriendList(self,obj):
        # get all friends
        friendList = obj.friends.all().values('author_id')
        friends = []
        for item in friendList:
            friends.append(str(item.values()[0]))

        result = []
        for author in self.context.get('authors'):
            author = str(author)
            if author in friends:
                result.append(author)
        return result

# Used to tell if two authors are friends
class FriendQuerySerializer(serializers.ModelSerializer):

    query = serializers.SerializerMethodField('getQuery')
    authors = serializers.SerializerMethodField('getAuthors')
    friends = serializers.SerializerMethodField('getFriendship')

    class Meta:
        model = Author
        fields = ('query','authors','friends')

    def getQuery(self,obj):
        return "friends"

    def getAuthors(self,obj):
        res = []
        for author in obj:
            res.append(author.id)
        return res

    def getFriendship(self,obj):
        author1 = obj[0]
        author2 = obj[1]

        for friend in author1.friends.all():
            if friend.author_id == author2.id:
                return True
        return False

class UpdateAuthorSerializer(serializers.ModelSerializer):

    first_name = serializers.CharField(source='user.first_name')
    last_name = serializers.CharField(source='user.last_name')
    email = serializers.CharField(source='user.email')

    class Meta:
        model = Author
        fields = ('first_name', 'last_name', 'email', 'github', 'bio')

    def update(self, instance, validated_data):
        #save the user info
        user_data = validated_data.pop('user')
        user =  self.context['request'].user
        user_serializer = UserSerializer(data=user_data)
        if user_serializer.is_valid():
            user_serializer.update(user, user_data)
        #save the author info
        instance.github = validated_data.get('github', instance.github)
        instance.bio = validated_data.get('bio', instance.bio)
        instance.save()

        return instance

#Serializers for Posts
#This serializer is to show the nested author object in a GET request
class PostAuthorSerializer(serializers.HyperlinkedModelSerializer):

    displayName = serializers.CharField(source='user.username')

    class Meta:
        model = Author
        fields = ('id', 'host', 'displayName', 'url', 'github')

#This serializer is to show comment author object
class PostCommentAuthorSerializer(serializers.HyperlinkedModelSerializer):

    id = serializers.UUIDField(source='author_id')

    class Meta:
        model = CommentAuthor
        fields = ('id', 'host', 'displayName', 'url', 'github')

#This serializer is to show the nested comment objects in a GET request
class PostCommentSerializer(serializers.HyperlinkedModelSerializer):

    author = PostCommentAuthorSerializer(read_only=True)
    published = serializers.DateTimeField(source='date_created')

    class Meta:
        model = Comment
        fields = ('author', 'comment', 'contentType', 'published', 'id')

#This serializer is for displaying posts in the API
class ViewPostsSerializer(serializers.ModelSerializer):

    count = serializers.SerializerMethodField()
    comments = PostCommentSerializer(many=True, read_only=True)
    #For GET request show the author object
    author = PostAuthorSerializer(read_only=True)
    published = serializers.DateTimeField(source='date_created')

    class Meta:
        model = Post
        fields = ('title', 'source', 'origin', 'description', 'contentType',
          'content', 'author', 'categories', 'count', 'comments', 'published',
          'id', 'visibility')

    def get_count(self, obj):
        if obj.comments == None:
            return 0
        return obj.comments.count()

#This serializer is for creating or editing posts in the API
class UpdatePostsSerializer(serializers.ModelSerializer):
    class Meta:
        model = Post
        fields = ('title', 'source', 'origin', 'description', 'date_created','contentType',
              'content', 'author', 'categories', 'visibility')

#Serializer for Comments
#This serializer is for viewing author of Comments
class ViewCommentAuthorSerializer(serializers.ModelSerializer):

    class Meta:
        model = CommentAuthor
        fields = ('id', 'host', 'displayName')

#This serializer is for viewing comments of a post
class ViewCommentSerializer(serializers.ModelSerializer):

    guid = serializers.UUIDField(source='id')
    author = ViewCommentAuthorSerializer(read_only=True)
    pubDate = serializers.DateTimeField(source='date_created')

    class Meta:
        model = Comment
        fields = ('author', 'comment', 'pubDate', 'guid')

#This serialzer is specifically creating author objects for a comment
class CommentAuthorSerializer(serializers.ModelSerializer):
    id = serializers.UUIDField(source='author_id')

    class Meta:
        model = CommentAuthor
        fields = ('id', 'host', 'displayName', 'url', 'github')

#This serializer is for creating and editing a comment of a post
class UpdateCommentSerializer(serializers.ModelSerializer):

    author = CommentAuthorSerializer()

    class Meta:
        model = Comment
        fields = ('author', 'comment', 'contentType')

    def create(self, validated_data):
        #create the comment author object
        commentAuthorData = validated_data.pop('author')
        author = CommentAuthor.objects.create(**commentAuthorData)
        post = Post.objects.filter(id=self.context['post_pk'])
        comment = Comment.objects.create(post=post[0], author=author, **validated_data)
        return comment

class AuthorPostSerializer(serializers.ModelSerializer):
    count = serializers.SerializerMethodField()
    comments = PostCommentSerializer(many=True, read_only=True)
    #For GET request show the author object
    #author = PostAuthorSerializer(read_only=True)
    published = serializers.DateTimeField(source='date_created')

    class Meta:
        model = Post
        fields = ('title', 'source', 'origin', 'description', 'contentType',
          'content', 'categories', 'count', 'comments', 'published',
          'id', 'visibility')

    def get_count(self, obj):
        if obj.comments == None:
            return 0
        return obj.comments.count()

class ViewFriendSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = Friend
        fields = ('author_id', 'host', 'display_name', 'url')

class ViewFriendRequestsSerializer(serializers.ModelSerializer):
    friendrequests = ViewFriendSerializer(source='pendingFriends', many=True, read_only=True)

    class Meta:
        model = Author
        fields = ('id', 'friendrequests')

class ViewFollowingSerializer(serializers.ModelSerializer):
    following = ViewFriendSerializer(many=True, read_only=True)

    class Meta:
        model = Author
        fields = ('id', 'following')

class ConnectedNodeSerializer(serializers.ModelSerializer):
    class Meta:
        model = Node
        fields = ('url', 'username', 'password')
<|endoftext|>


<commit_before>"""
This module handles accessing, storing, and managing the graph reference.

"""
from __future__ import absolute_import

import hashlib
import json
import os
import requests
from pkg_resources import resource_string

import six

from plotly import exceptions, files, utils

GRAPH_REFERENCE_PATH = '/v2/plot-schema'

def get_graph_reference():
    """
    Attempts to load local copy of graph reference or makes GET request if DNE.

    :return: (dict) The graph reference.
    :raises: (PlotlyError) When graph reference DNE and GET request fails.

    """
    default_config = files.FILE_CONTENT[files.CONFIG_FILE]
    if files.check_file_permissions():
        graph_reference = utils.load_json_dict(files.GRAPH_REFERENCE_FILE)
        config = utils.load_json_dict(files.CONFIG_FILE)

        # TODO: https://github.com/plotly/python-api/issues/293
        plotly_api_domain = config.get('plotly_api_domain',
                                       default_config['plotly_api_domain'])
    else:
        graph_reference = {}
        plotly_api_domain = default_config['plotly_api_domain']

    sha1 = hashlib.sha1(six.b(str(graph_reference))).hexdigest()

    graph_reference_url = '{}{}?sha1={}'.format(plotly_api_domain,
                                                GRAPH_REFERENCE_PATH, sha1)

    try:
        response = requests.get(graph_reference_url)
        response.raise_for_status()
    except requests.exceptions.RequestException:
        if not graph_reference:
            path = os.path.join('graph_reference', 'default-schema.json')
            s = resource_string('plotly', path).decode('utf-8')
            graph_reference = json.loads(s)
    else:
        if six.PY3:
            content = str(response.content, encoding='utf-8')
        else:
            content = response.content
        data = json.loads(content)
        if data['modified']:
            graph_reference = data['schema']

    return utils.decode_unicode(graph_reference)

GRAPH_REFERENCE = get_graph_reference()
<commit_msg>Add a `5s` timeout to the download request.<commit_after>"""
This module handles accessing, storing, and managing the graph reference.

"""
from __future__ import absolute_import

import hashlib
import json
import os
import requests
from pkg_resources import resource_string

import six

from plotly import exceptions, files, utils

GRAPH_REFERENCE_PATH = '/v2/plot-schema'
GRAPH_REFERENCE_DOWNLOAD_TIMEOUT = 5  # seconds

def get_graph_reference():
    """
    Attempts to load local copy of graph reference or makes GET request if DNE.

    :return: (dict) The graph reference.
    :raises: (PlotlyError) When graph reference DNE and GET request fails.

    """
    default_config = files.FILE_CONTENT[files.CONFIG_FILE]
    if files.check_file_permissions():
        graph_reference = utils.load_json_dict(files.GRAPH_REFERENCE_FILE)
        config = utils.load_json_dict(files.CONFIG_FILE)

        # TODO: https://github.com/plotly/python-api/issues/293
        plotly_api_domain = config.get('plotly_api_domain',
                                       default_config['plotly_api_domain'])
    else:
        graph_reference = {}
        plotly_api_domain = default_config['plotly_api_domain']

    sha1 = hashlib.sha1(six.b(str(graph_reference))).hexdigest()

    graph_reference_url = '{}{}?sha1={}'.format(plotly_api_domain,
                                                GRAPH_REFERENCE_PATH, sha1)

    try:
        response = requests.get(graph_reference_url,
                                timeout=GRAPH_REFERENCE_DOWNLOAD_TIMEOUT)
        response.raise_for_status()
    except requests.exceptions.RequestException:
        if not graph_reference:
            path = os.path.join('graph_reference', 'default-schema.json')
            s = resource_string('plotly', path).decode('utf-8')
            graph_reference = json.loads(s)
    else:
        if six.PY3:
            content = str(response.content, encoding='utf-8')
        else:
            content = response.content
        data = json.loads(content)
        if data['modified']:
            graph_reference = data['schema']

    return utils.decode_unicode(graph_reference)

GRAPH_REFERENCE = get_graph_reference()
<|endoftext|>