text:
# Makes bubble plots for congress members that show bills proposed, partisan score, and avg. num of cosponsors
# Author: Kiran D. Bhattacharyya
# License: CC0

# import relevant libraries
import plotly
import plotly.plotly as py
import plotly.graph_objs as go
import pandas as pd
import re
import numpy as np

# read in relevant data files
memberData = pd.read_pickle('../Data/memberData.pkl')
allBillData = pd.read_pickle('../Data/allBillMasterData.pkl')

# create lists to store and parse data
house_dem_name = list()
house_rep_name = list()
senate_dem_name = list()
senate_rep_name = list()
senate_ind_name = list()

house_rep_sponLeg = list()
house_dem_sponLeg = list()
senate_rep_sponLeg = list()
senate_dem_sponLeg = list()
senate_ind_sponLeg = list()

house_rep_avgCospon = list()
house_dem_avgCospon = list()
senate_rep_avgCospon = list()
senate_dem_avgCospon = list()
senate_ind_avgCospon = list()

house_rep_avgPartisan = list()
house_dem_avgPartisan = list()
senate_rep_avgPartisan = list()
senate_dem_avgPartisan = list()
senate_ind_avgPartisan = list()

# go through each member to find relevant data and sort into lists
for i in range(0,len(memberData)):
    thisName = memberData.MemberName[i]
    memberNameSqueeze = thisName.replace(" ", "")
    memberNameSqueeze = re.sub(r'[^\w]', '', memberNameSqueeze)

    thisMember_sponLeg = 0
    thisMember_avgCospon = 0
    thisMember_avgPartisan = 0

    allBillIndx = [k for k, x in enumerate(allBillData.BillSponsor) if memberNameSqueeze in x]

    if allBillIndx:
        for j in range(0,len(allBillIndx)):
            thisYear = int(allBillData.BillDate[allBillIndx[j]][-4:])
            if thisYear > 2016:
                thisMember_sponLeg = thisMember_sponLeg + 1
                thisMember_avgCospon = thisMember_avgCospon + allBillData.BillNumCosponsors[allBillIndx[j]]
                thisMember_avgPartisan = thisMember_avgPartisan + allBillData.BillPartisanScore[allBillIndx[j]]

        if thisMember_sponLeg > 0:
            thisMember_avgCospon = thisMember_avgCospon/thisMember_sponLeg
            thisMember_avgPartisan = thisMember_avgPartisan/thisMember_sponLeg

        if 'Representative' in thisName:
            if 'Demo' in memberData.MemberParty[i]:
                house_dem_sponLeg.append(thisMember_sponLeg)
                house_dem_avgCospon.append(thisMember_avgCospon)
                house_dem_name.append(thisName[15:])
                house_dem_avgPartisan.append(thisMember_avgPartisan)
            elif 'Repub' in memberData.MemberParty[i]:
                house_rep_sponLeg.append(thisMember_sponLeg)
                house_rep_avgCospon.append(thisMember_avgCospon)
                house_rep_name.append(thisName[15:])
                house_rep_avgPartisan.append(thisMember_avgPartisan)
        elif 'Senator' in thisName:
            if 'Demo' in memberData.MemberParty[i]:
                senate_dem_sponLeg.append(thisMember_sponLeg)
                senate_dem_avgCospon.append(thisMember_avgCospon)
                senate_dem_name.append(thisName[7:])
                senate_dem_avgPartisan.append(thisMember_avgPartisan)
            elif 'Repub' in memberData.MemberParty[i]:
                senate_rep_sponLeg.append(thisMember_sponLeg)
                senate_rep_avgCospon.append(thisMember_avgCospon)
                senate_rep_name.append(thisName[7:])
                senate_rep_avgPartisan.append(thisMember_avgPartisan)
            elif 'Ind' in memberData.MemberParty[i]:
                senate_ind_sponLeg.append(thisMember_sponLeg)
                senate_ind_avgCospon.append(thisMember_avgCospon)
                senate_ind_name.append(thisName[7:])
                senate_ind_avgPartisan.append(thisMember_avgPartisan)

####################### House bubble plot ###########################
# create lists of hover texts to plot with plotly
house_dem_hover_text = list()
house_dem_color = list()

for i in range(0,len(house_dem_name)):
    thisName = house_dem_name[i]
    thisParty = 'Democrat'
    thisNumOfSpon = str(house_dem_sponLeg[i])
    thisAvgCospon = str(house_dem_avgCospon[i])
    thisPartisan = str(house_dem_avgPartisan[i])
    house_dem_hover_text.append(('Name: ' + thisName + '
'+
                      'Party: ' + thisParty + '
'+
                      '# of Sponsored Bills: ' + thisNumOfSpon + '
'+
                      'Avg. # of Cosponsors: ' + thisAvgCospon + '
'+
                      'Avg. Partisan Score: ' + thisPartisan))
    house_dem_color.append('rgb(56, 94, 244)')

house_rep_hover_text = list()
house_rep_color = list()

for i in range(0,len(house_rep_name)):
    thisName = house_rep_name[i]
    thisParty = 'Republican'
    thisNumOfSpon = str(house_rep_sponLeg[i])
    thisAvgCospon = str(house_rep_avgCospon[i])
    thisPartisan = str(house_rep_avgPartisan[i])
    house_rep_hover_text.append(('Name: ' + thisName + '
'+
                      'Party: ' + thisParty + '
'+
                      '# of Sponsored Bills: ' + thisNumOfSpon + '
'+
                      'Avg. # of Cosponsors: ' + thisAvgCospon + '
'+
                      'Avg. Partisan Score: ' + thisPartisan))
    house_rep_color.append('rgb(247, 59, 59)')

# create traces in plotly for each party
trace0 = go.Scatter(
    x=house_dem_sponLeg + np.random.rand(len(house_dem_sponLeg)),
    y=house_dem_avgPartisan,
    mode='markers',
    name='Democrats',
    text=house_dem_hover_text,
    marker=dict(
        color=house_dem_color,
        size=house_dem_avgCospon,
        sizemode='area',
        sizeref=2.*max(house_rep_avgCospon)/(40.**2),
        sizemin=4
    )
)

trace1 = go.Scatter(
    x=house_rep_sponLeg + np.random.rand(len(house_rep_sponLeg)),
    y=house_rep_avgPartisan,
    mode='markers',
    name='Republicans',
    text=house_rep_hover_text,
    marker=dict(
        color=house_rep_color,
        size=house_rep_avgCospon,
        sizemode='area',
        sizeref=2.*max(house_rep_avgCospon)/(40.**2),
        sizemin=4
    )
)

data = [trace0, trace1]

# create plotly layout
layout = go.Layout(
    title='Productivity and Partisanship in the House',
    hovermode='closest',
    xaxis=dict(
        title='Number of sponsored bills since 2017',
        gridcolor='rgb(255, 255, 255)',
        range=[0, 70],
        zerolinewidth=1,
        ticklen=5,
        gridwidth=2,
    ),
    yaxis=dict(
        title='Average partisan score of sponsored bills',
        gridcolor='rgb(255, 255, 255)',
        range=[-1.1, 1.1],
        zerolinewidth=1,
        ticklen=0.2,
        gridwidth=0.1,
    ),
    paper_bgcolor='rgb(243, 243, 243)',
    plot_bgcolor='rgb(243, 243, 243)',
)

# plot using plotly
fig = go.Figure(data=data, layout=layout)
plotly.offline.plot(fig, filename='../Figures/HouseBubble.html')

####################### Senate bubble plot ###########################
# create lists with hover text
senate_dem_hover_text = list()
senate_dem_color = list()

for i in range(0,len(senate_dem_name)):
    thisName = senate_dem_name[i]
    thisParty = 'Democrat'
    thisNumOfSpon = str(senate_dem_sponLeg[i])
    thisAvgCospon = str(senate_dem_avgCospon[i])
    thisPartisan = str(senate_dem_avgPartisan[i])
    senate_dem_hover_text.append(('Name: ' + thisName + '
'+
                      'Party: ' + thisParty + '
'+
                      '# of Sponsored Bills: ' + thisNumOfSpon + '
'+
                      'Avg. # of Cosponsors: ' + thisAvgCospon + '
'+
                      'Avg. Partisan Score: ' + thisPartisan))
    senate_dem_color.append('rgb(56, 94, 244)')

senate_rep_hover_text = list()
senate_rep_color = list()

for i in range(0,len(senate_rep_name)):
    thisName = senate_rep_name[i]
    thisParty = 'Republican'
    thisNumOfSpon = str(senate_rep_sponLeg[i])
    thisAvgCospon = str(senate_rep_avgCospon[i])
    thisPartisan = str(senate_rep_avgPartisan[i])
    senate_rep_hover_text.append(('Name: ' + thisName + '
'+
                      'Party: ' + thisParty + '
'+
                      '# of Sponsored Bills: ' + thisNumOfSpon + '
'+
                      'Avg. # of Cosponsors: ' + thisAvgCospon + '
'+
                      'Avg. Partisan Score: ' + thisPartisan))
    senate_rep_color.append('rgb(247, 59, 59)')

# create traces for plotly
trace0 = go.Scatter(
    x=senate_dem_sponLeg + np.random.rand(len(senate_dem_sponLeg)),
    y=senate_dem_avgPartisan,
    mode='markers',
    name='Democrats',
    text=senate_dem_hover_text,
    marker=dict(
        color=senate_dem_color,
        size=senate_dem_avgCospon,
        sizemode='area',
        sizeref=2.*max(senate_rep_avgCospon)/(40.**2),
        sizemin=4
    )
)

trace1 = go.Scatter(
    x=senate_rep_sponLeg + np.random.rand(len(senate_rep_sponLeg)),
    y=senate_rep_avgPartisan,
    mode='markers',
    name='Republicans',
    text=senate_rep_hover_text,
    marker=dict(
        color=senate_rep_color,
        size=senate_rep_avgCospon,
        sizemode='area',
        sizeref=2.*max(senate_rep_avgCospon)/(40.**2),
        sizemin=4
    )
)

data = [trace0, trace1]

# create plotly layout
layout = go.Layout(
    title='Productivity and Partisanship in the Senate',
    hovermode='closest',
    xaxis=dict(
        title='Number of sponsored bills since 2017',
        gridcolor='rgb(255, 255, 255)',
        range=[0, 70],
        zerolinewidth=1,
        ticklen=5,
        gridwidth=2,
    ),
    yaxis=dict(
        title='Average partisan score of sponsored bills',
        gridcolor='rgb(255, 255, 255)',
        range=[-1.1, 1.1],
        zerolinewidth=1,
        ticklen=0.2,
        gridwidth=0.1,
    ),
    paper_bgcolor='rgb(243, 243, 243)',
    plot_bgcolor='rgb(243, 243, 243)',
)

# plot using plotly
fig = go.Figure(data=data, layout=layout)
plotly.offline.plot(fig, filename='../Figures/SenateBubble.html')


text:
import math
import numpy as np

from circuits.circuit_pack import aRCb

def cal_ChiSquare_0(z_arr, z_sim_arr, weight_type='modulus'):
    """
    :param
        weight_type: str
            'modulus': default
                Wreal = Wimag = 1 / ||Zi||
            'unity':
                Wreal = Wimag = 1
            'proportional':
                Wreal =
                Wimag =
    :return:
    """
    # weight_arr = [data_number * 2]
    weight_arr = np.zeros((z_arr.shape[0], 2))
    if weight_type == 'modulus':  # default
        for i, z in enumerate(z_arr):
            z_norm2 = 1.0 / (z.real ** 2 + z.imag ** 2)
            weight_arr[i, :] = [z_norm2, z_norm2]
    elif weight_type == 'proportional':
        # 尚不清楚
        weight_arr = None
    elif weight_type == 'unity':
        weight_arr = np.ones((z_arr.shape[0], 2))

    chi_square = 0.0
    for weight, z, z_sim in zip(weight_arr, z_arr, z_sim_arr):
        chi_square += weight[0] * ((z.real - z_sim.real) ** 2) + weight[1] * ((z.imag - z_sim.imag) ** 2)
    return chi_square

def cal_ZSimpWin_ChiSquare(data_num, para_num, z_arr, z_sim_arr, weight_type='modulus'):
    chi_square = cal_ChiSquare_0(z_arr, z_sim_arr, weight_type)

    # N测试的阻抗点数，如赖师兄N=30
    N = data_num
    # M为等效电路中需要拟合的参数数目，CPE元件包含两个参数Y和n
    M = para_num
    # v为系统的自由度，严格讲 v = N - M - 1，此处为了和ZSimpWIn一样，令 v = N - M
    v = N - M

    return chi_square / v

