text:
import glob
import numpy
from music21 import chord, converter, instrument, note

class Preprocessing():
    def __init__(self):
        self.notes = self.get_notes()
        self.len = len(set(self.notes))

        self.input_seq, self.out_seq  = self.prepare_sequences(self.notes, self.len)

    def prepare_sequences(notes, n_vocab):
        '''
        Exctracted Notes To A Numpy
        Array
        notes -> from get_notes
        vocab -> len of notes returned
        '''
        sequence_length = 100

        # get all pitch names
        pitchnames = sorted(set(item for item in notes))

        # create a dictionary to map pitches to integers
        note_to_int = dict((note, number) for number, note in enumerate(pitchnames))

        network_input = []
        network_output = []

        # create input sequences and the corresponding outputs
        for i in range(0, len(notes) - sequence_length, 1):
            sequence_in = notes[i:i + sequence_length]
            sequence_out = notes[i + sequence_length]
            network_input.append([note_to_int[char] for char in sequence_in])
            network_output.append(note_to_int[sequence_out])

        n_patterns = len(network_input)

        # reshape the input into a format compatible with LSTM layers
        network_input = numpy.reshape(network_input, (n_patterns, sequence_length, 1))
        # normalize input
        network_input = network_input / float(n_vocab)

        network_output = numpy.array(network_output)

        return (network_input, network_output)

    def get_notes():
        '''
        Notes And Chord
        From The midi

        '''
        notes = []

        for file in glob.glob("midi_songs/*.mid"): ##change
            midi = converter.parse(file)

            print("Parsing %s" % file)

            notes_to_parse = None

            try: # file has instrument parts
                s2 = instrument.partitionByInstrument(midi)
                notes_to_parse = s2.parts[0].recurse()
            except: # file has notes in a flat structure
                notes_to_parse = midi.flat.notesAndRests

            for element in notes_to_parse:
                if isinstance(element, note.Note):
                    notes.append(str(element.pitch))
                elif isinstance(element, chord.Chord):
                    notes.append('.'.join(str(n) for n in element.normalOrder))
                elif isinstance(element, note.Rest): #ADDED
                    notes.append(element.name) #ADDED

        return notes

max_stars_repo_name: srm-mic/wolfgang

max_issues_repo_name: srm-mic/wolfgang

max_forks_repo_name: srm-mic/wolfgang


text:
import plotly.graph_objects as go
import brightway2 as bw
from pathlib import Path
from copy import deepcopy
from stats_arrays import MCRandomNumberGenerator
import pickle
from plotly.subplots import make_subplots
import numpy as np

# TODO choose these parameters
save_fig = True
plot_narrow = False
scaling_factor = 3
num_params = 20
rows = 5
model_seed = 3333
iterations = 2000
act_plotting_dict = {
    "cheese production, soft, from cow milk": "cheese production, soft",
    "operation, housing system, pig, fully-slatted floor": "operation, housing, pig",
    "liquid manure storage and processing facility construction": "liquid manure facility constr.",
    "electricity voltage transformation from high to medium voltage": "voltage transf., high to med.",
    "market for housing system, pig, fully-slatted floor, per pig place": "market for housing, pig",
    "market for operation, housing system, pig, fully-slatted floor, per pig place": "market for oper., hous.",
    "market for housing system, cattle, tied, per animal unit": "market for housing, cattle",
    "soybean meal and crude oil production": "soybean meal, crude oil prod.",
    "operation, housing system, cattle, tied": "operation, housing, cattle",
    "housing system construction, pig, fully-slatted floor": "housing constr., pig",
    "electricity voltage transformation from medium to low voltage": "volt. transf. from med. to low",
    "housing system construction, cattle, tied": "housing constr., cattle",
    "market group for transport, freight, lorry, unspecified": "market for transport, freight, lorry",
    "market for liquid manure storage and processing facility": "market for manure stor. & process.",
    "market group for transport, freight, light commercial vehicle": "market for transport, freight, light veh.",
    "market for nitric acid, without water, in 50% solution state": "market for nitric acid, w/o water, 50%",
    "market for land tenure, arable land, measured as carbon net primary productivity, perennial crop": "market for land tenure, arable",
    "market group for electricity, low voltage": "market for electr., low volt.",
    "nutrient supply from calcium nitrate": "nutr. supply from calc. nitr.",
    "market for electricity, high voltage": "market for electr., high volt.",
    "maize grain, feed production": "maize grain, feed prod.",
    "soybean, feed production": "soybean, feed prod.",
    "milk production, from cow": "milk prod., from cow",
}

COLORS_DICT = {
    "parameters_standard": "#00CC96",
    "parameters_narrow": "#FFA15A",
}

path_base = Path("/Users/akim/PycharmProjects/gsa_framework/dev/write_files/paper_gsa")
path_setac = path_base / "setac_gsa"
path_merlin = path_setac / "merlin"
path_model_dir = path_setac / "regression" / "{}_model".format(model_seed)
filepath_row_acts_names = path_model_dir / "row_acts_names.pickle"
filepath_col_acts_names = path_model_dir / "col_acts_names.pickle"
filepath_tech_params = path_model_dir / "tech_params.pickle"
with open(filepath_row_acts_names, "rb") as f:
    row_acts_names = pickle.load(f)
with open(filepath_col_acts_names, "rb") as f:
    col_acts_names = pickle.load(f)
with open(filepath_tech_params, "rb") as f:
    tech_params = pickle.load(f)

tech_params_narrow = deepcopy(tech_params)  # TODO understand this!
tech_params_narrow["scale"] = tech_params_narrow["scale"] / scaling_factor
mc = MCRandomNumberGenerator(tech_params, maximum_iterations=iterations, seed=89)
mc_narrow = MCRandomNumberGenerator(tech_params_narrow, maximum_iterations=iterations)
X = np.array([list(next(mc)) for _ in range(iterations)])
X_narrow = np.array([list(next(mc_narrow)) for _ in range(iterations)])

units = []
act_in_names_ = []
act_out_names_ = []
name_ind = 0
unit_ind = 1
for k, param in enumerate(tech_params):
    units.append(row_acts_names[k][unit_ind])
    act_in_names_.append(row_acts_names[k][name_ind])
    act_out_names_.append(col_acts_names[k][name_ind])

act_in_names = [act_plotting_dict.get(act, act) for act in act_in_names_]
act_out_names = [act_plotting_dict.get(act, act) for act in act_out_names_]

cols = num_params // rows
num_bins = 60

fig = make_subplots(
    rows=rows,
    cols=cols,
    shared_yaxes=True,
    vertical_spacing=0.25,
    horizontal_spacing=0.06,
)

k = 0
opacity_ = 0.75
for i in range(1, rows + 1):
    freq_row = np.zeros((1,))
    for j in range(1, cols + 1):

        x, x_narrow = X[:, k], X_narrow[:, k]
        bin_min, bin_max = min(np.hstack([x, x_narrow])), max(np.hstack([x, x_narrow]))
        bins_ = np.linspace(bin_min, bin_max, num_bins, endpoint=True)
        freq, bins = np.histogram(x, bins=bins_)
        freq_narrow, bins_narrow = np.histogram(x_narrow, bins=bins_)
        freq_row = np.hstack([freq_row, freq_narrow])

        fig.add_trace(
            go.Bar(
                x=bins,
                y=freq,
                marker=dict(
                    color=COLORS_DICT["parameters_standard"],
                    opacity=opacity_,
                ),
                showlegend=False,
            ),
            row=i,
            col=j,
        )
        if plot_narrow:
            fig.add_trace(
                go.Bar(
                    x=bins_narrow,
                    y=freq_narrow,
                    marker=dict(
                        color=COLORS_DICT["parameters_narrow"],
                        opacity=opacity_,
                    ),
                    showlegend=False,
                ),
                row=i,
                col=j,
            )
        fig.update_xaxes(
            title_text=units[k],
            range=[bin_min, bin_max],
            row=i,
            col=j,
        )
        k += 1
    for j in range(1, cols + 1):
        fig.update_yaxes(
            range=[min(freq_row), max(freq_row)],
            row=i,
            col=j,
        )

annotations = []
k = 0
for i in range(rows):
    for j in range(cols):
        if k == 0:
            xpos = fig.layout["xaxis"]["domain"][0] + 0.02
            ypos = fig.layout["yaxis"]["domain"][1] + 0.09
        else:
            xpos = fig.layout["xaxis{}".format(k + 1)]["domain"][0] + 0.02
            ypos = fig.layout["yaxis{}".format(k + 1)]["domain"][1] + 0.09
        annotation = dict(
            x=xpos,
            y=ypos,
            xref="paper",
            yref="paper",
            text="{:5} {}".format("FROM", act_in_names[k]),
            xanchor="left",
            yanchor="top",
            showarrow=False,
        )
        annotations.append(annotation)

        if k == 0:
            # xpos = fig.layout["xaxis"]["domain"][0] + 0.02
            ypos = fig.layout["yaxis"]["domain"][1] + 0.06
        else:
            # xpos = fig.layout["xaxis{}".format(k + 1)]["domain"][0] + 0.02
            ypos = fig.layout["yaxis{}".format(k + 1)]["domain"][1] + 0.06
        annotation = dict(
            x=xpos,
            y=ypos,
            xref="paper",
            yref="paper",
            text="{:7} {}".format("TO", act_out_names[k]),
            xanchor="left",
            yanchor="top",
            showarrow=False,
        )
        annotations.append(annotation)
        ann_number = dict(
            x=xpos - 0.02,
            y=ypos + 0.02,
            xref="paper",
            yref="paper",
            text=str(k + 1),
            xanchor="left",
            yanchor="top",
            showarrow=False,
            font_size=14,
        )
        annotations.append(ann_number)

        k += 1

fig.update_layout(
    annotations=annotations,
    width=1100,
    height=470,
    barmode="overlay",
    margin=dict(l=40, r=40, t=60, b=40),  # TODO change margins
)

# k = 0
# for i in range(rows):
#     for j in range(cols):
#         fig.update_xaxes(title_text=units[k], row=i, col=j, )
#         k += 1
fig.update_yaxes(
    title_text="Frequency",
    row=1,
    col=1,
)
fig.update_yaxes(
    title_text="Frequency",
    row=2,
    col=1,
)
fig.update_yaxes(
    title_text="Frequency",
    row=3,
    col=1,
)

fig.show()

if save_fig:
    if plot_narrow:
        filename = "parameters_histograms_narrowed_div{}_distr.pdf".format(
            scaling_factor
        )
    else:
        filename = "parameters_histograms_standard_div{}_distr.pdf".format(
            scaling_factor
        )
    filepath = path_setac / "figures" / filename
    fig.write_image(str(filepath))

ext: py

reason: import numpy


text:
# -*- coding: utf-8 -*-
"""
Created on Fri Oct 19 01:32:04 2018

@author: Sakshi Panday

@Overview
"""

import time
start_time = time.time()

import finindicators as fi
import getBuySell as gbs
import pandas as pd
import numpy as np
from Prediction import predict

# =============================================================================
# Prepare the data with financial Indicators
# =============================================================================
df = pd.read_csv("Stock Data/AAPL_fulldata.csv")
df = fi.macd(df, 26, 12)
df = fi.stochastic_oscillator_d(df, 14)
df = fi.bollinger_bands(df, 20)
df = fi.moving_average(df, 20)
df = fi.accumulation_distribution(df, 14)
df = fi.chaikin_oscillator(df)
df = fi.commodity_channel_index(df,20)
df = fi.coppock_curve(df,20)
df = fi.keltner_channel(df,20)
df = fi.donchian_channel(df,20)
df = fi.average_true_range(df, 20)
df = fi.average_directional_movement_index(df, 20, 25)
df = fi.money_flow_index(df, 20)
df = fi.vortex_indicator(df, 20)

#df.to_csv("Stock Data/datasets/WFC_fin_train.csv", sep=',')

#df = gbs.getMACDhl(df)
#df = gbs.getSOhl(df)
#df = gbs.getBBhl(df)
df = gbs.getRealhl(df, 15)
df = df.dropna()

# =============================================================================
# choose feature selected indicators and predict
# =============================================================================

#allIndicators = ['MACD_26_12', 'SO%d_14', 'BollingerB_20', 'Bollinger%b_20', 'MA_20', 'Acc/Dist_ROC_14', 'Chaikin', 'CCI_20', 'Copp_20', 'KelChM_20', 'KelChU_20', 'KelChD_20', 'Donchian_20', 'ATR_20', 'ADX_20_25', 'MFI_20', 'Vortex_20']
allSelectedIndicators = ['MACD_26_12', 'SO%d_14', 'Bollinger%b_20', 'Acc/Dist_ROC_14', 'Chaikin', 'CCI_20', 'Copp_20', 'KelChD_20', 'Donchian_20', 'ATR_20', 'ADX_20_25', 'MFI_20', 'Vortex_20']

'''supress version warnings'''
import warnings
warnings.filterwarnings("ignore")

predict(df, allSelectedIndicators)

'''
For each indicator seperately
'''
#for indicator in allIndicators:
#    predict(df, indicator)

#print ("My program took", time.time() - start_time, "to run")

max_stars_repo_name: ShobhitLamba/Stocker

max_issues_repo_name: ShobhitLamba/Stocker

max_forks_repo_name: ShobhitLamba/Stocker


text:
from transformers import GPT2ForSequenceClassification
from classifier import GPT2Classifier
import torch
from torch import nn
import pytorch_lightning as pl
import torchmetrics
import numpy as np

class Embedder(pl.LightningModule):
    def __init__(
        self,
        checkpoint_path: str = "~/search-query-classification/pretrain_model/pretrain-checkpoints/model-epoch=01-val"
        "/loss=0.00.ckpt",
        lr: float = 1e-4,
        weights: list = None,
        num_labels=7,
        use_pretrained=True,
    ):
        super().__init__()
        transformer = GPT2Classifier.load_from_checkpoint(checkpoint_path)
        self.embedder = transformer.transformer.transformer

        if weights:
            weights = torch.tensor(weights)
            self.loss = nn.CrossEntropyLoss(weight=weights)
        else:
            self.loss = nn.CrossEntropyLoss()
        self.learning_rate = lr
        self.train_acc = torchmetrics.Accuracy()
        self.valid_acc = torchmetrics.Accuracy()

    def forward(self, input_ids, attention_mask):
        return self.embedder(input_ids=input_ids, attention_mask=attention_mask)[0][
            :, -1, :
        ]

    def predict_step(self, batch, batch_idx):
        input_ids, attention_mask = batch["input_ids"], batch["attention_mask"]
        label = batch["label"][0]
        preds = self(input_ids, attention_mask)
        with open(f"preds/{label}_{batch_idx}.npy", "wb") as f:
            np.save(f, preds.cpu())
        del preds


text:
# -*- coding: utf-8 -*-
# Author: Haoran Chen
# Date: 2019-05-26
from sklearn.metrics import f1_score, precision_score, recall_score, precision_recall_curve
import numpy as np

msrvtt_gt = 'msrvtt_tag_gt_4_msrvtt.npy'
msrvtt_tag_feats = ['msrvtt_e100_tag_feats.npy', 'msrvtt_e200_tag_feats.npy',
                    'msrvtt_e400_tag_feats.npy', 'msrvtt_e800_tag_feats.npy',
                    'msrvtt_e1000_tag_feats.npy']

msvd_gt = 'msvd_tag_gt_4_msvd.npy'
msvd_tag_feats = ['msvd_semantic_tag_e100.npy', 'msvd_semantic_tag_e200.npy',
                'msvd_semantic_tag_e400.npy', 'msvd_semantic_tag_e800.npy',
                'msvd_semantic_tag_e1000.npy']

msrvtt_res = {}
label = np.load(msrvtt_gt)
for mfeat in msrvtt_tag_feats:
    pred = np.load(mfeat)
    ap_list = []
    for j in range(pred.shape[1]):
        precision, recall, thresholds = precision_recall_curve(label[:,j], pred[:,j])
        ap = np.mean(precision)
        ap_list.append(ap)
    count = int(mfeat.split('_')[1][1:])
    msrvtt_res[count] = np.mean(ap_list)

msvd_res = {}
label = np.load(msvd_gt)
for mfeat in msvd_tag_feats:
    pred = np.load(mfeat)
    ap_list = []
    for j in range(pred.shape[1]):
        precision, recall, thresholds = precision_recall_curve(label[:,j], pred[:,j])
        ap = np.mean(precision)
        ap_list.append(ap)
    count = int(mfeat.split('_')[3].split('.')[0][1:])
    msvd_res[count] = np.mean(ap_list)

from pprint import pprint
print('MSRVTT results:')
pprint(msrvtt_res)

print('MSVD results:')
pprint(msvd_res)


text:
import pandas as pd
import numpy as np
from datetime import datetime
from datetime import timedelta
import settings
import const
from src import util
from src.preprocess import times
from src.preprocess.preprocess import PreProcess
from src.feature_generators.hybrid_fg import HybridFG
from src.methods.hybrid import Hybrid

config = settings.config[const.DEFAULT]

today = times.to_datetime(datetime.utcnow().date())
# tomorrow
date_border = times.to_datetime(today + timedelta(days=1))
# # 2 days before
# date_border = times.to_datetime(today - timedelta(days=5))

cases = {
    'BJ': {
        # 'PM2.5': True,
        # 'PM10': True,
        # 'O3': True,
    },
    'LD': {
        'PM2.5': True,
        # 'PM10': True,
    }
}

model_basic_cfg = {
    const.MODEL_DIR: config[const.MODEL_DIR],
    const.STATIONS: None,
    const.POLLUTANT: None,
    const.LOSS_FUNCTION: const.MEAN_PERCENT,
}

model_cfgs = {
    'BJ': {
        const.CITY: const.BJ,
    },
    'LD': {
        const.CITY: const.LD,
    }
}

print('Date border:', (date_border - timedelta(hours=1)).strftime(const.T_FORMAT_FULL))

outputs = list()
smape_total = 0
smape_count = 0
for city, pollutants in cases.items():
    observed = pd.read_csv(config[getattr(const, city + "_OBSERVED")], sep=';', low_memory=True)
    stations = pd.read_csv(config[getattr(const, city + "_STATIONS")], sep=';', low_memory=True)
    # keep only a necessary time range for feature generation for only predictable stations
    observed = times.select(df=observed, time_key=const.TIME, from_time='18-05-01 00')
    observed[const.TIME] = pd.to_datetime(observed[const.TIME], format=const.T_FORMAT)
    observed.sort_values(by=[const.ID, const.TIME], inplace=True)
    # Fill all remaining null values that were to wide to be filled in general pre-processing
    # nan_rows = pd.isnull(observed[const.PM25]).sum()
    pre_process = PreProcess().fill(observed=observed, stations=stations)
    observed = pre_process.get_observed()
    # print('Nan PM2.5 before {b} and after {a} filling'.format(
    #     b=nan_rows, a=pd.isnull(observed[const.PM25]).sum()))

    # Model configuration (city dependant)
    model_cfg = model_cfgs[city]
    model_cfg.update(model_basic_cfg)
    fg_cfg_basic = HybridFG.get_size_config(city=city, key='05-02')
    model_cfg.update(fg_cfg_basic)
    model_cfg[const.STATIONS] = config[getattr(const, city + '_STATIONS')]

    for pollutant in pollutants:
        fg_cfg = {const.CITY: city, const.POLLUTANT: pollutant}
        fg_cfg.update(fg_cfg_basic)
        fg = HybridFG(cfg=fg_cfg)
        # generate features to predict the pollutant on next 48 hours
        features = fg.generate(ts=observed, stations=stations, verbose=False, save=False).get_features()

        # only keep features with time = 23:00
        features = times.select(features, const.TIME,
                                from_time=date_border - timedelta(hours=1), to_time=date_border)

        # explode features to model inputs
        exploded = fg.explode(features=features)

        # initial model to predict
        model_cfg[const.POLLUTANT] = pollutant
        model = Hybrid(model_cfg).load_model(mode='best')
        predictions = model.predict(x=exploded)
        # test the model if date_border is set before today
        if date_border <= today:
            actual = features[fg.get_label_columns()].as_matrix()
            smape = util.SMAPE(forecast=predictions, actual=actual)
            print(' Test SMAPE', smape)
            smape_total += smape * actual.size
            smape_count += actual.size
        # add _aq prefix back for beijing stations
        station_col = (features[const.ID] + '_aq') if city == const.BJ else features[const.ID]
        pollutant_col = [pollutant for _ in range(0, len(predictions))]
        output = [[sid] + [pol] + p for sid, pol, p in
                  zip(station_col.tolist(), pollutant_col, predictions.tolist())]
        # add (city, pollutant) result to all outputs
        outputs.extend(output)
        print(' %s %s predicted.' % (city, pollutant))
        print('_________________________')

# convert result to desired format (station_id, hour, pm2.5, pm10, o3)
results = list()
mapper = dict()
row = 0
pollutant_position = {const.PM25: 1, const.PM10: 2, const.O3: 3}
for i, output in enumerate(outputs):
    sid = output[0]
    pollutant = output[1]
    for hour, value in enumerate(output[2:]):
        row_key = '%s#%s' % (sid, hour)
        if row_key not in mapper:
            mapper[row_key] = row
            results.append([row_key, np.nan, np.nan, np.nan])
            row += 1
        results[mapper[row_key]][pollutant_position[pollutant]] = value