def cal_residual(impSpe, R_arr, tao_arr, obj_fun_mode='imag', obj_fun_weighting_type='modulus') -> np.array:
    """
    Used For Linear-KK in ECM-Vogit
    residual = (观测数据 - vogit拟合的数据) * 权重
    :param
        obj_fun_mode: str
            'real' obj_fun == loss_fun == measure the error between the fitted and experimental Z_real data
            'imag' obj_fun == loss_fun == measure the error between the fitted and experimental Z_imag data
            'both' obj_fun == loss_fun == measure the error between the fitted and experimental Z_real_and_imag data
        obj_fun_weighting_type: str
            'unity':
                Wreal = Wimag = 1
            'modulus':
                Wreal = Wimag = 1 / (z.real ** 2 + z.imag ** 2)
            'proportional':
                Wreal = Wimag = 不会写
        tao_arr： tao = time constant
        R_arr: ndarray(complex, N*0)
            [R0, R1, ..., R_M-1] or
            [Rs, R0, R1, ..., R_M-1]
        w_list: ndarray(complex, N*0)
        RC_para_list:
    :return:
        f(x, para) = weight * [(Z_sim_imag - Z_imag) ** 2]
        chiSquare_pointWise_arr: ndarray(complex, N*0, N=data points)
    """
    z_arr = impSpe.z_arr
    w_arr = impSpe.w_arr

    if obj_fun_mode == 'imag':
        # R_arr = [R0, R1, ..., R_M-1]
        RC_para_list = [[R, tao / R] for R, tao in zip(R_arr, tao_arr)]

        z_sim_arr = np.empty(shape=(len(RC_para_list), z_arr.shape[0]), dtype=complex)
        for i, RC_list in enumerate(RC_para_list):
            R, C = RC_list
            tmp_z_sim_list = [aRCb(w, R0=R, C0=C) for w in w_arr]
            # IndexError: too many indices for array: array is 1-dimensional, but 2 were indexed
            z_sim_arr[i, :] = tmp_z_sim_list
        z_sim_arr = z_sim_arr.sum(axis=0)

    elif (obj_fun_mode == 'real') or (obj_fun_mode == 'both'):
        # R_arr = [*Rs*, R0, R1, ..., R_M-1]
        Rs = R_arr[0]
        RC_para_list = [[R, tao / R] for R, tao in zip(R_arr[1:], tao_arr)]

        # -------------- 计算M个RC各自产生的阻抗 --------------
        z_sim_arr = np.empty(shape=(len(RC_para_list), z_arr.shape[0]), dtype=complex)
        for i, RC_list in enumerate(RC_para_list):
            R, C = RC_list
            tmp_z_sim_list = [aRCb(w, R0=R, C0=C) for w in w_arr]
            # IndexError: too many indices for array: array is 1-dimensional, but 2 were indexed
            z_sim_arr[i, :] = tmp_z_sim_list
        # -------------- 计算M个RC各自产生的阻抗 --------------
        # 合并M个RC各自产生的阻抗
        z_sim_arr = z_sim_arr.sum(axis=0)

        # 合并Rs + M个RC各自产生的阻抗
        z_sim_arr += Rs

    residual_list = []
    for sim_z, z in zip(z_sim_arr, z_arr):
        if obj_fun_mode == 'imag':
            # imag_residual = (z.imag - sim_z.imag) / np.sqrt(z.imag**2 + z.real**2)
            residual = (z.imag - sim_z.imag) / np.sqrt(z.imag**2 + z.real**2)
        elif obj_fun_mode == 'real':
            # real_residual = (z.real - sim_z.real) / np.sqrt(z.imag**2 + z.real**2)
            residual = (z.real - sim_z.real) / np.sqrt(z.imag**2 + z.real**2)
        elif obj_fun_mode == 'both':
            # both_residual = (z - sim_z) / np.sqrt(z.imag**2 + z.real**2)
            residual = (z - sim_z) / np.sqrt(z.imag**2 + z.real**2)
        residual_list.append(residual)
    return np.array(residual_list)

def cal_ChiSquare_pointWise_0(impSpe, R_arr, tao_arr, obj_fun_mode='imag', obj_fun_weighting_type='modulus') -> np.array:
    """
    Used For Linear-KK in ECM-Vogit
    :param
        obj_fun_mode: str
            'real' obj_fun == loss_fun == measure the error between the fitted and experimental Z_real data
            'imag' obj_fun == loss_fun == measure the error between the fitted and experimental Z_imag data
            'both' obj_fun == loss_fun == measure the error between the fitted and experimental Z_real_and_imag data
        obj_fun_weighting_type: str
            'unity':
                Wreal = Wimag = 1
            'modulus':
                Wreal = Wimag = 1 / (z.real ** 2 + z.imag ** 2)
            'proportional':
                Wreal = Wimag = 不会写
        tao_arr： tao = time constant
        R_arr: ndarray(complex, N*0)
            [R0, R1, ..., R_M-1] or
            [Rs, R0, R1, ..., R_M-1]
        w_list: ndarray(complex, N*0)
        RC_para_list:
    :return:
        f(x, para) = weight * [(Z_sim_imag - Z_imag) ** 2]
        chiSquare_pointWise_arr: ndarray(complex, N*0, N=data points)
    """
    z_arr = impSpe.z_arr
    w_arr = impSpe.w_arr

    if obj_fun_mode == 'imag':
        # R_arr = [R0, R1, ..., R_M-1]
        RC_para_list = [[R, tao / R] for R, tao in zip(R_arr, tao_arr)]

        z_sim_arr = np.empty(shape=(len(RC_para_list), z_arr.shape[0]), dtype=complex)
        for i, RC_list in enumerate(RC_para_list):
            R, C = RC_list
            tmp_z_sim_list = [aRCb(w, R0=R, C0=C) for w in w_arr]
            # IndexError: too many indices for array: array is 1-dimensional, but 2 were indexed
            z_sim_arr[i, :] = tmp_z_sim_list
        z_sim_arr = z_sim_arr.sum(axis=0)

    elif (obj_fun_mode == 'real') or (obj_fun_mode == 'both'):
        # R_arr = [*Rs*, R0, R1, ..., R_M-1]
        Rs = R_arr[0]
        RC_para_list = [[R, tao / R] for R, tao in zip(R_arr[1:], tao_arr)]

        # -------------- 计算M个RC各自产生的阻抗 --------------
        z_sim_arr = np.empty(shape=(len(RC_para_list), z_arr.shape[0]), dtype=complex)
        for i, RC_list in enumerate(RC_para_list):
            R, C = RC_list
            tmp_z_sim_list = [aRCb(w, R0=R, C0=C) for w in w_arr]
            # IndexError: too many indices for array: array is 1-dimensional, but 2 were indexed
            z_sim_arr[i, :] = tmp_z_sim_list
        # -------------- 计算M个RC各自产生的阻抗 --------------
        # 合并M个RC各自产生的阻抗
        z_sim_arr = z_sim_arr.sum(axis=0)

        # 合并Rs + M个RC各自产生的阻抗
        z_sim_arr += Rs

    chiSquare_pointWise_list = []
    for sim_z, z in zip(z_sim_arr, z_arr):
        if obj_fun_mode == 'imag':
            chiSquare_pointWise = (sim_z.imag - z.imag) ** 2
        elif obj_fun_mode == 'real':
            chiSquare_pointWise = (sim_z.real - z.real) ** 2
        elif obj_fun_mode == 'both':
            chiSquare_pointWise = (sim_z.imag - z.imag) ** 2 + (sim_z.real - z.real) ** 2
        chiSquare_pointWise_list.append(chiSquare_pointWise)

    chiSquare_pointWise_arr = None
    if obj_fun_weighting_type == 'modulus':
        # paper: A Linear Kronig-Kramers Transform Test for Immittance Data Validation - Eq 13
        weight_list = [1 / (z.real ** 2 + z.imag ** 2) for z in z_arr]
        weighted_chiSquare_pointWise_list = [weight * zimag_residual for weight, zimag_residual in
                                             zip(weight_list, chiSquare_pointWise_list)]
        chiSquare_pointWise_arr = np.array(weighted_chiSquare_pointWise_list)
    elif obj_fun_weighting_type == 'proportional':
        pass
    elif obj_fun_weighting_type == 'unity':
        chiSquare_pointWise_arr = np.array(chiSquare_pointWise_list)

    return chiSquare_pointWise_arr

ext: py

reason: import numpy


text:
import numpy as np
from matplotlib import pyplot as plt

def fit(x, y):
    m, c = np.polyfit(x, y, 1)
    f = lambda a: np.add(np.multiply(a, m), c)
    return m, c, f

def plot_diagram(points):

    gas_liquid, liquid_solid = points

    glt, glp = zip(*gas_liquid)
    lst, lsp = zip(*liquid_solid)

    P = np.power(10.0, np.linspace(-1, 2))

    m, c, f = fit(glp, glt)
    plt.plot(f(P), P, label=f'gas-liquid $m={m:.1e}$, $c={c:.1e}$')
    m, c, f = fit(lsp, lst)
    plt.plot(f(P), P, label=f'liquid-solid $m={m:.1e}$, $c={c:.1e}$')

def pa2atm(pa):
    return pa / 101325.0

def bar2atm(bar):
    return bar / 1.01325

if __name__ == '__main__':

    # P in atm, T in K
    data = [
        ('H', [
            ((13.8033, pa2atm(7041.0)), (32.938, pa2atm(1285800.0))),
            ((13.8033, pa2atm(7041.0)), (20.0, bar2atm(50.0)))
            ]),
        ('He', [
            ((2.17, 0.08), (4.22, 1.0)),
            ((1.9, 50.0), (3.0, 80.0))
            ]),
    ]

    i = len(data)-1

    for symbol, points in data[i:i+1]:
        plot_diagram(points)
        plt.yscale('log')
        plt.xscale('log')
        plt.title(symbol)
        plt.ylabel('Pressure, $\\ P\\rm\\ [atm]$')
        plt.xlabel('Temperature, $\\ T\\rm\\ [K]$')
    plt.legend()
    plt.savefig('state_diagram.pdf')

max_stars_repo_name: cbosoft/aite

max_issues_repo_name: cbosoft/aite

max_forks_repo_name: cbosoft/aite


text:
import numpy as np
from gym.spaces import Box

from metaworld.envs.asset_path_utils import full_v1_path_for
from metaworld.envs.mujoco.sawyer_xyz.sawyer_xyz_env import SawyerXYZEnv, _assert_task_is_set

class SawyerTwoDrawersOpenEnv(SawyerXYZEnv):
    def __init__(self):

        super().__init__(
            self.model_name,
            hand_low=(-0.5, 0.40, 0.05),
            hand_high=(0.5, 1, 0.5),
        )

        self.drawer1_base_pos = np.array([-0.17, 0.9, 0.04])
        self.drawer2_base_pos = np.array([ 0.17, 0.9, 0.04])

        #obj_init_pos indicates the extent to which each of the drawers are open
        self.init_config = {
            'obj_init_pos': np.array([0, 0], dtype=np.float32),
            'hand_init_pos': np.array([0, 0.6, 0.2], dtype=np.float32),
        }

        self.obj_init_pos  = self.init_config['obj_init_pos']
        self.hand_init_pos = self.init_config['hand_init_pos']
        self.goal = np.array([0.1,0.1])

    @property
    def model_name(self):
        return full_v1_path_for('sawyer_xyz/sawyer_two_drawers.xml')

    @_assert_task_is_set
    def step(self, action):
        ob = super().step(action)
        pullDist1 =  np.abs(ob[3:6] - self._target_pos1)[1]
        pullDist2 =  np.abs(ob[6:9] - self._target_pos2)[1]
        reward = -pullDist1 - pullDist2

        self.curr_path_length += 1
        info = {
            'pullDist1': pullDist1,
            'pullDist2': pullDist2,
            'goalDist' : pullDist1 + pullDist2,
            'success': float(pullDist1 <= 0.08 and pullDist2 <= 0.08)
        }

        return ob, reward, False, info

    def _get_pos_objects(self):
        drawer1 = self.data.get_body_xpos('drawer1').copy()
        drawer2 = self.data.get_body_xpos('drawer2').copy()
        return np.concatenate([drawer1, drawer2])

    def _get_obs_dict(self):
        obs_dict = super()._get_obs_dict()
        obs_dict['state_achieved_goal'] = (self._get_site_pos('handleStart').copy() + self.data.get_geom_xpos('drawer_wall2').copy()) / 2
        return obs_dict

    def reset_model(self):
        self._reset_hand()
        self._target_pos = self._target_pos1 = self.drawer1_base_pos - np.array([0, self.goal[0], 0])
        self._target_pos2 = self.drawer2_base_pos - np.array([0, self.goal[1], 0])

        self.sim.model.body_pos[self.model.body_name2id('drawer1')] = self.drawer1_base_pos - np.array([0,self.obj_init_pos[0], 0])
        self.sim.model.body_pos[self.model.body_name2id('drawer2')] = self.drawer2_base_pos - np.array([0,self.obj_init_pos[1], 0])

        self.maxPullDist = 0.4
        return self._get_obs()

    def _reset_hand(self):
        super()._reset_hand(10)

        rightFinger, leftFinger = self._get_site_pos('rightEndEffector'), self._get_site_pos('leftEndEffector')
        self.init_fingerCOM  =  (rightFinger + leftFinger)/2
        self.reachCompleted = False