if date_border <= today:
    print('Total SMAPE', smape_total / smape_count)

df = pd.DataFrame(data=results, columns=['test_id', 'PM2.5', 'PM10', 'O3'])
submit_path = config[const.SUBMIT_DIR] + "result___" + \
              (date_border - timedelta(hours=1)).strftime("%Y_%m_%d") + ".csv"
df.to_csv(submit_path, sep=',', index=False, float_format='%.3f')


text:
#!/usr/bin/python
# Dependencies from the python standard library:
from pathlib import Path
import shutil          # For copying files
import subprocess      # For calling ffmpeg to make animations
import urllib.request  # For downloading raw data
import zipfile         # For unzipping downloads
# You can use 'pip' to install these dependencies:
import numpy as np
import matplotlib.pyplot as plt
from tifffile import imread, imwrite # v2020.6.3 or newer

# You'll need ffmpeg installed to make animations. If you can't type
# "ffmpeg --version" at the command prompt without an error, my code
# will probably fail too.

input_dir = ( # A temp directory, three folders up:
    Path(__file__).parents[3] /
    'relaxation_sensors_temp_files' / 'mito_ph_data')
temp_dir = ( # A temp directory, three folders up:
    Path(__file__).parents[3] /
    'relaxation_sensors_temp_files' / 'mito_figure')
output_dir = ( # The 'images' directory, two folders up:
    Path(__file__).parents[2] / 'images' /
    'Appendix_cos7_mitochondria_ph_figure')
# Sanity checks:
input_dir.mkdir(exist_ok=True, parents=True)
temp_dir.mkdir(exist_ok=True, parents=True)
output_dir.mkdir(exist_ok=True)

def main():
    # Estimated by running 'calibration.py':
    relaxation_ratios_to_ph = np.array((
        (0.09, 6.5),
        (0.12, 7.0),
        (0.21, 7.5),
        (0.49, 8.0)))

    data = load_data()
    # 30 measurements, with 14 frames per measurement:
    data = data.reshape(30, 14, 602, 671)
    # First measurement didn't prepare with 405 nm light, so skip it:
    data = data[1:, :, :, :]

    # We're only calculating average pH for the whole frame, so sum up all
    # the spatial pixels to estimate pH:
    brightness = data.sum(axis=(-1, -2))
    normalized_brightness = brightness - brightness[:, 0:1]
    normalized_brightness = (normalized_brightness /
                             normalized_brightness[:, 9:10])
    relaxation_ratios = (normalized_brightness[:, 9] -
                         normalized_brightness[:, 10])
    time = 7 * np.arange(len(relaxation_ratios))

    print("Generating animation frames...", end='')
    for which_timepoint in range(data.shape[0]):
        print('.', end='', sep='')
        luminance = adjust_contrast(data[which_timepoint, 9],
                                    data[which_timepoint, 9].min(),
                                    data[which_timepoint, 9].max()
                                    )[..., np.newaxis]
        hue = seminormalized_turbo(
            adjust_contrast(np.atleast_2d(relaxation_ratios[which_timepoint]),
                            0.09, 0.49))
        show_me = hue * luminance

        for patch, ratio in enumerate((0.09, 0.12, 0.21, 0.49)):
            ph_calibration_hue = seminormalized_turbo(
                adjust_contrast(np.atleast_2d(ratio), .09, .49))
            sl_y, sl_x = slice(-100, -20), slice(-60*(patch+2), -60*(patch+1))
            show_me[sl_y, sl_x, :] = 1
            show_me[sl_y, sl_x, :] *= ph_calibration_hue

        fig = plt.figure(figsize=(10, 4))
        ax1 = plt.axes([0.0, 0.15, 0.38, 0.8])
        ax1.imshow(show_me, interpolation='nearest')
        ax1.set_xticks([])
        ax1.set_yticks([])
        ax1.text(280, 630, "pH measured via relaxation rate",
            fontdict={'color': (0, 0, 0),
                      'weight': 'bold',
                      'size': 9})
        for xp, txt in ((380, "8.0"),
                        (440, "7.5"),
                        (500, "7.0"),
                        (560, "6.5"),):
            ax1.text(xp, 550, txt,
                fontdict={'color': (0, 0, 0),
                          'weight': 'bold',
                          'size': 9})

        ax2 = plt.axes([0.45, 0.15, 0.5, 0.8])
        ax2.plot(time, relaxation_ratios, '.-', c='black')
        ax2.grid('on')
        ax2.set_xlabel("Time (s)")
        ax2.set_ylabel("Relaxation ratio")
        ax2.axvline(time[which_timepoint], alpha=0.3, c='black')
        ax2.annotate("CCCP", xy=(time[5], 0.4), xytext=(time[5]-25, 0.35),
                     arrowprops=dict(facecolor='black'))
        for ratio in (0.09, 0.12, 0.21, 0.49):
            line_color = seminormalized_turbo(
                adjust_contrast(np.atleast_2d(ratio), 0.09, 0.49))[0, 0, :]
            ax2.axhline(ratio, alpha=0.3, c=line_color, linewidth=5)
        plt.savefig(temp_dir / ("animation_frame_%06i.png"%which_timepoint))
        plt.close(fig)
    print("done.")

    # Animate the frames into a gif:
    palette = temp_dir / "palette.png"
    filters = "scale=trunc(iw/2)*2:trunc(ih/2)*2:flags=lanczos"
    print("Converting pngs to gif...", end=' ')
    convert_command_1 = [
        'ffmpeg',
        '-f', 'image2',
        '-i', str(temp_dir / ("animation_frame_%06d.png")),
        '-vf', filters + ",palettegen",
        '-y', palette]
    convert_command_2 = [
        'ffmpeg',
        '-framerate', '8',
        '-f', 'image2',
        '-i', str(temp_dir / ('animation_frame_%6d.png')),
        '-i', palette,
        '-lavfi', filters + " [x]; [x][1:v] paletteuse",
        '-y', str(output_dir / ("1_data_animation.gif"))]
    for convert_command in convert_command_1, convert_command_2:
        try:
            with open(temp_dir / 'conversion_messages.txt', 'wt') as f:
                f.write("So far, everthing's fine...\n")
                f.flush()
                subprocess.check_call(convert_command, stderr=f, stdout=f)
                f.flush()
            (temp_dir / 'conversion_messages.txt').unlink()
        except: # This is unlikely to be platform independent :D
            print("GIF conversion failed. Is ffmpeg installed?")
            raise
    print('done.')

def load_data():
    image_data_filename = input_dir / 'data.tif'
    if not image_data_filename.is_file():
        print("The expected data file:")
        print(image_data_filename)
        print("...isn't where we expect it.\n")
        print(" * Let's try to unzip it...")
        zipped_data_filename = input_dir / "mito_ph_data.zip"
        if not zipfile.is_zipfile(zipped_data_filename):
            print("\n  The expected zipped data file:")
            print(zipped_data_filename)
            print("  ...isn't where we expect it.\n")
            print(" * * Let's try to download it from Zenodo.")
            download_data(zipped_data_filename)
        assert zipped_data_filename.is_file()
        assert zipfile.is_zipfile(zipped_data_filename)
        print(" Unzipping...")
        with zipfile.ZipFile(zipped_data_filename) as zf:
            zf.extract(image_data_filename.name,
                       image_data_filename.parent)
        print(" Successfully unzipped data.\n")
    assert image_data_filename.is_file()
    print("Loading data...")
    data = imread(image_data_filename).astype('float64')
    print("Data shape:", data.shape)
    print("Data dtype:", data.dtype)
    print()
    return data

def download_data(filename):
    url="https://zenodo.org/record/5816195/files/mito_ph_data.zip"
    u = urllib.request.urlopen(url)
    file_size = int(u.getheader("Content-Length"))
    block_size = 8192
    while block_size * 80 < file_size:
        block_size *= 2
    bar_size = max(1, int(0.5 * (file_size / block_size - 12)))

    print("    Downloading from:")
    print(url)
    print("    Downloading to:")
    print(filename)
    print("    File size: %0.2f MB"%(file_size/2**20))
    print("\nDownloading might take a while, so here's a progress bar:")
    print('0%', "-"*bar_size, '50%', "-"*bar_size, '100%')
    with open(filename, 'wb') as f:
        while True:
            buffer = u.read(block_size)
            if not buffer:
                break
            f.write(buffer)
            print('|', sep='', end='')
    print("\nDone downloading.\n")
    return None

def seminormalized_turbo(x):
    """Flat-luminance version of
    ai.googleblog.com/2019/08/turbo-improved-rainbow-colormap-for.html
    """
    turbo = plt.get_cmap('turbo')(x)[..., :-1]
    luminance = (0.2126 * turbo[..., 0] +
                 0.7152 * turbo[..., 1] +
                 0.0722 * turbo[..., 2])
    norm_turbo = turbo / np.expand_dims(luminance, -1)
    return np.clip(0.5 * norm_turbo, 0, 1) # Not-quite-flat luminance

def adjust_contrast(img, min_, max_, gamma=1):
    """Like setting "minimum" and "maximum" image contrast in ImageJ

    Output image intensity will range from zero to one.
    Non-quantitative, just useful for display. Be careful and honest if
    you use gamma != 1 (presumably to show higher dynamic range)
    """
    img = np.clip(img, min_, max_)
    img -= min_
    img /= max_ - min_
    img = img**gamma
    img = np.clip(img, 0, 1) # Fix small numerical errors from prev. line
    return img

main()


text:
import unittest
import os.path
import numpy as np
from datetime import datetime

from eolearn.core import EOPatch, FeatureType
from eolearn.mask import MaskFeature
from eolearn.features import ReferenceScenes, BlueCompositing, HOTCompositing, MaxNDVICompositing, \
    MaxNDWICompositing, MaxRatioCompositing, HistogramMatching