max_stars_repo_name: russellmendonca/metaworld

max_issues_repo_name: russellmendonca/metaworld

max_forks_repo_name: russellmendonca/metaworld


text:
import os
import numpy as np  # type: ignore
import unittest

from . import Image, Point

class TestImage(unittest.TestCase):
    """
    Tests for the Image class.
    """

    load_path = os.path.join(os.path.dirname(__file__), '..', 'textures', 'test.png')
    save_path = os.path.join(os.path.dirname(__file__), '..', 'textures', 'save.png')

    def test_load_save(self):
        """
        Tests the Image.load() and Image.save() functions.
        """
        image = Image(TestImage.load_path)
        image.load()

        have_pixels = image._Image__pixels
        have_filled = image._Image__filled

        want_size = np.array([3, 4])
        want_shape = np.array([3, 4, 3])
        want_pixels = np.array([
            [[255, 0, 0], [0, 255, 0], [0, 0, 255], [255, 255, 255]],
            [[255, 255, 255], [0, 0, 0], [0, 0, 0], [255, 255, 255]],
            [[255, 255, 255], [0, 0, 255], [0, 255, 0], [255, 0, 0]],
        ])
        want_filled = np.full((3, 4), True)

        self.assertEqual(image.width, 4, f'Load [Width]: Have = {image.width}, Want = {4}.')
        self.assertEqual(image.height, 3, f'Load [Height]: Have = {image.height}, Want = {3}.')
        self.assertEqual(image.area, 12, f'Load [Area]: Have = {image.area}, Want = {12}.')
        self.assertTrue(np.all(image.size == want_size), f'Load [Size]: Have = {image.size}, Want = {want_size}.')
        self.assertTrue(np.all(image.shape == want_shape), f'Load [Shape]: Have = {image.shape}, Want = {want_shape}.')
        self.assertTrue(np.all(have_pixels == want_pixels), f'Load [Pixels]: Have = {have_pixels}, Want = {want_pixels}.')
        self.assertTrue(np.all(have_filled == want_filled), f'Load [Filled]: Have = {have_filled}, Want = {want_filled}.')

        image._Image__path = TestImage.save_path
        image.save()
        clone = Image(TestImage.save_path)
        clone.load()

        self.assertTrue(np.all(image._Image__pixels == clone._Image__pixels), f'Save [Pixels]: Have = {image._Image__pixels}, Want = {clone._Image__pixels}.')
        self.assertTrue(np.all(image._Image__filled == clone._Image__filled), f'Save [Filled]: Have = {image._Image__filled}, Want = {clone._Image__filled}.')

        os.remove(TestImage.save_path)

    def test_create(self):
        """
        Tests the Image.create() function.
        """
        tests = [
            (1, 1),
            (3, 4),
        ]
        for i, (width, height) in enumerate(tests):
            image = Image()
            image.create(width, height)

            have_pixels = image._Image__pixels
            have_filled = image._Image__filled

            want_area = width * height
            want_size = np.array([height, width])
            want_shape = np.array([height, width, 3])
            want_pixels = np.full((height, width, 3), 0)
            want_filled = np.full((height, width), False)

            self.assertEqual(image.width, width, f'Test {i} [Width]: Have = {image.width}, Want = {width}.')
            self.assertEqual(image.height, height, f'Test {i} [Height]: Have = {image.height}, Want = {height}.')
            self.assertEqual(image.area, want_area, f'Test {i} [Area]: Have = {image.area}, Want = {want_area}.')
            self.assertTrue(np.all(image.size == want_size), f'Test {i} [Size]: Have = {image.size}, Want = {want_size}.')
            self.assertTrue(np.all(image.shape == want_shape), f'Test {i} [Shape]: Have = {image.shape}, Want = {want_shape}.')
            self.assertTrue(np.all(have_pixels == want_pixels), f'Test {i} [Pixels]: Have = {have_pixels}, Want = {want_pixels}.')
            self.assertTrue(np.all(have_filled == want_filled), f'Test {i} [Filled]: Have = {have_filled}, Want = {want_filled}.')

    def test_copy(self):
        """
        Tests the Image.copy() function.
        """
        image = Image(TestImage.load_path)
        image.load()
        clone = image.copy()

        have_path = clone._Image__path
        have_pixels = clone._Image__pixels
        have_filled = clone._Image__filled

        want_path = image._Image__path
        want_pixels = image._Image__pixels
        want_filled = image._Image__filled

        self.assertEqual(have_path, want_path, f'Before [Path]: Have = {have_path}, Want = {want_path}.')
        self.assertTrue(np.all(have_pixels == want_pixels), f'Before [Pixels]: Have = {have_pixels}, Want = {want_pixels}.')
        self.assertTrue(np.all(have_filled == want_filled), f'Before [Filled]: Have = {have_filled}, Want = {want_filled}.')

        image[:, :] = 255 - image[:, :]
        self.assertFalse(np.all(have_pixels == want_pixels), f'After [Pixels]: Have = {have_pixels}, Want = {want_pixels}.')

    def test_center(self):
        """
        Tests the Image.center() function.
        """
        tests = [
            ((1, 1), Point(0, 0)),
            ((5, 5), Point(2, 2)),
            ((4, 4), Point(2, 2)),
            ((3, 8), Point(1, 4)),
        ]
        for i, ((width, height), want) in enumerate(tests):
            image = Image()
            image.create(width, height)
            have = image.center
            self.assertEqual(have, want, f'Test {i}: Have = {have}, Want = {want}.')

    def test_clamp(self):
        """
        Tests the Image.clamp() function.
        """
        tests = [
            ((2, 3), Point(0, 0), Point(0, 0)),
            ((2, 3), Point(1, 2), Point(1, 2)),
            ((2, 3), Point(3, 0), Point(1, 0)),
            ((2, 3), Point(0, 4), Point(0, 2)),
            ((2, 3), Point(-2, 1), Point(0, 1)),
            ((2, 3), Point(1, -2), Point(1, 0)),
        ]
        for i, ((width, height), point, want) in enumerate(tests):
            image = Image()
            image.create(width, height)
            have = image.clamp(point)
            self.assertEqual(have, want, f'Test {i}: Have = {have}, Want = {want}.')

    def test_corners(self):
        """
        Tests the Image.corners() function.
        """
        tests = [
            ((1, 1), Point(0, 0), Point(0, 0)),
            ((3, 4), Point(0, 0), Point(2, 3)),
        ]
        for i, ((width, height), want_min_corner, want_max_corner) in enumerate(tests):
            image = Image()
            image.create(width, height)
            have_min_corner, have_max_corner = image.corners()
            self.assertEqual(have_min_corner, want_min_corner, f'Test {i} [Min]: Have = {have_min_corner}, Want = {want_min_corner}.')
            self.assertEqual(have_max_corner, want_max_corner, f'Test {i} [Max]: Have = {have_max_corner}, Want = {want_max_corner}.')

    def test_cover(self):
        """
        Tests the Image.cover() function.
        """
        tests = [
            ((1, 1), 'raster', [Point(0, 0)]),
            ((3, 2), 'raster', [Point(0, 0), Point(1, 0), Point(2, 0), Point(0, 1), Point(1, 1), Point(2, 1)]),
            ((1, 1), 'spiral', [Point(0, 0)]),
            ((3, 3), 'spiral', [Point(1, 1),
                                Point(0, 2), Point(1, 2), Point(2, 2), Point(2, 1), Point(2, 0), Point(1, 0), Point(0, 0), Point(0, 1)]),
            ((3, 2), 'spiral', [Point(1, 1),
                                Point(2, 1), Point(2, 0), Point(1, 0), Point(0, 0), Point(0, 1)]),
            ((1, 3), 'spiral', [Point(0, 1),
                                Point(0, 2), Point(0, 0)]),
            ((5, 5), 'spiral', [Point(2, 2),
                                Point(1, 3), Point(2, 3), Point(3, 3), Point(3, 2), Point(3, 1), Point(2, 1), Point(1, 1), Point(1, 2),
                                Point(0, 4), Point(1, 4), Point(2, 4), Point(3, 4), Point(4, 4), Point(4, 3), Point(4, 2), Point(4, 1), Point(4, 0), Point(3, 0), Point(2, 0), Point(1, 0), Point(0, 0), Point(0, 1), Point(0, 2), Point(0, 3)]),
        ]
        for i, ((width, height), mode, want) in enumerate(tests):
            image = Image()
            image.create(width, height)
            have = [point for point in image.cover(mode)]
            self.assertEqual(have, want, f'Test {i}: Have = {have}, Want = {want}.')

    def test_extract(self):
        """
        Tests the Image.extract() function.
        """
        image = Image(TestImage.load_path)
        image.load()
        r, g, b, k, w = list(map(np.array, [[255, 0, 0], [0, 255, 0], [0, 0, 255], [0, 0, 0], [255, 255, 255]]))

        tests = [
            (Point(1, 1), 1, 'drop', np.array([[r, g, b], [w, k, k], [w, b, g]])),
            (Point(0, 0), 1, 'drop', np.array([[k, k, k], [k, r, g], [k, w, k]])),
            (Point(2, 2), 2, 'drop', np.array([[r, g, b, w, k], [w, k, k, w, k], [w, b, g, r, k], [k, k, k, k, k], [k, k, k, k, k]])),
            (Point(1, 1), 2, 'drop', np.array([[k, k, k, k, k], [k, r, g, b, w], [k, w, k, k, w], [k, w, b, g, r], [k, k, k, k, k]])),
            (Point(1, 1), 1, 'wrap', np.array([[r, g, b], [w, k, k], [w, b, g]])),
            (Point(0, 0), 1, 'wrap', np.array([[r, w, b], [w, r, g], [w, w, k]])),
            (Point(2, 2), 2, 'wrap', np.array([[r, g, b, w, r], [w, k, k, w, w], [w, b, g, r, w], [r, g, b, w, r], [w, k, k, w, w]])),
            (Point(1, 1), 1, 'reflect', np.array([[r, g, b], [w, k, k], [w, b, g]])),
            (Point(0, 0), 1, 'reflect', np.array([[k, w, k], [g, r, g], [k, w, k]])),
            (Point(2, 2), 2, 'reflect', np.array([[r, g, b, w, b], [w, k, k, w, k], [w, b, g, r, g], [w, k, k, w, k], [r, g, b, w, b]]))
        ]
        for i, (center, padding, mode, want) in enumerate(tests):
            window = image.extract(center, padding, mode)
            have = window._Image__pixels
            self.assertTrue(np.all(have == want), f'Test {i}: Have = {have}, Want = {want}.')

    def test_filled(self):
        """
        Tests the Image.filled() function.
        """
        image = Image()
        image.create(3, 3)
        image._Image__filled[:2, :2] = np.full((2, 2), True)

        tests = [
            (Point(1, 1), 1, 'drop', False, np.array([[1, 1, 0], [1, 1, 0], [0, 0, 0]])),
            (Point(0, 0), 1, 'drop', False, np.array([[0, 0, 0], [0, 1, 1], [0, 1, 1]])),
            (Point(2, 2), 2, 'drop', False, np.array([[1, 1, 0, 0, 0], [1, 1, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]])),
            (Point(1, 1), 1, 'wrap', False, np.array([[1, 1, 0], [1, 1, 0], [0, 0, 0]])),
            (Point(0, 0), 1, 'wrap', False, np.array([[0, 0, 0], [0, 1, 1], [0, 1, 1]])),
            (Point(2, 2), 2, 'wrap', False, np.array([[1, 1, 0, 1, 1], [1, 1, 0, 1, 1], [0, 0, 0, 0, 0], [1, 1, 0, 1, 1], [1, 1, 0, 1, 1]])),
            (Point(1, 1), 1, 'reflect', False, np.array([[1, 1, 0], [1, 1, 0], [0, 0, 0]])),
            (Point(0, 0), 1, 'reflect', False, np.array([[1, 1, 1], [1, 1, 1], [1, 1, 1]])),
            (Point(2, 2), 2, 'reflect', False, np.array([[1, 1, 0, 1, 1], [1, 1, 0, 1, 1], [0, 0, 0, 0, 0], [1, 1, 0, 1, 1], [1, 1, 0, 1, 1]])),
            (Point(1, 1), 1, 'drop', True, np.array([[1, 1, 0], [1, 1, 0], [0, 0, 0]])),
            (Point(0, 0), 1, 'drop', True, np.array([[0, 0, 0], [0, 1, 0], [0, 0, 0]])),
            (Point(2, 2), 2, 'wrap', True, np.array([[1, 1, 0, 1, 1], [1, 1, 0, 1, 1], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]])),
        ]
        for i, (center, padding, mode, causal, want) in enumerate(tests):
            have = image.filled(center, padding, mode, causal)
            self.assertTrue(np.all(have == want), f'Test {i}: Have = {have}, Want = {want}.')

    def test_paste(self):
        """
        Tests the Image.paste() function.
        """
        r, g, b, k, w, x = list(map(np.array, [[255, 0, 0], [0, 255, 0], [0, 0, 255], [0, 0, 0], [255, 255, 255], [1, 1, 1]]))

        tests = [
            (Point(1, 1), 1, np.array([[x, x, x, w], [x, x, x, w], [x, x, x, r]])),
            (Point(0, 0), 1, np.array([[x, x, b, w], [x, x, k, w], [w, b, g, r]])),
            (Point(4, 1), 1, np.array([[r, g, b, x], [w, k, k, x], [w, b, g, x]])),
            (Point(2, 1), 2, np.array([[x, x, x, x], [x, x, x, x], [x, x, x, x]])),
        ]
        for i, (center, padding, want) in enumerate(tests):
            window = Image()
            window.create(2 * padding + 1, 2 * padding + 1)
            window[:, :] = 1
            image = Image(TestImage.load_path)
            image.load()
            image.paste(center, window)
            have = image._Image__pixels
            self.assertTrue(np.all(have == want), f'Test {i}: Have = {have}, Want = {want}.')

    def test_project(self):
        """
        Tests the Image.project() function.
        """
        tests = [
            ((2, 3), 'wrap', Point(0, 0), Point(0, 0)),
            ((2, 3), 'wrap', Point(1, 2), Point(1, 2)),
            ((2, 3), 'wrap', Point(3, 0), Point(1, 0)),
            ((2, 3), 'wrap', Point(0, 3), Point(0, 0)),
            ((2, 3), 'wrap', Point(-2, 2), Point(0, 2)),
            ((2, 3), 'wrap', Point(1, -2), Point(1, 1)),
            ((2, 3), 'reflect', Point(0, 0), Point(0, 0)),
            ((2, 3), 'reflect', Point(1, 2), Point(1, 2)),
            ((2, 3), 'reflect', Point(2, 0), Point(0, 0)),
            ((2, 3), 'reflect', Point(3, 0), Point(1, 0)),
            ((2, 3), 'reflect', Point(0, 3), Point(0, 1)),
            ((2, 3), 'reflect', Point(0, 4), Point(0, 0)),
            ((2, 3), 'reflect', Point(-1, 2), Point(1, 2)),
            ((2, 3), 'reflect', Point(1, -2), Point(1, 2)),
        ]
        for i, ((width, height), mode, point, want) in enumerate(tests):
            image = Image()
            image.create(width, height)
            have = image.project(point, mode)
            self.assertEqual(have, want, f'Test {i}: Have = {have}, Want = {want}.')

    def test_resize(self):
        """
        Tests the Image.resize() function.
        """
        tests = [
            ((1, 1), (1, 1)),
            ((5, 5), (3, 3)),
            ((5, 5), (7, 8)),
        ]
        for i, ((old_width, old_height), (new_width, new_height)) in enumerate(tests):
            image = Image()
            image.create(old_width, old_height)
            image.resize(new_width, new_height)
            self.assertEqual(image.width, new_width, f'Test {i} [Width]: Have = {image.width}, Want = {new_width}.')
            self.assertEqual(image.height, new_height, f'Test {i} [Height]: Have = {image.height}, Want = {new_height}.')

    def test_contains(self):
        """
        Tests the Image.__contains__() function.
        """
        tests = [
            ((2, 3), Point(0, 0), True),
            ((2, 3), Point(1, 2), True),
            ((2, 3), Point(3, 0), False),
            ((2, 3), Point(0, 4), False),
            ((2, 3), Point(-2, 1), False),
            ((2, 3), Point(1, -2), False),
        ]
        for i, ((width, height), point, want) in enumerate(tests):
            image = Image()
            image.create(width, height)
            have = point in image
            self.assertEqual(have, want, f'Test {i}: Have = {have}, Want = {want}.')

    def test_eq(self):
        """
        Tests the Image.__eq__() function.
        """
        reference = Image()
        reference.create(2, 2)
        reference[:, :] = np.full((2, 2, 3), 1)

        tests = [
            (None, np.full((2, 2, 3), 1), np.full((2, 2), True), True),
            ("//", np.full((2, 2, 3), 1), np.full((2, 2), True), True),
            (None, np.full((2, 2, 3), 1), np.full((2, 2), False), True),
            (None, np.full((2, 2, 3), 1), np.full((3, 3), False), True),
            (None, np.full((2, 2, 3), 2), np.full((2, 2), True), False),
            (None, np.full((3, 3, 3), 1), np.full((2, 2), True), False),
        ]
        for i, (path, pixels, filled, want) in enumerate(tests):
            image = Image()
            image._Image__path = path
            image._Image__pixels = pixels
            image._Image__filled = filled
            have = reference == image
            self.assertEqual(have, want, f'Test {i}: Have = {have}, Want = {want}.')

    def test_items(self):
        """
        Tests the Image.__getitem__() and Image.__setitem__() functions.
        """
        image = Image()
        image.create(2, 2)
        image[0, 0] = np.array([0, 0, 0])
        image[0, 1] = np.array([1, 1, 1])
        image[1, 0] = np.array([2, 2, 2])
        image[1, 1] = np.array([3, 3, 3])

        tests = [
            (Point(0, 0), np.array([0, 0, 0]), Point(0, 0), np.array([[0, 0, 0]])),
            (Point(0, 0), np.array([0, 0, 0]), (slice(1), slice(2)), np.array([[0, 0, 0], [1, 1, 1]])),
            (Point(0, 0), np.array([4, 4, 4]), Point(0, 0), np.array([[4, 4, 4]])),
            ((slice(1, 6), slice(6)), np.array([5, 5, 5]), (slice(1, 7), slice(7)), np.array([[5, 5, 5], [5, 5, 5]])),
        ]
        for i, (set_key, colour, get_key, want) in enumerate(tests):
            image[set_key] = colour
            have = image[get_key]
            self.assertTrue(np.all(have == want), f'Test {i}: Have = {have}, Want = {want}.')

max_stars_repo_name: Mandrenkov/SVBRDF-Texture-Synthesis

max_issues_repo_name: Mandrenkov/SVBRDF-Texture-Synthesis

max_forks_repo_name: Mandrenkov/SVBRDF-Texture-Synthesis


text:
'''
Calculates and plots eddy enstrophy for OpenMARS, Isca - all years and exps.
'''

import numpy as np
import xarray as xr
import os, sys

import analysis_functions as funcs

if __name__ == "__main__":

    theta0 = 200.
    kappa = 1/4.0
    p0 = 610.

    EMARS = True
    ilev = 350

    Lsmax = 360
    Lsmin = 0

    PATH = '/export/anthropocene/array-01/xz19136/EMARS'
    files = '/*isentropic*'
    reanalysis = 'EMARS'

    d = xr.open_mfdataset(PATH+files, decode_times=False, concat_dim='time',
                           combine='nested',chunks={'time':'auto'})

    d["Ls"] = d.Ls.expand_dims({"lat":d.lat})
    #d = d.rename({"pfull":"plev"})
    #d = d.rename({"t":"tmp"})
    smooth = 200
    yearmax = 32

    d = d.sel(ilev = ilev, method='nearest').squeeze()
    d = d[["PV", "MY", "Ls"]]
    d = d.where(d.lat > 0, drop = True)
    d = d.where(d.Ls <= Lsmax, drop = True)
    d = d.where(d.Ls >= Lsmin, drop = True)

    d.to_netcdf('/export/anthropocene/array-01/xz19136/Data_Ball_etal_2021/' \
        + 'EMARS_' + 'Ls' + str(Lsmin) + '-' + str(Lsmax) + '_350K.nc')


text:
import numpy as np
import matplotlib.pyplot as plt

class MonteCarloModel:
    def __init__(self, model, monte_carlo_times):
        """
        :param model: Stochastic Model
        :param monte_carlo_times: Times at which the simulation calculates the underlying variable
        :return:
        """
        self.model = model
        self.time_vector = monte_carlo_times

        self.random_matrix = None
        self.random_indexes = None
        self.generated_path = None

        self._initialise_model()

    def _initialise_model(self):
        """ Does all the necessary calculation
        :return:
        """
        initial_value = self.model.underlying_curve.spot_rate(0)
        self.model.set_initial_state_index(initial_value)

        if len(self.model.total_stochastic_kernels) == 0:
            self.model.create_total_stochastic_kernels(self.time_vector)

        if len(self.model.total_cumulative_stochastic_kernels) == 0:
            self.model.create_total_cumulative_stochastic_kernels()

        elif (self.model.total_cumulative_stochastic_kernels.keys() !=
                  self.model.total_stochastic_kernels.keys()):
            self.model.total_cumulative_stochastic_kernels.clear()
            self.model.create_total_cumulative_stochastic_kernels()

    def _generate_random_numbers(self, n_scenarios):
        """
        Returns a vector of random numbers distributed ~ U(0, 1)
        :param n_scenarios:
        :return:
        """
        np.random.seed()
        return np.random.rand(n_scenarios)

    def _generate_monte_carlo_indexes(self, n_scenarios):
        t_vector = sorted(self.model.total_cumulative_stochastic_kernels.keys())

        if len(t_vector) == 0:
            raise RuntimeError("Produce cumulative kernels first!")

        self.random_matrix = self._generate_random_numbers(n_scenarios)
        self.random_indexes = self.binary_search(self.random_matrix, t_vector)

    def binary_search(self, random_number_vector, time_vector):
        n_scenarios = len(random_number_vector)
        n_times = len(time_vector)

        x_s_t = np.zeros((n_scenarios, n_times), dtype=np.intp)

        for t in range(n_times):
            for s in range(n_scenarios):
                x_s_t[s, t] = self._binary_search(random_number_vector[s], time_vector[t])

        return x_s_t

    def _binary_search(self, random_number, t):
        raise NotImplementedError()

    def generate_monte_carlo_path(self, n_scenarios, update_random_matrix=True):
        raise NotImplementedError()

    def plot(self, title=""):
        fig = plt.figure()

        t_vector = sorted(self.model.total_cumulative_stochastic_kernels.keys())

        ax = fig.add_subplot(121)
        ax.set_title("Simulation")

        curve = []
        for t in t_vector:
            curve.append(self.model.underlying_curve.spot_rate(t))

        ax.plot(t_vector, curve, 'bo-')

        for s in range(np.shape(self.generated_path)[0]):
            ax.plot(t_vector, self.generated_path[s], 'r--')

        ax = fig.add_subplot(122)
        ax.set_title("PDF")

        for t in t_vector:
            ax.plot(self.model.grid,
                    self.model.total_stochastic_kernels[t][self.model.x0, :], label=str(t))

        ax.legend()

        plt.suptitle(title)

        plt.show()

max_stars_repo_name: pmontalb/MarkovModels

max_issues_repo_name: pmontalb/MarkovModels

max_forks_repo_name: pmontalb/MarkovModels


text:
from pathlib import Path
import sys
from datetime import datetime
import argparse

sys.path.append("..")
sys.path.append("/workspace/aseeo-research")

import torch
from svox2 import *
import RLResearch.utils.gen_utils as gu
import numpy as np