class TestRadiometricNormalization(unittest.TestCase):

    class RadiometricNormalizationTestCase:
        """
        Container for each interpolation test case
        """
        TEST_PATCH_FILENAME = os.path.join(os.path.dirname(os.path.realpath(__file__)), '../../../example_data',
                                           'TestEOPatch')

        def __init__(self, name, task, img_min=None, img_max=None, img_mean=None, img_median=None):
            self.name = name
            self.task = task
            self.img_min = img_min
            self.img_max = img_max
            self.img_mean = img_mean
            self.img_median = img_median
            self.result = None

        def execute(self):
            patch = EOPatch.load(self.TEST_PATCH_FILENAME)
            np.random.seed(0)
            patch.mask['SCL'] = np.random.randint(0, 11, patch.data['BANDS-S2-L1C'].shape, np.uint8)
            blue = BlueCompositing((FeatureType.DATA, 'REFERENCE_SCENES'),
                                   (FeatureType.DATA_TIMELESS, 'REFERENCE_COMPOSITE'), blue_idx=0,
                                   interpolation='geoville')
            blue.execute(patch)
            self.result = self.task.execute(patch)

    @classmethod
    def setUpClass(cls):

        cls.test_cases = [
            cls.RadiometricNormalizationTestCase('mask feature', MaskFeature(
                (FeatureType.DATA, 'BANDS-S2-L1C', 'TEST'), (FeatureType.MASK, 'SCL'),
                mask_values=[0, 1, 2, 3, 8, 9, 10, 11]), img_min=0.0002, img_max=1.4244, img_mean=0.21167801,
                img_median=0.142),

            cls.RadiometricNormalizationTestCase('reference scene', ReferenceScenes(
                (FeatureType.DATA, 'BANDS-S2-L1C', 'TEST'), (FeatureType.SCALAR, 'CLOUD_COVERAGE'),
                max_scene_number=5), img_min=0.0005, img_max=0.5318, img_mean=0.16823094,
                img_median=0.1404),

            cls.RadiometricNormalizationTestCase('blue compositing', BlueCompositing(
                (FeatureType.DATA, 'REFERENCE_SCENES'), (FeatureType.DATA_TIMELESS, 'TEST'),
                blue_idx=0, interpolation='geoville'), img_min=0.0005, img_max=0.5075, img_mean=0.11658352,
                img_median=0.0833),

            cls.RadiometricNormalizationTestCase('hot compositing', HOTCompositing(
                (FeatureType.DATA, 'REFERENCE_SCENES'), (FeatureType.DATA_TIMELESS, 'TEST'),
                blue_idx=0, red_idx=2, interpolation='geoville'), img_min=0.0005, img_max=0.5075, img_mean=0.117758796,
                img_median=0.0846),

            cls.RadiometricNormalizationTestCase('max ndvi compositing', MaxNDVICompositing(
                (FeatureType.DATA, 'REFERENCE_SCENES'), (FeatureType.DATA_TIMELESS, 'TEST'),
                red_idx=2, nir_idx=7, interpolation='geoville'), img_min=0.0005, img_max=0.5075,
                img_mean=0.13430128, img_median=0.0941),

            cls.RadiometricNormalizationTestCase('max ndwi compositing', MaxNDWICompositing(
                (FeatureType.DATA, 'REFERENCE_SCENES'), (FeatureType.DATA_TIMELESS, 'TEST'),
                nir_idx=6, swir1_idx=8, interpolation='geoville'), img_min=0.0005, img_max=0.5318,
                img_mean=0.2580135, img_median=0.2888),

            cls.RadiometricNormalizationTestCase('max ratio compositing', MaxRatioCompositing(
                (FeatureType.DATA, 'REFERENCE_SCENES'), (FeatureType.DATA_TIMELESS, 'TEST'),
                blue_idx=0, nir_idx=6, swir1_idx=8, interpolation='geoville'), img_min=0.0006, img_max=0.5075,
                img_mean=0.13513365, img_median=0.0958),

            cls.RadiometricNormalizationTestCase('histogram matching', HistogramMatching(
                (FeatureType.DATA, 'BANDS-S2-L1C', 'TEST'),
                (FeatureType.DATA_TIMELESS, 'REFERENCE_COMPOSITE')),
                img_min=-0.049050678, img_max=0.68174845, img_mean=0.1165936, img_median=0.08370649)
        ]

        for test_case in cls.test_cases:
            test_case.execute()

    def test_output_type(self):
        for test_case in self.test_cases:
            with self.subTest(msg='Test case {}'.format(test_case.name)):
                self.assertTrue(isinstance(test_case.result.timestamp, list), "Expected a list of timestamps")
                self.assertTrue(isinstance(test_case.result.timestamp[0], datetime),
                                "Expected timestamps of type datetime.datetime")

    def test_stats(self):
        for tci, test_case in enumerate(self.test_cases):
            delta = 1e-3

            if tci not in [2, 3, 4, 5, 6]:
                if test_case.img_min is not None:
                    min_val = np.nanmin(test_case.result.data['TEST'])
                    with self.subTest(msg='Test case {}'.format(test_case.name)):
                        self.assertAlmostEqual(test_case.img_min, min_val, delta=delta,
                                               msg="Expected min {}, got {}".format(test_case.img_min, min_val))
                if test_case.img_max is not None:
                    max_val = np.nanmax(test_case.result.data['TEST'])
                    with self.subTest(msg='Test case {}'.format(test_case.name)):
                        self.assertAlmostEqual(test_case.img_max, max_val, delta=delta,
                                               msg="Expected max {}, got {}".format(test_case.img_max, max_val))
                if test_case.img_mean is not None:
                    mean_val = np.nanmean(test_case.result.data['TEST'])
                    with self.subTest(msg='Test case {}'.format(test_case.name)):
                        self.assertAlmostEqual(test_case.img_mean, mean_val, delta=delta,
                                               msg="Expected mean {}, got {}".format(test_case.img_mean, mean_val))
                if test_case.img_median is not None:
                    median_val = np.nanmedian(test_case.result.data['TEST'])
                    with self.subTest(msg='Test case {}'.format(test_case.name)):
                        self.assertAlmostEqual(test_case.img_median, median_val, delta=delta,
                                               msg="Expected median {}, got {}".format(test_case.img_median,
                                                                                median_val))

            else:
                if test_case.img_min is not None:
                    min_val = np.nanmin(test_case.result.data_timeless['TEST'])
                    with self.subTest(msg='Test case {}'.format(test_case.name)):
                        self.assertAlmostEqual(test_case.img_min, min_val, delta=delta,
                                               msg="Expected min {}, got {}".format(test_case.img_min, min_val))
                if test_case.img_max is not None:
                    max_val = np.nanmax(test_case.result.data_timeless['TEST'])
                    with self.subTest(msg='Test case {}'.format(test_case.name)):
                        self.assertAlmostEqual(test_case.img_max, max_val, delta=delta,
                                               msg="Expected max {}, got {}".format(test_case.img_max, max_val))
                if test_case.img_mean is not None:
                    mean_val = np.nanmean(test_case.result.data_timeless['TEST'])
                    with self.subTest(msg='Test case {}'.format(test_case.name)):
                        self.assertAlmostEqual(test_case.img_mean, mean_val, delta=delta,
                                               msg="Expected mean {}, got {}".format(test_case.img_mean, mean_val))
                if test_case.img_median is not None:
                    median_val = np.nanmedian(test_case.result.data_timeless['TEST'])
                    with self.subTest(msg='Test case {}'.format(test_case.name)):
                        self.assertAlmostEqual(test_case.img_median, median_val, delta=delta,
                                               msg="Expected median {}, got {}".format(test_case.img_median,
                                                                                median_val))

if __name__ == '__main__':
    unittest.main()

max_stars_repo_name: HugoFRN/eo-learn

max_issues_repo_name: HugoFRN/eo-learn

max_forks_repo_name: HugoFRN/eo-learn


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

from scipy.stats import expon
from scipy.stats import gaussian_kde

def main():
	with open('BTCUSDT.json', 'r') as f:
		trades = ijson.items(f, 'item')

		previous_price = None
		previous_time = None

		interarrivals = []
		changes = []

		for i, trade in enumerate(trades):
			#if previous_price is not None and int(trade['time']) - previous_time > 2000:
			if previous_price is not None:
				interarrival = int(trade['time']) - previous_time
				interarrivals.append(interarrival)
				changes.append(float(trade['price']) - previous_price)
			if i > 10000:
				break

			previous_price = float(trade['price'])
			previous_time = int(trade['time'])

		to_remove = []

		for i, dt in enumerate(interarrivals):
			if dt < 50:
				to_remove.append(i)

		for index in reversed(to_remove):
			interarrivals.pop(index)

		plt.hist(interarrivals, 100, density=True, range=(0, 6000))

		loc, scale = expon.fit(interarrivals)
		x = np.linspace(0, 6000, 100)
		plt.plot(x, expon.pdf(x, scale=scale))

		plt.show()

if __name__ == '__main__':
	main()

max_stars_repo_path: PreliminaryAnalysis/Individual Trade Analysis/Further Properties/time_between_trades.py

reason: import numpy,from scipy


text:
# FastSpeech2 Decoder
import numpy as np
import tensorflow as tf
from tensorflow_tts.modules.FFT import Feed_Forward_Transformer

class Embedding(tf.keras.layers.Embedding):
    """Faster version of embedding."""
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

    def call(self, inputs):
        inputs = tf.cast(inputs, tf.int32)
        outputs = tf.gather(self.embeddings, inputs)
        return outputs