def get_pointcloud(args):
    checkpoint = args.checkpoint#"/workspace/svox2/opt/ckpt/tttest/ckpt.npz"
    cuda_device = 1
    grid = SparseGrid.load(checkpoint, cuda_device)

    # # Single point
    # data_points = torch.tensor([[0, 0, 0]], device=torch.device('cuda:1'), dtype=torch.float32)
    # density, color = grid.sample(data_point)

    # Grid

    grid_res = torch.tensor([50, 50, 50])

    xx = torch.linspace(0, grid.links.shape[0],grid_res[0] )
    yy = torch.linspace(0, grid.links.shape[1],grid_res[1] )
    zz = torch.linspace(0, grid.links.shape[2],grid_res[2] )

    grid_mesh = torch.meshgrid(xx, yy, zz)
    grid_points = torch.cat((grid_mesh[0][...,None], grid_mesh[1][...,None], grid_mesh[2][...,None]), 3)
    num_voxels = grid_points.shape[0] * grid_points.shape[1] * grid_points.shape[2]
    grid_points = grid_points.reshape((num_voxels, 3))
    grid_points = torch.tensor(grid_points, device=torch.device('cuda:1'), dtype=torch.float32)

    density, color = grid.sample(grid_points, grid_coords=True)

    # Filter
    thres = 0
    indices = density[:,0] > thres
    indices = indices.nonzero()

    filtered_density = density[indices,0]
    filtered_points = grid_points[indices[:,0], ...]

    #Reshape 27 into 3 times 9
    color_rs = color.reshape(-1, 3, grid.basis_dim)
    filtered_colors = color_rs[indices[:,0], ...]
    rgb_colors = utils.SH_C0 * filtered_colors[:,:,0]
    rgb_color_factor = 10
    rgb_colors = torch.tensor(rgb_colors)
    print("Grid shape", grid.links.shape)
    # fn = 'test-%s.vox'%datetime.now().isoformat().replace(':', '_') + "_" + str(grid.links.shape[0])
    exp_name = Path(checkpoint).parent.parent
    result_path = Path(checkpoint).parent.parent/"result"
    result_path.mkdir(exist_ok=True)
    output_path = result_path / ( exp_name.name + str(grid.links.shape[0]))
    data_file = output_path.with_suffix(".ply")
    gu.write_point_cloud(np.array(filtered_points.cpu()), colors=np.array(rgb_colors.cpu()), filename=data_file, color_range=255)

if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('--checkpoint', type=str, help="Checkpoint folder")
    # parser.add_argument('--output_folder', type=str, help="output folder")

    args = parser.parse_args()

    get_pointcloud(args)


text:
# -*- coding: utf-8 -*-

import chainer
from chainer import serializers
import sys
import numpy as np
import cv2
import copy
from argparse import ArgumentParser

from model import Model
from model import Classifier

ap = ArgumentParser(description='python main_cap.py')
ap.add_argument('--model', '-m', nargs='?', default='0', help='Specify loading file path of learned Model')
ap.add_argument('--gpu', '-g', type=int, default=-1, help='Specify GPU ID (negative value indicates CPU)')
ap.add_argument('--frame', '-f', type=int, default=3, help='Specify Grouping frame rate (default = 3)')
ap.add_argument('--method', '-d', type=int, default=1, help='Specify Method Flag (1 : Haarcascades Frontalface Default, 2 : Haarcascades Frontalface Alt1, 3 : Haarcascades Frontalface Alt2, Without : Haarcascades Frontalface Alt Tree)')

args = ap.parse_args()

# GPU use flag
print 'GPU: {}'.format(args.gpu)

if __name__ == '__main__':

    group = ['Ravenclaw', 'Gryffindor', 'Hufflpuff', 'Slytherin']

    model = Model()
    if not args.model == '0':
        try:
            serializers.load_hdf5(args.model, model)
            print 'Loading Model : ' + args.model
        except:
            print 'ERROR!!'
            print 'Usage : Input File Path of Model (ex ./hoge.model)'
            sys.exit()
    clf = Classifier(model)

    if args.method == 1:
        face_cascade = cv2.CascadeClassifier('haarcascade/haarcascade_frontalface_default.xml')
    elif args.method == 2:
        face_cascade = cv2.CascadeClassifier('haarcascade/haarcascade_frontalface_alt.xml')
    elif args.method == 3:
        face_cascade = cv2.CascadeClassifier('haarcascade/haarcascade_frontalface_alt2.xml')
    else:
        face_cascade = cv2.CascadeClassifier('haarcascade/haarcascade_frontalface_alt_tree.xml')
    cap = cv2.VideoCapture(0)
    cap.set(3,640)
    cap.set(4,480)
    cnt = 0
    while(True):
        ret, frame = cap.read()
        if ret == False:
            break
        else:
            if cnt % args.frame == 0:
                gImg = cv2.cvtColor(frame, cv2.COLOR_RGB2GRAY)
                faces = face_cascade.detectMultiScale(gImg, 1.3, 5)
                if len(faces) > 0:
                    for num in range(len(faces)):
                        cropImg = copy.deepcopy(frame[faces[num][1]:faces[num][1]+faces[num][3], faces[num][0]:faces[num][0]+faces[num][2]])
                        resizeImg = cv2.resize(cropImg, (100, 100))
                        resizeImg = resizeImg.astype(np.float32) / resizeImg.max()
                        testImg = copy.deepcopy(resizeImg.reshape(1, 100, 100, 3).transpose(0, 3, 1, 2))
                        pre = clf.predictor(testImg, train=False)

                        text = group[np.argmax(pre.data)]
                        x, y, w, h = faces[num]
                        if np.argmax(pre.data) == 0:  # Re
                            color = (255, 0, 0)
                        elif np.argmax(pre.data) == 1:  # Gr
                            color = (0, 0, 255)
                        elif np.argmax(pre.data) == 2:  # Hu
                            color = (0, 255, 255)
                        elif np.argmax(pre.data) == 3:  # Sl
                            color = (0, 255, 0)
                        else:
                            color = (0, 0, 0)
                        cv2.rectangle(frame, (x, y), (x+w, y+h), color, 2)
                        cv2.putText(frame, text, (x+(w/5), y+h+30), cv2.FONT_HERSHEY_COMPLEX, 1.0, color, thickness=3)
                cv2.imshow('fram', frame)
            cnt += 1

        if cv2.waitKey(1) & 0xFF == ord('q'):
            break

    cap.release()
    cv2.destroyAllWindows()

max_stars_repo_name: tokkuman/GroupingNN

max_issues_repo_name: tokkuman/GroupingNN

max_forks_repo_name: tokkuman/GroupingNN


text:
# Copyright 2021 DeepMind Technologies Limited. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
"""Utility functions for conversion between different types."""

from typing import Optional, Union

import chex
from distrax._src.bijectors import bijector
from distrax._src.bijectors import bijector_from_tfp
from distrax._src.bijectors import lambda_bijector
from distrax._src.bijectors import sigmoid
from distrax._src.bijectors import tanh
from distrax._src.bijectors import tfp_compatible_bijector
from distrax._src.distributions import distribution
from distrax._src.distributions import distribution_from_tfp
from distrax._src.distributions import tfp_compatible_distribution
import jax
import jax.numpy as jnp
from tensorflow_probability.substrates import jax as tfp

tfb = tfp.bijectors
tfd = tfp.distributions

Array = chex.Array
Numeric = chex.Numeric
BijectorLike = bijector.BijectorLike
DistributionLike = distribution.DistributionLike

def to_tfp(obj: Union[bijector.Bijector, tfb.Bijector,
                      distribution.Distribution, tfd.Distribution],
           name: Optional[str] = None):
  """Converts a distribution or bijector to a TFP-compatible equivalent object.

  The returned object is not necessarily of type `tfb.Bijector` or
  `tfd.Distribution`; rather, it is a Distrax object that implements TFP
  functionality so that it can be used in TFP.

  If the input is already of TFP type, it is returned unchanged.

  Args:
    obj: The distribution or bijector to be converted to TFP.
    name: The name of the resulting object.

  Returns:
    A TFP-compatible equivalent distribution or bijector.
  """
  if isinstance(obj, (tfb.Bijector, tfd.Distribution)):
    return obj
  elif isinstance(obj, bijector.Bijector):
    return tfp_compatible_bijector.tfp_compatible_bijector(obj, name)
  elif isinstance(obj, distribution.Distribution):
    return tfp_compatible_distribution.tfp_compatible_distribution(obj, name)
  else:
    raise TypeError(
        f"`to_tfp` can only convert objects of type: `distrax.Bijector`,"
        f" `tfb.Bijector`, `distrax.Distribution`, `tfd.Distribution`. Got type"
        f" `{type(obj)}`.")

def as_bijector(obj: BijectorLike) -> bijector.BijectorT:
  """Converts a bijector-like object to a Distrax bijector.

  Bijector-like objects are: Distrax bijectors, TFP bijectors, and callables.
  Distrax bijectors are returned unchanged. TFP bijectors are converted to a
  Distrax equivalent. Callables are wrapped by `distrax.Lambda`, with a few
  exceptions where an explicit implementation already exists and is returned.

  Args:
    obj: The bijector-like object to be converted.

  Returns:
    A Distrax bijector.
  """
  if isinstance(obj, bijector.Bijector):
    return obj
  elif isinstance(obj, tfb.Bijector):
    return bijector_from_tfp.BijectorFromTFP(obj)
  elif obj is jax.nn.sigmoid:
    return sigmoid.Sigmoid()
  elif obj is jnp.tanh:
    return tanh.Tanh()
  elif callable(obj):
    return lambda_bijector.Lambda(obj)
  else:
    raise TypeError(
        f"A bijector-like object can be a `distrax.Bijector`, a `tfb.Bijector`,"
        f" or a callable. Got type `{type(obj)}`.")

def as_distribution(obj: DistributionLike) -> distribution.DistributionT:
  """Converts a distribution-like object to a Distrax distribution.

  Distribution-like objects are: Distrax distributions and TFP distributions.
  Distrax distributions are returned unchanged. TFP distributions are converted
  to a Distrax equivalent.

  Args:
    obj: A distribution-like object to be converted.

  Returns:
    A Distrax distribution.
  """
  if isinstance(obj, distribution.Distribution):
    return obj
  elif isinstance(obj, tfd.Distribution):
    return distribution_from_tfp.distribution_from_tfp(obj)
  else:
    raise TypeError(
        f"A distribution-like object can be a `distrax.Distribution` or a"
        f" `tfd.Distribution`. Got type `{type(obj)}`.")

def as_float_array(x: Numeric) -> Array:
  """Converts input to an array with floating-point dtype.

  If the input is already an array with floating-point dtype, it is returned
  unchanged.

  Args:
    x: input to convert.

  Returns:
    An array with floating-point dtype.
  """
  if not isinstance(x, Array):
    x = jnp.asarray(x)
  if jnp.issubdtype(x.dtype, jnp.floating):
    return x
  elif jnp.issubdtype(x.dtype, jnp.integer):
    return x.astype(jnp.float_)
  else:
    raise ValueError(
        f"Expected either floating or integer dtype, got {x.dtype}.")

max_stars_repo_name: kuperov/distrax

max_issues_repo_name: kuperov/distrax

max_forks_repo_name: kuperov/distrax

reason: import jax


text:
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
import os

from lottery_ticket.foundations import model_base

class ModelWgan(model_base.ModelBase):

    def __init__(self,
                 hyperparameters,
                 input_placeholder,
                 label_placeholder,
                 presets=None,
                 masks=None):

        super(ModelWgan, self).__init__(presets=presets, masks=masks)

        def xavier_init(size):
            in_dim = size[0]
            xavier_stddev = 1. / tf.sqrt(in_dim / 2.)
            return tf.random_normal(shape=size, stddev=xavier_stddev)

        # dimensions
        mb_size = 32
        X_dim = 784
        z_dim = 10
        h_dim = 128
        lam = 10
        n_disc = 5
        self.lr = 1e-4

        # variable creation
        X = input_placeholder[0]
        z = input_placeholder[1]

        # create generator
        G_W1 = self.dense_layer(
                'g1',
                z,
                h_dim,
                activation=tf.nn.relu,
                kernel_initializer=xavier_init
        )

        G_sample = self.dense_layer(
                'g2',
                G_W1,
                X_dim,
                activation=tf.nn.sigmoid,
                kernel_initializer=xavier_init
        )

        self.theta_G = ['g1_w', 'g1_b', 'g2_w', 'g2_b']

        D_W1 = tf.Variable(xavier_init([X_dim, h_dim]))
        D_b1 = tf.Variable(tf.zeros(shape=[h_dim]))

        D_W2 = tf.Variable(xavier_init([h_dim, 1]))
        D_b2 = tf.Variable(tf.zeros(shape=[1]))

        self.theta_D = [D_W1, D_b1, D_W2, D_b2]

        # create discriminator
        def D(X):
            D_h1 = tf.nn.relu(tf.matmul(X, D_W1) + D_b1)
            out = tf.matmul(D_h1, D_W2) + D_b2
            return out

        D_real = D(X)
        D_fake = D(G_sample)

        eps = tf.random_uniform([mb_size, 1], minval=0., maxval=1.)
        X_inter = eps*X + (1. - eps)*G_sample
        grad = tf.gradients(D(X_inter), [X_inter])[0]
        grad_norm = tf.sqrt(tf.reduce_sum((grad)**2, axis=1))
        grad_pen = lam * tf.reduce_mean((grad_norm - 1)**2)

        self.D_loss = tf.reduce_mean(D_fake) - tf.reduce_mean(D_real) + grad_pen
        self.G_loss = -tf.reduce_mean(D_fake)

    def sample_z(m, n):
        return np.random.uniform(-1., 1., size=[m, n])

mnist = input_data.read_data_sets('../../MNIST_data', one_hot=True)

def plot(samples):
    fig = plt.figure(figsize=(4, 4))
    gs = gridspec.GridSpec(4, 4)
    gs.update(wspace=0.05, hspace=0.05)

    for i, sample in enumerate(samples):
        ax = plt.subplot(gs[i])
        plt.axis('off')
        ax.set_xticklabels([])
        ax.set_yticklabels([])
        ax.set_aspect('equal')
        plt.imshow(sample.reshape(28, 28), cmap='Greys_r')

    return fig

X = tf.placeholder(tf.float32, shape=[None, X_dim])

z = tf.placeholder(tf.float32, shape=[None, z_dim])

G_sample = G(z)
D_real = D(X)
D_fake = D(G_sample)

eps = tf.random_uniform([mb_size, 1], minval=0., maxval=1.)
X_inter = eps*X + (1. - eps)*G_sample
grad = tf.gradients(D(X_inter), [X_inter])[0]
grad_norm = tf.sqrt(tf.reduce_sum((grad)**2, axis=1))
grad_pen = lam * tf.reduce_mean((grad_norm - 1)**2)

D_loss = tf.reduce_mean(D_fake) - tf.reduce_mean(D_real) + grad_pen
G_loss = -tf.reduce_mean(D_fake)

D_solver = (tf.train.AdamOptimizer(learning_rate=lr, beta1=0.5)
            .minimize(D_loss, var_list=theta_D))
G_solver = (tf.train.AdamOptimizer(learning_rate=lr, beta1=0.5)
            .minimize(G_loss, var_list=theta_G))

sess = tf.Session()
sess.run(tf.global_variables_initializer())

if not os.path.exists('out/'):
    os.makedirs('out/')

i = 0

for it in range(1000000):
    for _ in range(n_disc):
        X_mb, _ = mnist.train.next_batch(mb_size)

        _, D_loss_curr = sess.run(
            [D_solver, D_loss],
            feed_dict={X: X_mb, z: sample_z(mb_size, z_dim)}
        )

    _, G_loss_curr = sess.run(
        [G_solver, G_loss],
        feed_dict={z: sample_z(mb_size, z_dim)}
    )

    if it % 1000 == 0:
        print('Iter: {}; D loss: {:.4}; G_loss: {:.4}'
              .format(it, D_loss_curr, G_loss_curr))

        if it % 1000 == 0:
            samples = sess.run(G_sample, feed_dict={z: sample_z(16, z_dim)})

            fig = plot(samples)
            plt.savefig('out/{}.png'
                        .format(str(i).zfill(3)), bbox_inches='tight')
            i += 1
            plt.close(fig)


text:
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Created on Tue Nov 22 16:15:28 2016

@author: login
"""
import pandas as pd
import numpy as np
import sys, os
from sklearn.decomposition import PCA, TruncatedSVD, LatentDirichletAllocation
from sklearn.preprocessing import StandardScaler

def removeZeroCols(df):
    return (df.T[(df != 0).any()]).T

def parseBiosample(df):
    """
    This function accepts a df with samples in rows and OTUs in columns
    and parses the biosample key
    """
    biosamples = list(df.index)
    dates_, primers_, kits_, replicates_, depths_ = [], [], [], [], []
    for bs in biosamples:
        if bs[:2] == "SB":
            clipped = bs[2:]
        else:
            sys.exit("Non-SB start to biosample")

        if 'TAWMD' in clipped:
            date, rest = clipped.split("TAWMD")
        elif "TAWWD" in clipped:
            date, rest = clipped.split("TAWWD")
        else:
            sys.exit("Bridge Sequence not Detected")

        if "VV4T" in rest:
            primer = "VV4"
            depth, rest2 = rest.split("VV4T")
        elif "VV4V5T" in rest:
            primer = "V4V5"
            depth, rest2 = rest.split("VV4V5T")
        else:
            sys.exit("Primer Sequence not Detected")

        if rest2[0] == "M":
            kit = "MolBio"
        elif rest2[0] == "Q":
            kit = "Qiagen"
        elif rest2[:4] == "filt" and rest2[4] == "M":
            kit = "MolBio"
        elif rest2[:2] == "NA":
            kit = "NA"
        else:
            print clipped
            print rest2[0]
            sys.exit("kit type not detected")

        if rest2[-2] == "R":
            replicate = rest2[-1]
        else:
            sys.exit("replicate signifier not detected")

        if depth == '015':
            depth = '01.5'

        dates_.append(date)
        primers_.append(primer)
        kits_.append(kit)
        replicates_.append(replicate)
        depths_.append(depth)

    df['date'] = dates_
    df['primers'] = primers_
    df['kit'] = kits_
    df['replicates'] = replicates_
    df['depth'] = depths_
    return df

def add_quadrants(dF):
    depth_list = list(dF.depth)
    depth_ns = np.array([float(n) for n in depth_list])
    quad_ns = np.array(["  "]*len(depth_ns))
    quad_ns[depth_ns < 5] = "Q1"
    quad_ns[(depth_ns > 4) & (depth_ns < 11)]= "Q2"
    quad_ns[(depth_ns > 10) & (depth_ns < 17)]= "Q3"
    quad_ns[depth_ns > 16]= "Q4"
    dF["Quadrants"] = quad_ns
    return dF.copy()

def numericEncodings(df, metadata_cols, verbose=True):
    print "Changing {} metadata columns".format(len(metadata_cols))
    unq_metadata = {i:{} for i in metadata_cols}
    for md, unqs in unq_metadata.items():
        print "\n", md
        for num, unq in enumerate(np.unique(df[md])):
            num+=1
            unqs[num] = unq
            if verbose == True:
                print "Encoding", unq, "as", num
            bool_ = df[md] == unq
            df.ix[bool_, md] = num

    for i in metadata_cols:
        df[[i]] = df[[i]].apply(pd.to_numeric)

    df2 = df.copy()
    return df2, unq_metadata

from itertools import groupby

def listRepGroups(df):
    # make a list of the index
    dfindex = list(df.index)
    # create a list for each grouping and a list of already matched samples
    rep_groups, consumed = [], []
    # Start with one index
    for first in dfindex:
        this_group = []
        # Check the entire list for members that match
        if first not in consumed:
            for second in dfindex:
                # If a sample wasn't already consumed, and isn't the exact same sample
                if second not in consumed and first != second:
                    # check for a match
                    if first.split("VV")[0] == second.split("VV")[0]:
                        # if detected, add to the already consumed list
                        consumed.append(second)
                        this_group.append(second)
        if len(this_group) != 0:
            this_group.append(first)
            this_group.sort()
            rep_groups.append(this_group)
    rep_groups.sort()
    unq_rep_groups = list(rg for rg,_ in groupby(rep_groups))
    print len(unq_rep_groups), "groups of replicates detected"
    return unq_rep_groups

def JensenShannonDiv_Sqrt(df_otu):
    ps_df = df_otu.copy().replace(0.0, 1.0)
    ps_n_df = ps_df.divide(ps_df.sum(axis=1), axis=0)
    shape_sq = len(ps_n_df.index)
    dist_dat = np.zeros((shape_sq, shape_sq))

    for r_idx, r in enumerate(ps_n_df.index):
        for c_idx, c in enumerate(ps_n_df.index):
            x_ = ps_n_df.ix[r, :]
            y_ = ps_n_df.ix[c, :]
            m_ = (x_+y_) / 2
            jsd_sqrt = (0.5*(x_*np.log2(x_/m_) + y_*np.log2(y_/m_)).sum())**0.5
            dist_dat[r_idx, c_idx] = jsd_sqrt

    dist_mat = pd.DataFrame(index=ps_n_df.index, columns=ps_n_df.index,
                            data=dist_dat)
    return dist_mat

def ReplicateReport(df, df_otus, rep_groups, verbose=True, metric="JSD"):
    print "REPLICATE REPORT"

    in_rep_distances, all_dists, worst_reps = [], [], []
    broken_groups = 0

    for idx, group in enumerate(rep_groups):
        print "Group {}".format(idx)

        this_grps = df_otus.ix[group, :]
        dist_mat = JensenShannonDiv_Sqrt(this_grps)
        for a_d in np.unique(dist_mat.values):
            if a_d != 0:
                in_rep_distances.append(a_d)

        if verbose == True:
            print dist_mat.max()

        most_distant = dist_mat.max().max()

        print "Most Distant: {}".format(most_distant)
        if most_distant > 0.3:
            broken_groups+=1
            while most_distant > 0.3:
                # find one of the pair that are most divergent
                bad_reps_bool = (dist_mat.max() == dist_mat.max().max())
                bad_means = dist_mat[bad_reps_bool].mean(axis=1)
                worst_rep = bad_means.argmax()
                worst_reps.append(worst_rep)
                print "\tdropping {}".format(worst_rep)
                group.remove(worst_rep)
                this_grps = df_otus.ix[group, :]
                dist_mat = JensenShannonDiv_Sqrt(this_grps)
                most_distant = dist_mat.max().max()
                print "\tmost distant now: {}".format(most_distant)

    in_rep_distances = np.array(in_rep_distances)
    if len(in_rep_distances) > (len(df_otus.index)-2):
        random_samples = len(df_otus.index)-2
    else:
        random_samples = len(in_rep_distances)

    rand_samp_idx = list(np.random.choice(df_otus.index, random_samples,
                                          replace=False))

    print "{} random vectors selected".format(len(rand_samp_idx))
    df_otus_rand = df_otus.ix[rand_samp_idx, :]
    rand_dist_mat = JensenShannonDiv_Sqrt(df_otus_rand)
    for all_d in np.unique(rand_dist_mat.values):
        if all_d != 0:
            all_dists.append(all_d)

    all_dists = np.array(all_dists)
    for lab, dist_arr in zip(['In Rep','Rand Dists'], [in_rep_distances, all_dists]):
        print lab
        print "Mean: {}".format(dist_arr.mean())
        print "Variance: {}".format(np.var(dist_arr))
        print "CV: {}".format(np.std(dist_arr)/dist_arr.mean())
        print "n: {}".format(len(dist_arr))

    return worst_reps, broken_groups

"""
        this_sum = this_grps
        best_rep = list(this_sum[ this_sum == this_sum.max()].index)
        bad_reps = list(this_sum[this_sum != this_sum.max()].index)
        farthest_rep = dist_mat.max().max()

        if farthest_rep > 0.25:
            while farthest_rep > 0.25:
                distances = euclidean_distances(df.ix[best_rep, :],
                                                df.ix[bad_reps, :])
                worst_rep = bad_reps[np.argmax(distances)]
                bad_reps.remove(worst_rep)
                worst_reps.append(worst_rep)
                rem_reps = bad_reps + best_rep
                rem_grps = df.ix[rem_reps, 'enspie']
                rem_sum = rem_grps
                this_cv = (rem_sum.std() / rem_sum.mean())
        else:
            rem_reps = group

        this_grps = df_otus.ix[rem_reps, :]
        this_bool = this_grps != 0
        this_sum = this_bool.T.sum()

        if verbose == True:
            print group[0][:-1]
            print "Mean Count Total:", this_sum.mean()
            print "CV:", (this_sum.std() / this_sum.mean())
            reps, _ = this_grps.shape
            n_ = range(1,reps+1)
            totalTaxa = (this_bool.sum() > 0).sum()
            print "Total Taxa Discovered in {} reps: {}".format(reps ,totalTaxa)
            for n in n_:
                overlap = ((this_bool.sum() == n).sum()/float(totalTaxa))*100.0
                print "{0:.3f}% among {1} replicates".format(overlap, n)

    return (list(set(worst_reps)), cvs)