class Decoder(tf.keras.layers.Layer):
    """Fast Speech decoder module."""
    def __init__(self, config, is_compatible_encoder, **kwargs):
        super().__init__(**kwargs)
        self.config = config
        self.is_compatible_encoder = is_compatible_encoder
        # create decoder positional embedding
        self.decoder_positional_embeddings = Embedding(
            config.max_position_embeddings + 1,
            config.hidden_size,
            weights=[self._sincos_embedding()],
            name="position_embeddings",
            trainable=False,
        )
        # layers
        self.layer = [
            Feed_Forward_Transformer(config, name="layer_._{}".format(i))
            for i in range(config.num_hidden_layers)
        ]
        if self.is_compatible_encoder is False:
            self.project_compatible_decoder = tf.keras.layers.Dense(
                units=config.hidden_size, name="project_compatible_decoder"
            )

    def call(self, inputs, training=False):
        hidden_states, speaker_ids, encoder_mask, decoder_pos = inputs
        # project input
        if self.is_compatible_encoder is False:
            hidden_states = self.project_compatible_decoder(hidden_states)
        # calculate new hidden states.
        hidden_states += tf.cast(
            self.decoder_positional_embeddings(decoder_pos), hidden_states.dtype
        )
        for layer_module in self.layer:
            hidden_states = layer_module(
                [hidden_states, encoder_mask], training=training
            )
        return (hidden_states,)

    def _sincos_embedding(self):
        position_enc = np.array(
            [
                [
                    pos / np.power(10000, 2.0 * (i // 2) / self.config.hidden_size)
                    for i in range(self.config.hidden_size)
                ]
                for pos in range(self.config.max_position_embeddings + 1)
            ]
        )

        position_enc[:, 0::2] = np.sin(position_enc[:, 0::2])
        position_enc[:, 1::2] = np.cos(position_enc[:, 1::2])

        # pad embedding.
        position_enc[0] = 0.0
        return position_enc


text:
import numpy as np
import scipy.fftpack
import utils as utils

def choose_frequencies():
    """
    # provide three frequencies in a range between 1 and 50
    :return: [int, int, int]
    """
    # *** TODO provide three frequencies between 1 and 50
    freq1 = 10
    freq2 = 25
    freq3 = 40
    # end TODO

    return [freq1, freq2, freq3]

def add_the_waves(freqs):
    """
    create three sinusoidal waves and one wave that is the sum of the three
    :param freqs: [int, int, int]
    :return: [np.array, np.array, np.array, np.array]
        representing wave1, wave2, wave3, sum of waves
        each array contains 500(by default) discrete values for plotting a sinusoidal
    """
    _, _, t = utils.get_wave_timing()
    w1, w2, w3 = utils.make_waves(t, freqs)

    # TODO sum the waves together to form sum_waves
    sum_waves = w1+w2+w3
    # end TODO

    return [w1, w2, w3, sum_waves]

def demo_fft(sum_waves):
    num_samples, spacing, _ = utils.get_wave_timing()

    # TODO create a Fast Fourier Transform of the waveform using scipy.fftpack.fft
    # named 'y_fft'
    y_fft = scipy.fftpack.fft(sum_waves, num_samples)
    # end TODO

    x_fft = np.linspace(0.0, 1.0/spacing, num_samples)
    return x_fft, y_fft

max_stars_repo_name: emarkou/Audio-auto-tagging

max_issues_repo_name: emarkou/Audio-auto-tagging

max_forks_repo_name: emarkou/Audio-auto-tagging

reason: import numpy,import scipy


text:
from PIL import Image, ImageFont, ImageDraw
import os
import numpy as np
from vito import imutils
from vito import imvis

def load_font(font_size):
    fonts = ['EPDFont', 'xkcdext-Regular', 'xkcd-Regular']
    for fs in fonts:
        for ext in ['.ttc', '.otf', '.ttf']:
            try:
                font = ImageFont.truetype(os.path.join('..', 'assets', fs + ext), size=font_size, encoding="unic", index=0, layout_engine=ImageFont.LAYOUT_RAQM)
                return font
            except IOError as e:
                print(fs + ext, e)
                pass
    return None

def cvt_bw(img_pil):
    # Simply converting looks pretty ugly
    # bw_pil = img_pil.convert('1')
    # bw_pil.show()
    img_np = np.asarray(img_pil)
    # gray = imutils.rgb2gray(img_np)
    # imvis.imshow(gray, 'gray np')
    # print(gray.dtype)
    mask_np = np.max(img_np, axis=2) > 210
    # imvis.imshow(masked)
    # import iminspect
    # iminspect.show(masked)
    bw = Image.fromarray(mask_np)
    bw.show()

def draw_xkcd_text():
    scale_factor = 1
    # Images look way nicer if text is drawn on 3-channel image
    width, height = 400, 300
    font_size = 42
    img = Image.new('RGB', (scale_factor * width, scale_factor * height), 'white') # 1: clear the frame
    draw = ImageDraw.Draw(img)

    font = load_font(scale_factor * font_size)
    draw.text((scale_factor * 10, scale_factor * 30), "Hello", font=font, fill=(0, 0, 0))
    draw.text((scale_factor * 10, scale_factor * 100), "Hello @\u03c0 3.14", font=font, fill=(0, 0, 0))
    draw.rectangle((scale_factor * 200, scale_factor * 80, scale_factor * 360, scale_factor * 280), fill=0)

    # Default text rendering with PIL is ugly to say the least...
    # https://stackoverflow.com/questions/5414639/python-imaging-library-text-rendering
    img.show()
    if scale_factor == 1:
        print('NOT SCALING')
    else:
        img = img.resize((width, height), Image.ANTIALIAS) #Image.LINEAR) #Image.BICUBIC)# Image.ANTIALIAS)
    # img.show()
    cvt_bw(img)

if __name__ == '__main__':
    draw_xkcd_text()

max_stars_repo_name: snototter/ragnaroek

max_issues_repo_name: snototter/ragnaroek

max_forks_repo_name: snototter/ragnaroek


text:
#!/usr/bin/python

import cv2 as cv
from utils import yolo, GrabCut
import numpy as np
import streamlit as st

def run():
    yolopath = "./yolo-fish"
    confidence = 0.25
    threshold = 0.45

    st.title("fish YOLO + GrabCut demo")

    uploaded_img = st.file_uploader("Choose an image...", type=['png', 'jpg', 'bmp', 'jpeg'])
    if uploaded_img is not None:
        file_bytes = np.asarray(bytearray(uploaded_img.read()), dtype=np.uint8)
        image = cv.imdecode(file_bytes, 1)

        st.write("This is your uploaded image:")
        st.image(image, caption='Uploaded Image', channels="BGR", use_column_width=True)

        boxes, idxs = yolo.runYOLOBoundingBoxes_streamlit(image, yolopath, confidence, threshold)
        result_images = GrabCut.runGrabCut(image, boxes, idxs)

        st.write("")
        st.write("finish grabcut")
        st.write(f"There are {len(result_images)} segmented fish image. Each listed as below:")
        for i in range(len(result_images)):
            #cv.imwrite(f'grabcut{i}.jpg', result_images[i])
            st.image(result_images[i], channels="BGR", use_column_width=True)

if __name__ == '__main__':
    run()

max_stars_repo_name: Cuda-Chen/fish-yolo-grabcut

max_issues_repo_name: ZurMaD/fish-yolo-grabcut

max_forks_repo_name: Cuda-Chen/fish-yolo-grabcut


text:
"""
defines:
 - PLOADv
 - PLOAD1v
 - PLOAD2v
 - PLOAD4v
"""
from __future__ import print_function
from collections import defaultdict
from itertools import count
import numpy as np

from pyNastran.bdf.bdf_interface.assign_type import (
    integer, integer_or_blank, double, double_or_blank, string_or_blank,
    integer_string_or_blank, string, fields, components_or_blank)
from pyNastran.bdf.field_writer_8 import (
    print_float_8, print_card_8, set_blank_if_default, set_string8_blank_if_default)
from pyNastran.bdf.field_writer_16 import (
    print_float_16, set_string16_blank_if_default)
from pyNastran.bdf.field_writer_double import print_scientific_double
from pyNastran.bdf.cards.base_card import _format_comment
from pyNastran.bdf.cards.base_card import expand_thru
from pyNastran.dev.bdf_vectorized2.cards.loads.loads import BaseLoad

class PLOADv(BaseLoad):
    """
    Static Pressure Load

    Defines a uniform static pressure load on a triangular or quadrilateral surface
    comprised of surface elements and/or the faces of solid elements.

    +-------+-----+------+----+----+----+----+
    |   1   |  2  |  3   | 4  | 5  | 6  | 7  |
    +=======+=====+======+====+====+====+====+
    | PLOAD | SID |  P   | G1 | G2 | G3 | G4 |
    +-------+-----+------+----+----+----+----+
    | PLOAD |  1  | -4.0 | 16 | 32 | 11 |    |
    +-------+-----+------+----+----+----+----+
    """
    card_name = 'PLOAD'

    def __init__(self, model):
        BaseLoad.__init__(self, model)
        self.pressure = np.array([], dtype='float64')
        self.nids = np.array([], dtype='int32')

        #self._sid = []
        self._pressure = []
        self._nids = []
        self.comment = defaultdict(str)

    def add(self, sid, pressure, nids, comment=''):
        """
        Creates a PLOAD card, which defines a uniform pressure load on a
        shell/solid face or arbitrarily defined quad/tri face.

        Parameters
        ----------
        sid : int
            load id
        pressure : float
            the pressure to apply
        nodes : List[int]
            The nodes that are used to define the normal are defined
            using the same method as the CTRIA3/CQUAD4 normal.
            n = 3 or 4
        comment : str; default=''
            a comment for the card
        """
        if comment:
            self.comment[len(self)] = _format_comment(comment)
        self.is_current = False
        self._sid.append(sid)
        self._pressure.append(pressure)
        self._nids.append(nids)

    def add_card(self, card, comment=''):
        """
        Adds a PLOAD card from ``BDF.add_card(...)``

        Parameters
        ----------
        card : BDFCard()
            a BDFCard object
        comment : str; default=''
            a comment for the card
        """
        sid = integer(card, 1, 'sid')
        pressure = double(card, 2, 'pressure')
        nodes = [
            integer(card, 3, 'n1'),
            integer(card, 4, 'n2'),
            integer(card, 5, 'n3'),
            integer_or_blank(card, 6, 'n4', 0),
        ]
        assert len(card) <= 7, 'len(PLOAD card) = %i\ncard=%s' % (len(card), card)
        self.add(sid, pressure, nodes, comment=comment)

    def write_card(self, size=8, is_double=False, bdf_file=None):
        assert bdf_file is not None
        self.make_current()
        msg = ''
        for i, sid, pressure, node_ids in zip(count(), self.sid, self.pressure, self.nids):
            list_fields = ['PLOAD', sid, pressure] + node_ids.tolist()
            msgi = print_card_8(list_fields)
            msg += self.comment[i] + msgi
        bdf_file.write(msg)
        return msg

    def make_current(self):
        """creates an array of the elements"""
        if not self.is_current:
            if len(self.sid) > 0: # there are already elements in self.eid
                self.sid = np.hstack([self.sid, self._sid])
                self.nids = np.vstack([self.nids, self._nids])

                self.pressure = np.hstack([self.pressure, self._pressure])
                # TODO: need to handle comments
            else:
                self.sid = np.array(self._sid, dtype='int32')
                self.nids = np.array(self._nids, dtype='int32')
                self.pressure = np.array(self._pressure, dtype='float64')

            self._sid = []
            self._nids = []
            self._pressure = []
            self.is_current = True

    def repr_indent(self, indent=''):
        msg = '%sPLOADv:\n' % indent
        msg += '%s  sid = %s\n' % self.sid
        return msg

class PLOAD1v(BaseLoad):
    """
    Applied Load on CBAR, CBEAM or CBEND Elements

    Defines concentrated, uniformly distributed, or linearly distributed
    applied loads to the CBAR or CBEAM elements at user-chosen points
    along the axis. For the CBEND element, only distributed loads over
    an entire length may be defined.

    +--------+-----+------+------+-------+-----+-------+-----+-------+
    |   1    |  2  |  3   |  4   |   5   |  6  |   7   |  8  |   9   |
    +========+=====+======+======+=======+=====+=======+=====+=======+
    | PLOAD1 | SID | EID  | TYPE | SCALE | X1  |  P1   |  X2 |  P2   |
    +--------+-----+------+------+-------+-----+-------+-----+-------+
    | PLOAD1 | 25  | 1065 |  MY  | FRPR  | 0.2 | 2.5E3 | 0.8 | 3.5E3 |
    +--------+-----+------+------+-------+-----+-------+-----+-------+
    """
    card_name = 'PLOAD1'
    valid_types = ['FX', 'FY', 'FZ', 'FXE', 'FYE', 'FZE',
                   'MX', 'MY', 'MZ', 'MXE', 'MYE', 'MZE']

    # LE: length-based; FR: fractional; PR:projected
    valid_scales = ['LE', 'FR', 'LEPR', 'FRPR']

    def __init__(self, model):
        BaseLoad.__init__(self, model)
        self.eid = np.array([], dtype='int32')
        self.load_type = np.array([], dtype='|U4')
        self.scale = np.array([], dtype='|U4')
        self.p12 = np.array([], dtype='float64')
        self.x12 = np.array([], dtype='float64')

        self._sid = []
        self._eid = []
        self._p12 = []
        self._x12 = []
        self._scale = []
        self._load_type = []
        self.comment = defaultdict(str)

    def add(self, sid, eid, load_type, scale, x1, p1, x2=None, p2=None, comment=''):
        """
        Creates a PLOAD1 card, which may be applied to a CBAR/CBEAM

        Parameters
        ----------
        sid : int
            load id
        eid : int
            element to apply the load to
        load_type : str
            type of load that's applied
            valid_types = {FX, FY, FZ, FXE, FYE, FZE,
                           MX, MY, MZ, MXE, MYE, MZE}
        scale : float
            local pressure scaling factor
        x1 / x2 : float / float
            the starting/end position for the load application
            the default for x2 is x1
        p1 / p2 : float / float
            the magnitude of the load at x1 and x2
            the default for p2 is p1
        comment : str; default=''
            a comment for the card

        Point Load       : x1 == x2
        Distributed Load : x1 != x2
        """

        if comment:
            self.comment[len(self)] = _format_comment(comment)
        self.is_current = False
        self._sid.append(sid)
        self._eid.append(eid)
        self._scale.append(scale)
        self._load_type.append(load_type)
        self._p12.append((p1, p2))
        self._x12.append((x1, x2))

    def add_card(self, card, comment=''):
        """
        Adds a PLOAD1 card from ``BDF.add_card(...)``

        Parameters
        ----------
        card : BDFCard()
            a BDFCard object
        comment : str; default=''
            a comment for the card
        """
        sid = integer(card, 1, 'sid')
        eid = integer(card, 2, 'eid')
        load_type = string(card, 3, 'Type ("%s")' % '",  "'.join(self.valid_types))
        scale = string(card, 4, 'scale ("%s")' % '", "'.join(self.valid_scales))
        x1 = double(card, 5, 'x1')
        p1 = double(card, 6, 'p1')
        x2 = double_or_blank(card, 7, 'x2', x1)
        p2 = double_or_blank(card, 8, 'p2', p1)
        assert len(card) <= 9, 'len(PLOAD1 card) = %i\ncard=%s' % (len(card), card)
        self.add(sid, eid, load_type, scale, x1, p1, x2, p2, comment=comment)

    def write_card(self, size=8, is_double=False, bdf_file=None):
        assert bdf_file is not None
        self.make_current()
        msg = ''
        for i, sid, eid, load_typei, scalei, x12, p12 in zip(
            count(), self.sid, self.eid, self.load_type, self.scale, self.x12, self.p12):
            list_fields = ['PLOAD1', sid, eid, load_typei, scalei,
                           x12[0], p12[0], x12[1], p12[1]]
            msgi = print_card_8(list_fields)
            msg += self.comment[i] + msgi
        bdf_file.write(msg)
        return msg

    def make_current(self):
        """creates an array of the elements"""
        if not self.is_current:
            if len(self.sid) > 0: # there are already elements in self.eid
                self.sid = np.hstack([self.sid, self._sid])
                self.eid = np.vstack([self.eid, self._eid])

                self.p12 = np.hstack([self.p12, self._p12])
                self.x12 = np.hstack([self.x12, self._x12])
                self.scale = np.hstack([self.scale, self._scale])
                self.load_type = np.hstack([self.load_type, self._load_type])
                # TODO: need to handle comments
            else:
                self.sid = np.array(self._sid, dtype='int32')
                self.eid = np.array(self._eid, dtype='int32')
                self.load_type = np.array(self._load_type, dtype='|U4')
                self.scale = np.array(self._scale, dtype='|U4')
                self.p12 = np.array(self._p12, dtype='float64')
                self.x12 = np.array(self._x12, dtype='float64')

            self._sid = []
            self._eid = []
            self._p12 = []
            self._x12 = []
            self._scale = []
            self._load_type = []
            self.is_current = True

    def repr_indent(self, indent=''):
        msg = '%sPLOAD1v:\n' % indent
        msg += '%s  sid = %s\n' % self.sid
        return msg

class PLOAD2v(BaseLoad):
    """
    +--------+-----+------+------+------+------+------+------+------+
    |    1   |   2 |  3   |  4   |   5  |   6  |   7  |   8  |   9  |
    +========+=====+======+======+======+=============+======+======+
    | PLOAD2 | SID |  P   | EID1 | EID2 | EID3 | EID4 | EID5 | EID6 |
    +--------+-----+------+------+------+------+------+------+------+
    | PLOAD2 | 21  | -3.6 |  4   |  16  |  2   |      |      |      |
    +--------+-----+------+------+------+------+------+------+------+
    | PLOAD2 | SID |  P   | EID1 | THRU | EID2 |      |      |      |
    +--------+-----+------+------+------+------+------+------+------+
    """
    card_name = 'PLOAD2'

    def __init__(self, model):
        BaseLoad.__init__(self, model)
        self.pressure = np.array([], dtype='float64')
        self.eid = np.array([], dtype='int32')

        self._pressure = []
        self._eid = []
        self.comment = defaultdict(str)

    def add(self, sid, pressure, eids, comment=''):
        """
        Creates a PLOAD2 card, which defines an applied load normal to the quad/tri face

        Parameters
        ----------
        sid : int
            load id
        pressure : float
            the pressure to apply to the elements
        eids : List[int]
            the elements to apply pressure to
            n < 6 or a continouus monotonic list of elements (e.g., [1, 2, ..., 1000])
        comment : str; default=''
            a comment for the card
        """
        if comment:
            self.comment[len(self)] = _format_comment(comment)
        self.is_current = False
        self._sid.append(sid)
        self._pressure.append(pressure)
        self._eid.append(eids)

    def add_card(self, card, comment=''):
        """
        Adds a PLOAD2 card from ``BDF.add_card(...)``

        Parameters
        ----------
        card : BDFCard()
            a BDFCard object
        comment : str; default=''
            a comment for the card
        """
        sid = integer(card, 1, 'sid')
        pressure = double(card, 2, 'p')

        if integer_string_or_blank(card, 4, 'THRU') == 'THRU':
            e1 = integer(card, 3, 'Element1')
            e2 = integer(card, 5, 'Element1')
            eids = [i for i in range(e1, e2 + 1)]
            assert len(card) == 6, 'len(PLOAD2 card) = %i\ncard=%s' % (len(card), card)
        else:
            eids = fields(integer, card, 'eid', i=3, j=len(card))
        for eid in eids:
            self.add(sid, pressure, eid, comment=comment)
            comment = ''

    def write_card(self, size=8, is_double=False, bdf_file=None):
        assert bdf_file is not None
        self.make_current()
        msg = ''
        for i, sid, pressure, eid in zip(count(), self.sid, self.pressure, self.eid):
            list_fields = ['PLOAD2', sid, pressure, eid]
            msgi = print_card_8(list_fields)
            msg += self.comment[i] + msgi
        bdf_file.write(msg)
        return msg

    def make_current(self):
        """creates an array of the elements"""
        if not self.is_current:
            if len(self.sid) > 0: # there are already elements in self.eid
                self.sid = np.hstack([self.sid, self._sid])
                self.eid = np.vstack([self.eid, self._eid])

                self.pressure = np.hstack([self.pressure, self._pressure])
                # TODO: need to handle comments
            else:
                self.sid = np.array(self._sid, dtype='int32')
                self.eid = np.array(self._eid, dtype='int32')
                self.pressure = np.array(self._pressure, dtype='float64')

            self._sid = []
            self._eid = []
            self._pressure = []
            self.is_current = True

    def repr_indent(self, indent=''):
        msg = '%sPLOAD2v:\n' % indent
        msg += '%s  sid = %s\n' % self.sid
        return msg

class PLOAD4v(BaseLoad):
    """
    ``Solid Format``

    Defines a pressure load on a face of a CHEXA, CPENTA, or CTETRA element.

    +--------+-----+-----+----+----+------+------+------+-------+
    |   1    |  2  |  3  |  4 |  5 |  6   |   7  |   8  |   9   |
    +========+=====+=====+====+====+======+======+======+=======+
    | PLOAD4 | SID | EID | P1 | P2 |  P3  |  P4  |  G1  | G3/G4 |
    +--------+-----+-----+----+----+------+------+------+-------+
    |        | CID | N1  | N2 | N3 | SORL | LDIR |      |       |
    +--------+-----+-----+----+----+------+------+------+-------+

    ``Shell Format``

    Defines a pressure load on a face of a CTRIA3, CTRIA6, CTRIAR,
    CQUAD4, CQUAD8, or CQUADR element.

    +--------+-----+-----+----+----+------+------+------+-------+
    |   1    |  2  |  3  |  4 |  5 |  6   |   7  |   8  |   9   |
    +========+=====+=====+====+====+======+======+======+=======+
    | PLOAD4 | SID | EID | P1 | P2 |  P3  |  P4  | THRU | EID2  |
    +--------+-----+-----+----+----+------+------+------+-------+
    |        | CID | N1  | N2 | N3 | SORL | LDIR |      |       |
    +--------+-----+-----+----+----+------+------+------+-------+

    .. warning:: NX does not support SORL and LDIR, MSC does
    """
    card_name = 'PLOAD4'

    def __init__(self, model):
        BaseLoad.__init__(self, model)
        self.eid = np.array([], dtype='int32')
        self.cid = np.array([], dtype='int32')
        self.pressures = np.array([], dtype='float64')
        self.nvector = np.array([], dtype='float64')
        self.g1 = np.array([], dtype='int32')
        self.g34 = np.array([], dtype='int32')
        self.surf_or_line = np.array([], dtype='|U8')
        self.line_load_dir = np.array([], dtype='|U8')

        self._eid = []
        self._cid = []
        self._pressures = []
        self._g1 = []
        self._g34 = []
        self._nvector = []
        self._surf_or_line = []
        self._line_load_dir = []
        self.comment = defaultdict(str)

    def add(self, sid, eid, pressures, g1, g34,
            cid=0, nvector=None, surf_or_line='SURF',
            line_load_dir='NORM', comment=''):
        """
        Creates a PLOAD4 card

        Parameters
        ----------
        sid : int
            the load id
        eids : List[int, ...]
            shells : the range of element ids; must be sequential
            solids : must be length 1
        pressures : List[float, float, float, float]
            tri : must be length 4 (the last value should be the same as the 0th value)
            quad : must be length 4
        g1 : int/None
            only used for solid elements
        g34 : int / None
            only used for solid elements
        cid : int; default=0
            the coordinate system for ???
        nvector : (3, ) float ndarray
           blank : load acts normal to the face
           the local pressure vector
        surf_or_line : str; default='SURF'
           SURF : surface load
           LINE : line load    (only defined for QUADR, TRIAR)
           not supported
        line_load_dir : str; default='NORM'
           direction of the line load (see surf_or_line); {X, Y, Z, TANG, NORM}
           not supported
        comment : str; default=''
            a comment for the card

        TODO: fix the way "pressures" works
        """
        if comment:
            self.comment[len(self)] = _format_comment(comment)
        self.is_current = False
        self._sid.append(sid)
        self._eid.append(eid)
        self._cid.append(cid)
        self._pressures.append(pressures)
        self._g1.append(g1)
        self._g34.append(g34)
        self._nvector.append(nvector)
        self._surf_or_line.append(surf_or_line)
        self._line_load_dir.append(line_load_dir)

    def add_card(self, card, comment=''):
        """
        Adds a PLOAD4 card from ``BDF.add_card(...)``

        Parameters
        ----------
        card : BDFCard()
            a BDFCard object
        comment : str; default=''
            a comment for the card
        """
        sid = integer(card, 1, 'sid')
        eid = integer(card, 2, 'eid')
        p1 = double_or_blank(card, 3, 'p1', 0.0)
        pressures = [
            p1,
            double_or_blank(card, 4, 'p2', p1),
            double_or_blank(card, 5, 'p3', p1),
            double_or_blank(card, 6, 'p4', p1)]

        eids = [eid]
        g1_thru = integer_string_or_blank(card, 7, 'g1/THRU')
        if g1_thru == 'THRU' and integer_or_blank(card, 8, 'eid2'):
            # alternate form
            eid2 = integer(card, 8, 'eid2')
            if eid2:
                eids = list(np.unique(
                    expand_thru([eid, 'THRU', eid2], set_fields=False, sort_fields=False)
                ))
            g1 = 0
            g34 = 0
        else:
            # standard form
            eids = [eid]
            g1 = integer_or_blank(card, 7, 'g1', -1)
            g34 = integer_or_blank(card, 8, 'g34', -1)

        cid = integer_or_blank(card, 9, 'cid', 0)
        nvector = np.array([double_or_blank(card, 10, 'N1', 0.0),
                            double_or_blank(card, 11, 'N2', 0.0),
                            double_or_blank(card, 12, 'N3', 0.0)])
        surf_or_line = string_or_blank(card, 13, 'sorl', 'SURF')
        line_load_dir = string_or_blank(card, 14, 'ldir', 'NORM')
        assert len(card) <= 15, 'len(PLOAD4 card) = %i\ncard=%s' % (len(card), card)
        for eid in eids:
            self.add(sid, eid, pressures, g1, g34, cid, nvector,
                     surf_or_line, line_load_dir, comment=comment)
            comment = ''

    @property
    def is_solid(self):
        return np.where(self.g1 == 0)[0]

    def write_card(self, size=8, is_double=False, bdf_file=None):
        assert bdf_file is not None
        self.make_current()
        msg = ''
        for sid, eid, cid, g1, g34, pressures, nvector, surf_or_line, line_load_dir in zip(
            self.sid, self.eid, self.cid, self.g1, self.g34, self.pressures,
            self.nvector, self.surf_or_line, self.line_load_dir):
            p1 = pressures[0]
            p2 = set_blank_if_default(pressures[1], p1)
            p3 = set_blank_if_default(pressures[2], p1)
            p4 = set_blank_if_default(pressures[3], p1)
            list_fields = ['PLOAD4', sid, eid, pressures[0], p2, p3, p4]

            if g1 != -1:
                # is it a SOLID element
                list_fields += [g1, g34]
            else:
                list_fields.extend([eid, None])
                #if len(eids) > 1:
                    #try:
                        #list_fields.append('THRU')
                        #eidi = eids[-1]
                    #except:
                        #print("g1  = %s" % g1)
                        #print("g34 = %s" % g34)
                        #print("eid = %s" % eid)
                        #raise
                    #list_fields.append(eidi)
                #else:
                    #list_fields += [None, None]

            if cid or np.linalg.norm(nvector) > 0.0:
                n1 = nvector[0]
                n2 = nvector[1]
                n3 = nvector[2]
                list_fields.append(cid)
                list_fields += [n1, n2, n3]
                surf_or_line = surf_or_line
                line_load_dir = line_load_dir
            else:
                list_fields += [None, None, None, None]
                surf_or_line = set_blank_if_default(surf_or_line, 'SURF')
                line_load_dir = set_blank_if_default(line_load_dir, 'NORM')
            list_fields.append(surf_or_line)
            if surf_or_line == 'LINE':
                list_fields.append(line_load_dir)

            msgi = print_card_8(list_fields)
            #msg += self.comment[eid] + msgi.rstrip() + '\n'
            msg += msgi
        bdf_file.write(msg)
        return msg

    def make_current(self):
        """creates an array of the elements"""
        if not self.is_current:
            if len(self.sid) > 0: # there are already elements in self.eid
                self.sid = np.hstack([self.sid, self._sid])
                self.eid = np.vstack([self.eid, self._eid])
                self.cid = np.hstack([self.cid, self._cid])

                self.pressures = np.hstack([self.pressures, self._pressures])
                self.g1 = np.hstack([self.g1, self._g1])
                self.g34 = np.hstack([self.g34, self._g34])
                self.nvector = np.hstack([self.nvector, self._nvector])
                self.surf_or_line = np.hstack([self.surf_or_line, self._surf_or_line])
                self.line_load_dir = np.hstack([self.line_load_dir, self._line_load_dir])

                # TODO: need to handle comments
            else:
                self.sid = np.array(self._sid, dtype='int32')
                self.eid = np.array(self._eid, dtype='int32')
                self.cid = np.array(self._cid, dtype='int32')
                self.pressures = np.array(self._pressures, dtype='float64')
                self.g1 = np.array(self._g1, dtype='int32')
                self.g34 = np.array(self._g34, dtype='int32')
                self.nvector = np.array(self._nvector, dtype='float64')
                self.surf_or_line = np.array(self._surf_or_line, dtype='|U8')
                self.line_load_dir = np.array(self._line_load_dir, dtype='|U8')

            self._sid = []
            self._eid = []
            self._cid = []
            self._pressures = []
            self._g1 = []
            self._g34 = []
            self._nvector = []
            self._surf_or_line = []
            self._line_load_dir = []
            self.is_current = True

    def repr_indent(self, indent=''):
        msg = '%sPLOAD4v:\n' % indent
        msg += '%s  sid = %s\n' % self.sid
        return msg


text:
"""Defines evaluation metrics.
"""
import numpy as np

def mean_reciprocal_rank(labels, scores):
    # TODO docstring
    pos_correct = np.argmax(labels)
    score = scores[pos_correct]
    rank = sum(1 for s in scores if s > score)
    return 1 / (rank + 1)

max_stars_repo_name: mcrisc/lexdecomp

max_issues_repo_name: mcrisc/lexdecomp

max_forks_repo_name: mcrisc/lexdecomp


text:
import numpy as np
from src.featurization import molecular_feature_collection, get_gmm
from GauL.GauLsrc.representation import represent
from GauL.GauLsrc.plots import output_plot
from GauL.GauLsrc.crossDouble import training, write_statistics
from GauL.GauLsrc.results_processing import train_results_to_logfile, test_results_to_logfile
from tensorflow.keras.models import load_model
import pickle

def representation_maker(smiles_dict, save_folder):
    try:
        with open(str(save_folder + "/representation_dict.pickle"), "rb") as f:
            representation_dict = pickle.load(f)
    except FileNotFoundError:
        representation_dict = {}
        gmm, smiles, conformers = get_gmm(save_folder, smiles_dict, "representing")
        representations, bad = represent(smiles, conformers, gmm, save_folder)
        for smile, representation in zip(smiles, representations):
            representation_dict[smile] = representation
        with open(str(save_folder + "/representation_dict.pickle"), "wb") as f:
            pickle.dump(representation_dict, f)

    return representation_dict

def representation_selector(dataframe, smiles_dict, save_folder):
    ch_dict, bp_dict, tc_dict, sg_dict, vap_dict, bp_to_predict, \
        tc_to_predict, sg_to_predict, vap_to_predict = molecular_feature_collection(dataframe, smiles_dict)

    representation_dict = representation_maker(smiles_dict, save_folder)

    bp_train_representations = []
    tc_train_representations = []
    sg_train_representations = []
    vap_train_representations = []

    bp_outputs = []
    tc_outputs = []
    sg_outputs = []
    vap_outputs = []

    bp_smiles_list = []
    tc_smiles_list = []
    sg_smiles_list = []
    vap_smiles_list = []

    bp_test_representations = []
    tc_test_representations = []
    sg_test_representations = []
    vap_test_representations = []

    for bp_smiles in bp_dict:
        bp_train_representations.append(representation_dict[bp_smiles])
        bp_outputs.append(bp_dict[bp_smiles])
        bp_smiles_list.append(bp_smiles)

    bp_outputs = np.asarray(bp_outputs).astype(np.float)

    for tc_smiles in tc_dict:
        tc_train_representations.append(representation_dict[tc_smiles])
        tc_outputs.append(tc_dict[tc_smiles])
        tc_smiles_list.append(tc_smiles)

    tc_outputs = np.asarray(tc_outputs).astype(np.float)

    for sg_smiles in sg_dict:
        sg_train_representations.append(representation_dict[sg_smiles])
        sg_outputs.append(sg_dict[sg_smiles])
        sg_smiles_list.append(sg_smiles)

    sg_outputs = np.asarray(sg_outputs).astype(np.float)

    for vap_smiles in vap_dict:
        vap_train_representations.append(representation_dict[vap_smiles])
        vap_outputs.append(vap_dict[vap_smiles])
        vap_smiles_list.append(vap_smiles)

    vap_outputs = np.asarray(vap_outputs).astype(np.float)

    bp_train_representations = np.stack(bp_train_representations)
    tc_train_representations = np.stack(tc_train_representations)
    sg_train_representations = np.stack(sg_train_representations)
    vap_train_representations = np.stack(vap_train_representations)

    for bp_test_smiles in bp_to_predict:
        bp_test_representations.append(representation_dict[bp_test_smiles])

    for tc_test_smiles in tc_to_predict:
        tc_test_representations.append(representation_dict[tc_test_smiles])

    for sg_test_smiles in sg_to_predict:
        sg_test_representations.append(representation_dict[sg_test_smiles])

    for vap_test_smiles in vap_to_predict:
        vap_test_representations.append(representation_dict[vap_test_smiles])

    bp_test_representations = np.stack(bp_test_representations)
    tc_test_representations = np.stack(tc_test_representations)
    sg_test_representations = np.stack(sg_test_representations)
    vap_test_representations = np.stack(vap_test_representations)

    return bp_train_representations, tc_train_representations, sg_train_representations, vap_train_representations, \
        bp_outputs, tc_outputs, sg_outputs, vap_outputs, \
        bp_test_representations, tc_test_representations, sg_test_representations, vap_test_representations, \
        ch_dict, bp_dict, tc_dict, sg_dict, vap_dict, \
        bp_smiles_list, tc_smiles_list, sg_smiles_list, vap_smiles_list, \
        bp_to_predict, tc_to_predict, sg_to_predict, vap_to_predict, representation_dict

def predict_properties(dataframe, smiles_dict, save_folder):
    bp_train_representations, tc_train_representations, sg_train_representations, vap_train_representations, \
        bp_outputs, tc_outputs, sg_outputs, vap_outputs, \
        bp_test_representations, tc_test_representations, sg_test_representations, vap_test_representations, \
        ch_dict, bp_dict, tc_dict, sg_dict, vap_dict, \
        bp_smiles, tc_smiles, sg_smiles, vap_smiles, \
        bp_to_predict, tc_to_predict, sg_to_predict, vap_to_predict, \
        representation_dict = representation_selector(dataframe, smiles_dict, save_folder)

    bp_save_folder = str(save_folder + "/Boiling Point")
    tc_save_folder = str(save_folder + "/Critical Point")
    sg_save_folder = str(save_folder + "/Density")
    vap_save_folder = str(save_folder + "/Vapor Pressure")

    output_plot(bp_smiles, bp_outputs, "bp", "output_plot", bp_save_folder)
    output_plot(tc_smiles, tc_outputs, "tc", "output_plot", tc_save_folder)
    output_plot(sg_smiles, sg_outputs, "sg", "output_plot", sg_save_folder)
    output_plot(vap_smiles, vap_outputs, "vap", "output_plot", vap_save_folder)

    n_folds = 10  # TODO: Make argument

    folders = ['Fold 1', 'Fold 2', 'Fold 3', 'Fold 4', 'Fold 5', 'Fold 6', 'Fold 7', 'Fold 8', 'Fold 9', 'Fold 10']

    try:
        print("Found pretrained Boiling Point models!")
        bp_models = [load_model(str(bp_save_folder + '/' + folder)) for folder in folders]
        ensemble = np.array([])
        for model in bp_models:
            test_predicted = model.predict(bp_test_representations).reshape(-1)
            test_predicted = np.asarray(test_predicted).astype(np.float)
            if len(ensemble) == 0:
                ensemble = test_predicted
            else:
                ensemble = np.vstack((ensemble, test_predicted))

        ensemble_prediction = np.mean(ensemble, axis=0)
        ensemble_sd = np.std(ensemble, axis=0)

        test_results_to_logfile(bp_to_predict, ensemble_prediction, ensemble_sd, "bp", bp_save_folder)
        for smiles, prediction in zip(bp_to_predict, ensemble_prediction):
            bp_dict[smiles] = prediction
    except OSError:
        bp_info = training(bp_smiles, bp_train_representations, bp_outputs, bp_save_folder, "bp", n_folds)
        bp_results_list = write_statistics(bp_info, "bp", n_folds, bp_save_folder)
        train_results_to_logfile(bp_smiles, bp_outputs, bp_results_list, bp_train_representations,
                                 "bp", bp_save_folder)
        bp_models = [load_model(str(bp_save_folder + '/' + folder)) for folder in folders]
        ensemble = np.array([])
        for model in bp_models:
            test_predicted = model.predict(bp_test_representations).reshape(-1)
            test_predicted = np.asarray(test_predicted).astype(np.float)
            if len(ensemble) == 0:
                ensemble = test_predicted
            else:
                ensemble = np.vstack((ensemble, test_predicted))

        ensemble_prediction = np.mean(ensemble, axis=0)
        ensemble_sd = np.std(ensemble, axis=0)

        test_results_to_logfile(bp_to_predict, ensemble_prediction, ensemble_sd, "bp", bp_save_folder)
        for smiles, prediction in zip(bp_to_predict, ensemble_prediction):
            bp_dict[smiles] = prediction

    try:
        print("Found pretrained Critical Temperature models!")
        tc_models = [load_model(str(tc_save_folder + '/' + folder)) for folder in folders]
        ensemble = np.array([])
        for model in tc_models:
            test_predicted = model.predict(tc_test_representations).reshape(-1)
            test_predicted = np.asarray(test_predicted).astype(np.float)
            if len(ensemble) == 0:
                ensemble = test_predicted
            else:
                ensemble = np.vstack((ensemble, test_predicted))

        ensemble_prediction = np.mean(ensemble, axis=0)
        ensemble_sd = np.std(ensemble, axis=0)

        test_results_to_logfile(tc_to_predict, ensemble_prediction, ensemble_sd, "tc", tc_save_folder)
        for smiles, prediction in zip(tc_to_predict, ensemble_prediction):
            tc_dict[smiles] = prediction
    except OSError:
        tc_info = training(tc_smiles, tc_train_representations, tc_outputs, tc_save_folder, "tc", n_folds)
        tc_results_list = write_statistics(tc_info, "tc", n_folds, tc_save_folder)
        train_results_to_logfile(tc_smiles, tc_outputs, tc_results_list, tc_train_representations,
                                 "tc", tc_save_folder)

        tc_models = [load_model(str(tc_save_folder + '/' + folder)) for folder in folders]
        ensemble = np.array([])
        for model in tc_models:
            test_predicted = model.predict(tc_test_representations).reshape(-1)
            test_predicted = np.asarray(test_predicted).astype(np.float)
            if len(ensemble) == 0:
                ensemble = test_predicted
            else:
                ensemble = np.vstack((ensemble, test_predicted))

        ensemble_prediction = np.mean(ensemble, axis=0)
        ensemble_sd = np.std(ensemble, axis=0)

        test_results_to_logfile(tc_to_predict, ensemble_prediction, ensemble_sd, "tc", tc_save_folder)
        for smiles, prediction in zip(tc_to_predict, ensemble_prediction):
            tc_dict[smiles] = prediction

    try:
        print("Found pretrained Density models!")

        sg_models = [load_model(str(sg_save_folder + '/' + folder)) for folder in folders]
        ensemble = np.array([])
        for model in sg_models:
            test_predicted = model.predict(sg_test_representations).reshape(-1)
            test_predicted = np.asarray(test_predicted).astype(np.float)
            if len(ensemble) == 0:
                ensemble = test_predicted
            else:
                ensemble = np.vstack((ensemble, test_predicted))

        ensemble_prediction = np.mean(ensemble, axis=0)
        ensemble_sd = np.std(ensemble, axis=0)

        test_results_to_logfile(sg_to_predict, ensemble_prediction, ensemble_sd, "sg", sg_save_folder)
        for smiles, prediction in zip(sg_to_predict, ensemble_prediction):
            sg_dict[smiles] = prediction
    except OSError:

        sg_info = training(sg_smiles, sg_train_representations, sg_outputs, sg_save_folder, "sg", n_folds)
        sg_results_list = write_statistics(sg_info, "sg", n_folds, sg_save_folder)
        train_results_to_logfile(sg_smiles, sg_outputs, sg_results_list, sg_train_representations,
                                 "sg", sg_save_folder)

        sg_models = [load_model(str(sg_save_folder + '/' + folder)) for folder in folders]
        ensemble = np.array([])
        for model in sg_models:
            test_predicted = model.predict(sg_test_representations).reshape(-1)
            test_predicted = np.asarray(test_predicted).astype(np.float)
            if len(ensemble) == 0:
                ensemble = test_predicted
            else:
                ensemble = np.vstack((ensemble, test_predicted))

        ensemble_prediction = np.mean(ensemble, axis=0)
        ensemble_sd = np.std(ensemble, axis=0)

        test_results_to_logfile(sg_to_predict, ensemble_prediction, ensemble_sd, "sg", sg_save_folder)
        for smiles, prediction in zip(sg_to_predict, ensemble_prediction):
            sg_dict[smiles] = prediction

    try:
        print("Found pretrained Vapor Pressure models!")
        vap_models = [load_model(str(vap_save_folder + '/' + folder)) for folder in folders]

        ensemble = np.array([])
        for model in vap_models:
            test_predicted = model.predict(vap_test_representations).reshape(-1)
            test_predicted = np.asarray(test_predicted).astype(np.float)
            if len(ensemble) == 0:
                ensemble = test_predicted
            else:
                ensemble = np.vstack((ensemble, test_predicted))

        ensemble_prediction = np.mean(ensemble, axis=0)
        ensemble_sd = np.std(ensemble, axis=0)

        test_results_to_logfile(vap_to_predict, ensemble_prediction, ensemble_sd, "vap", vap_save_folder)
        for smiles, prediction in zip(vap_to_predict, ensemble_prediction):
            vap_dict[smiles] = prediction

    except OSError:
        vap_info = training(vap_smiles, vap_train_representations, vap_outputs, vap_save_folder, "vap", n_folds)
        vap_results_list = write_statistics(vap_info, "vap", n_folds, vap_save_folder)
        train_results_to_logfile(vap_smiles, vap_outputs, vap_results_list, vap_train_representations,
                                 "vap", vap_save_folder)

        vap_models = [load_model(str(vap_save_folder + '/' + folder)) for folder in folders]

        ensemble = np.array([])
        for model in vap_models:
            test_predicted = model.predict(vap_test_representations).reshape(-1)
            test_predicted = np.asarray(test_predicted).astype(np.float)
            if len(ensemble) == 0:
                ensemble = test_predicted
            else:
                ensemble = np.vstack((ensemble, test_predicted))

        ensemble_prediction = np.mean(ensemble, axis=0)
        ensemble_sd = np.std(ensemble, axis=0)

        test_results_to_logfile(vap_to_predict, ensemble_prediction, ensemble_sd, "vap", vap_save_folder)
        for smiles, prediction in zip(vap_to_predict, ensemble_prediction):
            vap_dict[smiles] = prediction

    return ch_dict, bp_dict, tc_dict, sg_dict, vap_dict, representation_dict

max_stars_repo_name: mrodobbe/naphtha-mixtures

max_issues_repo_name: mrodobbe/naphtha-mixtures

max_forks_repo_name: mrodobbe/naphtha-mixtures