"""

def dropBadReps(less_diverse_reps, rep_groups):
    new_rep_groups = []
    for g in rep_groups:
        for l in less_diverse_reps:
            if l in g:
                g.remove(l)
            else:
                pass
        if len(g) > 1:
            new_rep_groups.append(g)
        else:
            pass

    return new_rep_groups

import matplotlib.pyplot as plt
import seaborn as sns

def plotHeatmap(df, fignum):
    plt.figure(fignum, figsize=(12,9))
    ax = sns.heatmap(df)
    for item in ax.get_yticklabels():
        item.set_rotation(0)

    for item in ax.get_xticklabels():
        item.set_rotation(90)
    #plt.savefig('seabornPandas.png', dpi=100)
    plt.show()

import ecopy as ep

def beta_wrapper(df, var_key):
    print "\n", var_key
    brayDist = ep.distance(df, method='bray')
    groups = list(df[var_key])
    rand_groups = list(np.random.choice(np.unique(groups),
                                    size=np.array(groups).shape))

    ep.beta_dispersion(brayDist, groups, test='anova',
                   center='median', scores=False)
    ep.beta_dispersion(brayDist, rand_groups, test='anova',
                   center='median', scores=False)
    return brayDist

def scalePCAcorrelate(df_numerical, df_w_mdata, metadata_cols, transformed):
    if transformed:
        X_std2 = df_numerical.values.T
    else:
        X_std2 = StandardScaler().fit_transform(df_numerical.values.T)

    rows_n, cols_n = X_std2.shape
    print "\nPerforming PCA"
    print "Matrix has {} samples and {} features".format(rows_n, cols_n)
    pca2 = TruncatedSVD(n_components=100, n_iter=20, random_state=42)
    pca2.fit(X_std2)
    print "Top two components explain {} and {} of variance.".format(pca2.explained_variance_ratio_[0],
                                                                     pca2.explained_variance_ratio_[1])
    for mdata in metadata_cols:
        md_arr = np.array(df_w_mdata[mdata])

        corrs = [np.corrcoef(pca2.components_[i, :], md_arr)[0, 1] for i in range(100)]
        print "Metadata Variable:", mdata
        print "Max Correlation:", np.array(corrs).max()
        pca_comp_no = np.argmax(np.array(corrs))
        print "PCA component No:", pca_comp_no
        print "Explained Variance:", pca2.explained_variance_ratio_[pca_comp_no]
        print ""
    return pca2

def readChemData(chem_path, units, ftype, plotbool=False):
    print os.path.basename(chem_path)
    print units

    if ftype == 'depth_profile':
        site_chem_spec_df = pd.read_csv(chem_path, index_col=0,
                                        parse_dates=True,
                                        infer_datetime_format=True)
        new_idx = []
        for i in site_chem_spec_df.index:
            new_idx.append(pd.Period(i, 'M'))
        site_chem_spec_df.index = new_idx

    elif ftype == 'surface_measure':
        chem_spec_csv = pd.read_csv(chem_path, sep=",", index_col=0)
        print "Null Values: {}".format(chem_spec_csv.isnull().sum().sum())
        print "Database Shape: {}".format(chem_spec_csv.shape)
        new_cols = [pd.Period(i) for i in chem_spec_csv.columns]
        chem_spec_csv.columns = new_cols
        site_chem_spec_df = chem_spec_csv.T.interpolate()
    else:
        sys.exit("invalid ftype")

    if plotbool == True:
        site_chem_spec_df.plot()
    return site_chem_spec_df

def plotCommonTaxa(taxa_series):

    taxa_list = list(taxa_series.values)
    taxa_decomposition = []
    for tax_str in taxa_list:
        this_tl = tax_str.split(";")
        clean_list = [i for i in this_tl if i[-2:] != "__" ]
        taxa_decomposition += clean_list
    unq_taxa = np.unique(taxa_decomposition, return_counts=True)
    dfindex, dfdata = unq_taxa[0], unq_taxa[1]
    taxa_df = pd.DataFrame(data=dfdata, index=dfindex, columns=['count'])
    taxa_df.sort_values('count', ascending=False, inplace=True)
    rtaxa_df = taxa_df.divide(taxa_df.sum(), axis=1)
    total_called = []
    t_levels_s = ['k', 'p', 'c', 'o', 'f', 'g', 's']
    t_levels = ['kingdom', 'pylum', 'class', 'order', 'family', 'genus', 'species']
    rtaxa_df = taxa_df.divide(taxa_df.sum(), axis=1)
    for tL in t_levels_s:
        posHits = [i for i in rtaxa_df.index if i[0] == tL]
        subdf = rtaxa_df.ix[posHits, :]
        print tL, subdf.sum()
        total_called.append(subdf.sum())
    t_level_rel = np.array(total_called)
    width = 0.35
    ind = np.arange(len(t_levels))
    print "total pct%", t_level_rel.sum()
    fig, ax = plt.subplots(1, 1)
    ax.bar(ind + width, t_level_rel,width)
    ax.set_xticks(ind + width)
    ax.set_xticklabels(t_levels)

def inferTaxaLevel(taxa_series):
    addSemicolon = lambda x: x+";"
    taxa_edit = taxa_series.copy().apply(addSemicolon)
    taxa_dict = taxa_edit.to_dict()
    taxa_frame = taxa_edit.copy().to_frame()
    taxa_depth = np.zeros(taxa_series.shape)
    taxa_frame["Taxa Depth"] = taxa_depth
    for seq, ts in taxa_dict.items():
        taxa_frame.ix[seq, "Taxa Depth"] = 7 - ts.count("_;")
    return taxa_frame

def analyze_alpha_diversity(decoder, derepped_otus_m, valPairs):
    ad_cats =  []
    ad_cols = {"Mean":[], "Median":[], "Std":[], "N":[]}

    for var in decoder.keys():
        codes = decoder[var].keys()
        for code in codes:
            if var == 'date':
                ad_cats.append(str(decoder[var][code]).split("T")[0]+" ("+var+")")
            elif var == 'replicates':
                pass
            else:
                ad_cats.append(str(decoder[var][code])+" ("+var+")")

            if var == 'replicates':
                pass
            else:
                sub_bool = derepped_otus_m[var] == code
                subdf = derepped_otus_m[sub_bool]
                ad_cols["Median"].append(np.median(subdf.enspie))
                ad_cols["Std"].append(subdf.enspie.std())
                ad_cols["N"].append(subdf.shape[0])
                ad_cols["Mean"].append(subdf.enspie.mean())

    for idx, vp in enumerate(valPairs):
        kitT, primT = vp[0], vp[1]
        bool1 = derepped_otus_m.primers == primT
        bool2 = derepped_otus_m.kit == kitT
        subdf2 = derepped_otus_m[bool1 & bool2]
        if idx == 2:
            primer_outgroup = list(subdf2.index)

        ad_cats.append(str(decoder['primers'][primT])+" & "+str(decoder['kit'][kitT]))
        ad_cols["Median"].append(np.median(subdf2.enspie))
        ad_cols["Std"].append(subdf2.enspie.std())
        ad_cols["N"].append(subdf2.shape[0])
        ad_cols["Mean"].append(subdf2.enspie.mean())

    alpha_df = pd.DataFrame(data=ad_cols, index=ad_cats)
    return alpha_df, primer_outgroup

def alpha_diversity(dereplicated_otus, derepped_otus_m, metrics):
    row_sum = dereplicated_otus.copy().sum(axis=1)
    row_rel = dereplicated_otus.copy().divide(row_sum, axis=0).astype('float64')
    if 'enspie' in metrics:
        enspie_sq = row_rel.apply(np.square)
        enspie_dom = enspie_sq.sum(axis=1)
        enspie_ = enspie_dom**-1
        derepped_otus_m['enspie'] = enspie_
    if 'shannon' in metrics:
        entrop = lambda n: n*np.log(n)
        shannon_ = row_rel.replace({ 0 : np.nan }).applymap(entrop).T.sum()*-1.0
        derepped_otus_m['shannon'] = shannon_.apply(np.exp)
    if 'chao1' in metrics:
        total_s =  (dereplicated_otus > 0).T.sum()
        singletons = (dereplicated_otus == 1).T.sum()
        doubletons = (dereplicated_otus == 2).T.sum()
        numerator = singletons.multiply(singletons-1)
        denominator = 2*(doubletons+1)
        chao1_ = total_s + numerator.divide(denominator, axis=0)
        derepped_otus_m['chao1'] = chao1_

    return dereplicated_otus, derepped_otus_m

def plotCountTotalsByMetadata(df_m, decoder, mList, mVar, fignum):
    ## Get Normalized Count Totals
    # drop everything but the key grouping variable and sum
    mList.remove(mVar)
    counts_Depth = df_m.drop(mList, axis=1)
    depthGroup = counts_Depth.groupby([mVar]).sum()
    # find the number of samples per grouping
    (_, n_per_depth) = np.unique(df_m[mVar].values, return_counts=True)
    # average the total counts per group by the number of samples
    mean_counts_per_group = depthGroup.T.sum().divide(n_per_depth)
    ## Get Standard Deviation of Count Totals
    # Drop depths & sum each sample before grouping
    just_counts = counts_Depth.drop([mVar], axis=1)
    depthSum = just_counts.T.sum().to_frame()
    # Convert Series into DataFrame, add col names, and modify dtype
    depthSum[mVar] = df_m[mVar].values
    depthSum.columns = ['counts', mVar]
    depthSum = depthSum.applymap(pd.to_numeric)
    # group each sum by depth and flatten by std
    depthStd = depthSum.groupby([mVar]).std()
    # convert labels for display
    if mVar == 'date':
        decoded_labs = [str(decoder[mVar][i]).split("T")[0] for i in list(np.unique(df_m[mVar].values))]
    else:
        decoded_labs = [str(decoder[mVar][i]) for i in list(np.unique(df_m[mVar].values))]
    # Setup Plot
    width = 0.35; ind = np.arange(len(n_per_depth));
    plt.figure(fignum, figsize=(14, 8))
    ax = plt.gca()
    ax.bar(ind + width, mean_counts_per_group, width,
            yerr=depthStd.values.flatten())
    ax.set_xticks(ind + width)
    ax.set_xticklabels(decoded_labs)
    ax.set_xticklabels(ax.xaxis.get_majorticklabels(), rotation=45)
    ax.set_xlim(0.0, float(len(list(np.unique(df_m[mVar].values)))))
    ax.set_xlabel(mVar.capitalize()+" (m)")
    ax.set_ylabel("Average Total OTUs (n)")
    # Add back metadata variable
    mList.append(mVar)

def replicateAlphaDiv(df, metric, rep_groups):
    enspie_cv = []
    enspie_1 = []
    enspie_2 = []
    for g in rep_groups:
        this_grp = df.ix[g, metric]
        enspie_cv.append(this_grp.std() / this_grp.mean())
        justTwo = list(np.random.choice(g, size=2, replace=False))
        enspie_1.append(df.ix[justTwo[0], metric])
        enspie_2.append(df.ix[justTwo[1], metric])

    enspie_1 = np.array(enspie_1)
    enspie_2 = np.array(enspie_2)

    return (enspie_cv, enspie_1, enspie_2)

def centeredLogRatio(otu_table, otu_table_m=None):
    from scipy.stats.mstats import gmean
    noZeros = otu_table.copy().replace(0, np.nan)
    geomeans = np.repeat(np.nan, repeats = noZeros.shape[0])
    for i in range(0, noZeros.shape[0]):
        geomeans[i] = gmean(noZeros.ix[i, :].dropna())
    clr_table = np.log(noZeros.divide(geomeans, axis=0))
    clr_table.replace(np.nan, 0, inplace=True)
    if otu_table_m is not None:
        clr_table_m = otu_table_m.copy()
        clr_table_m.ix[:, otu_table.columns] = clr_table
        return clr_table, clr_table_m
    else:
        return clr_table

from sklearn.model_selection import train_test_split

def lda_tuner(ingroup_otu, best_models):

    best_score = -1*np.inf
    dtp_series = [0.0001, 0.001, 0.01, 0.1, 0.2]
    twp_series = [0.0001, 0.001, 0.01, 0.1, 0.2]
    topic_series = [3]
    X = ingroup_otu.values
    eval_counter = 0

    for topics in topic_series:
        for dtp in dtp_series:
            for twp in twp_series:
                eval_counter +=1
                X_train, X_test = train_test_split(X, test_size=0.5)
                lda = LatentDirichletAllocation(n_topics=topics,
                                                doc_topic_prior=dtp,
                                                topic_word_prior=twp,
                                                learning_method='batch',
                                                random_state=42,
                                                max_iter=20)
                lda.fit(X_train)
                this_score = lda.score(X_test)
                this_perplexity = lda.perplexity(X_test)
                if this_score > best_score:
                    best_score = this_score
                    print "New Max Likelihood: {}".format(best_score)

                print "#{}: n:{}, dtp:{}, twp:{}, score:{}, perp:{}".format(eval_counter,
                                                                 topics, dtp, twp,
                                                                 this_score, this_perplexity)

                best_models.append({'n': topics, 'dtp': dtp, 'twp': twp,
                                    'score': this_score, 'perp': this_perplexity})
                if (dtp == dtp_series[-1]) and (twp == twp_series[-1]):
                    eval_counter +=1
                    X_train, X_test = train_test_split(X, test_size=0.5)
                    lda = LatentDirichletAllocation(n_topics=topics,
                                                    doc_topic_prior=1./topics,
                                                    topic_word_prior=1./topics,
                                                    learning_method='batch',
                                                    random_state=42,
                                                    max_iter=20)
                    lda.fit(X_train)
                    this_score = lda.score(X_test)
                    this_perplexity = lda.perplexity(X_test)
                    if this_score > best_score:
                        best_score = this_score
                        print "New Max Likelihood: {}".format(best_score)

                    print "#{}: n:{}, dtp:{}, twp:{}, score:{} perp: {}".format(eval_counter,
                                                                                topics,
                                                                                (1./topics),
                                                                                (1./topics),
                                                                                this_score,
                                                                                this_perplexity)

                    best_models.append({'n': topics, 'dtp': (1./topics),
                                        'twp': (1./topics), 'score': this_score,
                                        'perp': this_perplexity})
    return best_models

def collapseBdiversity(dist_mat, raw_data_m, metaData_var):
    metaOptions = np.unique(raw_data_m.ix[:, metaData_var])
    n_ = len(metaOptions)
    metaDistance = np.full((n_, n_), np.nan)
    metaDeviation = np.full((n_, n_), np.nan)
    for r_i, r in enumerate(metaOptions):
        for c_i, c in enumerate(metaOptions):
            dist_copy = dist_mat.copy()
            dist_copy[metaData_var] = raw_data_m.ix[:, metaData_var]
            dist_filt_1 = dist_copy[dist_copy[metaData_var] == r]
            dist_filt_1.drop([metaData_var], axis=1, inplace=True)
            dist_filt_t = dist_filt_1.T
            dist_filt_t[metaData_var] = raw_data_m.ix[:, metaData_var]
            dist_filt_2 = dist_filt_t[dist_filt_t[metaData_var] == c]
            dist_filt = dist_filt_2.drop([metaData_var], axis=1)
            dist_filt_flat = dist_filt.values.flatten()
            dist_filt_nz = dist_filt_flat[dist_filt_flat != 0]
            mD = np.median(dist_filt_nz)
            mDev = dist_filt_nz.std()
            print "{} versus {} metadistance".format(r, c)
            print "\t {} ({})".format(mD, mDev)
            metaDistance[r_i,c_i] = mD
            metaDeviation[r_i,c_i] = mDev
    return metaDistance, metaDeviation

"""
http://qiime.org/scripts/make_otu_network.html
http://qiime.org/scripts/differential_abundance.html

# alternative to random forest classifier

from sklearn.gaussian_process import GaussianProcessClassifier
from sklearn.gaussian_process.kernels import RBF
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler

clf = GaussianProcessClassifier(1.0 * RBF(1.0), warm_start=True,
                                n_jobs=-1)
X = StandardScaler().fit_transform(X)
X_train, X_test, y_train, y_test = \
        train_test_split(X, y, test_size=.4, random_state=42)
clf.fit(X_train, y_train)
score = clf.score(X_test, y_test)

"""

import subprocess as sp

def varStabTransform(path, df_otus):
    """
    Uses edgeR's variance stabilizing transformation to transform sequence counts
    into OTU abundances. Involves adding and subtracting psuedocounts.
    """
    wrapper_file = os.path.join(os.getcwd(), "edgeRwrapper.R")
    if not os.path.exists(wrapper_file):
        sys.exit("Accessory script missing")

    # direct export path
    to_transform = path
    shared_otus = df_otus.copy()
    arg_1 = os.path.dirname(to_transform)
    arg_2 = to_transform.split("/")[-1]
    # export data to disk
    shared_otus.to_csv(to_transform, sep=",")
    # run R script
    base_cmd = "Rscript edgeRwrapper.R"
    cmd = " ".join([base_cmd, arg_1, arg_2])
    p = sp.Popen(cmd, cwd=os.getcwd(), shell=True, stderr=sp.PIPE, stdout=sp.PIPE)
    stdout, stderr = p.communicate()
    if "Execution halted" in stderr:
        sys.exit("R wrapper failed")

    to_read_back = os.path.join(arg_1, arg_2.split(".")[0]+"_vst.csv")
    shared_vst = pd.read_csv(to_read_back, index_col = 0)
    for i, j in zip(shared_vst.index, shared_otus.index):
        assert i == j
    for i, j in zip(shared_vst.columns, shared_otus.columns):
        assert i == j

    for temps in [to_read_back, to_transform]:
        os.remove(temps)
    return shared_vst

reason: import numpy,from scipy


text:
# -*- coding: utf-8 -*-
"""
Created on Mon Mar  7 14:38:46 2011
Plot pca training error as a function of percentage of samples used
@author: -
"""

# Computes the gaussian gradients on a boxm_alpha_scene

import os;
import optparse;
import time;
import sys;
import plot_pca_functions;
import numpy as np
import matplotlib.pyplot as plt
import math

if __name__=="__main__":

  #Parse inputs
#  parser = optparse.OptionParser(description='Compute PCA basis');
#
#  parser.add_option('--pca_dir', action="store", dest="pca_dir");
#  options, args = parser.parse_args();
#
#  pca_dir = options.pca_dir;

  pca_dir = '/Users/isa/Experiments/PCA/CapitolBOXM_6_4_4';
  print (pca_dir)

  if not os.path.isdir( pca_dir + '/'):
    sys.exit(-1);

  fig = plt.figure(10);

  for frac in range(1,6):

    this_pca_dir = pca_dir + '/' +str(int(frac*10));
    if not os.path.isdir( this_pca_dir + '/'):
        sys.exit(-1);

    error_file = this_pca_dir + "/normalized_training_error.txt";
    error= plot_pca_functions.read_vector(error_file);

    plt.title('Training Error ');
    x = np.arange(0, len(error), 1);
    y = plt.plot(x, error, label=(str(int(frac*10)) +'% of total samples'));
    plt.hold(True);

  plt.xlabel('Number of components used for reconstruction');
  plt.ylabel('Average error per feature vector');
  a = plt.gca()
  a.set_xlim([1,125])
  plt.legend();
  plt.show();

max_stars_repo_name: mirestrepo/voxels-at-lems

max_issues_repo_name: mirestrepo/voxels-at-lems

max_forks_repo_name: mirestrepo/voxels-at-lems


text:
#!/usr/bin/python3

import numpy as np
import matplotlib.pyplot as plt
import os
import shutil

from tkinter import Tk, messagebox
def main(date):
    """  """
    if args.amount:
        for scans in range(args.amount):
            date = input('scan number or filename: ')
            if len(date) <2:
                date= args.date[0:9] + date +'.mass'
            else:
                date= date + '.mass'
            with open(date) as f:
                data = f.read()

            data = data.split('\n')

            x = -np.ones(len(data))
            y = -np.ones(len(data))

            for index, a in enumerate(data):

                if '#' in a or len(a) < 1:
                    if '# m03_ao09_width' in a:
                        a = a.split('\t\t#')
                        b0 = "%.0f" % (int(a[3]) / 1000)
                else:
                    a = a.split('\t')
                    x[index] = float(a[0])
                    y[index] = float(a[1])

            name = f.name[:8].replace('_', '-')

            plt.semilogy(x, y, label= input('legenda input: '))
            plt.legend()

        compound_name = input("What is your compounds name? ")
        compound_structure = input('The structure? ')
        Temp = input('Temperature? ')
        ion = input('Electron energy? ')

        plt.suptitle('Mass spectrum ion source from {} ({}) \n {}, T={}K, B0 opening {} ms, Ee={}V'.format(compound_name, compound_structure, name, Temp, b0, ion))

        plt.xlabel('mass [u]')
        plt.ylabel('ion counts /{} ms'.format(b0))
        plt.grid(True)
        plt.ylim(1)

        plt.show()
    else:
        date= date + '.mass'
        with open(date) as f:
            data = f.read()

        data = data.split('\n')

        x = -np.ones(len(data))
        y = -np.ones(len(data))

        for index, a in enumerate(data):

            if '#' in a or len(a) < 1:
                if '# m03_ao09_width' in a:
                    a = a.split('\t\t#')
                    b0 = "%.0f" % (int(a[3])/1000)
            else:
                a = a.split('\t')
                x[index] = float(a[0])
                y[index] = float(a[1])

        name = f.name[:8].replace('_', '-')

        plt.semilogy(x, y)
        if args.amount:
            plt.title('{}, T={}K, B0 opening {} ms, Ee={}V'.format(name,Temp, b0, ion))
            plt.suptitle('Mass spectrum ion source from {} ({})'.format(compound_name,compound_structure))
        plt.xlabel('mass [u]')
        plt.ylabel('ion counts /{} ms'.format(b0))
        plt.grid(True)
        plt.ylim(1)

        plt.show()

def massSpec(fname, mname, temp, bwidth, ie, xmin, xmax, location,\
            fig_width, fig_height, filelist, avgname, combine, save_fig):

    os.chdir(location)
    my_path = os.getcwd()

    def saveinfo(name):
        if os.path.isfile(my_path+"/MassSpec_DATA/{}.png".format(name)):
            root = Tk()
            root.withdraw()
            messagebox.showinfo("Information", "File '{}.png' SAVED \nin MassSpec_DATA directory"\
                                .format(name))
            root.destroy()

    def filenotfound():
        root = Tk()
        root.withdraw()
        messagebox.showerror("Error", "FILE '{}.mass' NOT FOUND".format(fname))
        root.destroy()

    try:
        if fname.find(".mass")>0:
            fname = fname.split(".")[0]

        plt.rcParams['figure.figsize'] = [fig_width,fig_height]
        plt.rcParams['figure.dpi'] = 80
        plt.rcParams['savefig.dpi'] = 100

        if not combine:

            filename = fname + ".mass"

            if not os.path.isdir("MassSpec_DATA"):
                os.mkdir("MassSpec_DATA")
                shutil.copyfile(my_path + "/{}".format(filename), my_path + "/MassSpec_DATA/{}".format(filename))
            else:
                shutil.copyfile(my_path + "/{}".format(filename), my_path + "/MassSpec_DATA/{}".format(filename))

            with open(filename) as f:
                data = f.read()

            data = data.split('\n')

            x = -np.ones(len(data))
            y = -np.ones(len(data))

            for index, a in enumerate(data):
                if '#' in a or len(a) < 1:
                    if '# m03_ao09_width' in a:
                        a = a.split('\t\t#')
                        b0 = "%.0f" % (int(a[3])/1000)
                else:
                    a = a.split('\t')
                    x[index] = float(a[0])
                    y[index] = float(a[1])
            plt.semilogy(x, y)
            plt.xlabel('Mass [u]')
            plt.ylabel('Ion counts /{} ms'.format(b0))
            plt.grid(True)
            plt.xlim([xmin,xmax])
            plt.ylim(1)
            plt.title("Filename: {}, for {}, at temp: {}K, B0: {}ms and IE(eV): {}"\
                        .format(fname, mname, temp, bwidth, ie))
            if save_fig:
                plt.savefig(my_path + "/MassSpec_DATA/{}.png".format(fname))
                plt.show()
                saveinfo(fname)
            else:
                plt.show()

        if combine:
            filelist = filelist.split(",")

            for file in filelist:
                file = file.strip()
                if file.find("mass")>=0:
                    file = file.split(".")[0]

                if len(file) <2:
                    file= fname[0:9] + file +'.mass'
                else:
                    file = file + '.mass'
                with open(file) as f:
                    data = f.read()

                data = data.split('\n')

                x = -np.ones(len(data))
                y = -np.ones(len(data))

                for index, a in enumerate(data):

                    if '#' in a or len(a) < 1:
                        if '# m03_ao09_width' in a:
                            a = a.split('\t\t#')
                            b0 = "%.0f" % (int(a[3]) / 1000)
                    else:
                        a = a.split('\t')
                        x[index] = float(a[0])
                        y[index] = float(a[1])

                plt.semilogy(x, y, label=file)
                plt.legend()

            plt.xlabel('mass [u]')
            plt.ylabel('ion counts /{} ms'.format(b0))
            plt.grid(True)
            plt.ylim(1)
            plt.xlim([xmin,xmax])
            plt.title("Filename: {}, for {}, at temp: {}K, B0: {}ms and IE(eV): {}".format(fname, mname, temp, bwidth, ie))

            if save_fig:
                plt.savefig(my_path + "/MassSpec_DATA/{}.png".format(avgname))
                plt.show()
                saveinfo(avgname)
            else:
                plt.show()

    except:
        filenotfound()

max_stars_repo_name: aravindhnivas/FELion-Spectrum-Analyser

max_issues_repo_name: aravindhnivas/FELion-Spectrum-Analyser

max_forks_repo_name: aravindhnivas/FELion-Spectrum-Analyser