<commit_before>from math import *
from matplotlib import pyplot as plt
from matplotlib import colors as col
import numpy as np
import sys,os

def cart_comp(x,y):
    """
    A basic comparison function to insure that layers are selected in terms of larger positive to larger negative
    """
    return int(y-x)

def parse_tracer_file(files,overwrite_old_data=False):
    """
    takes iSALE tracer file and appends the information at time 0 and final time and smashes it into a text file with tracer numbers and in a readable format this is then used to generate the final results file which can be used to plot by calling mk_final_results
    @param: file_name -> name of the tracer file 
    @writes: TracerResults/tracer-out.txt -> parsed output file
    """
    out_dict = {}
    for filename in files:

        out_str = ""

        tracer_file_name = filename.split('/')
        tracer_num = tracer_file_name[-1].strip('tracer-').strip('.txt')
        tracer_file = open(filename)

        header = tracer_file.readline().split()
        header = [item for item in header if item != '(m)']
        header_out = tracer_num + '\n'
        for item in header[0:-1]:
            header_out += item + '\t'
        try: header_out += header[3][0:3] + '\t' + header[3][3:6]
        except IndexError: header_out += header[-1]
        out_str += header_out + '\n'

        initial_values = tracer_file.readline().split()
        initial_values_out = 'initial values\t'
        for value in initial_values:
            initial_values_out += value + '\t'
        out_str += initial_values_out + '\n'

        if len(initial_values) > 3:
            max_v1 = float(initial_values[3])
            max_v2 = float(initial_values[4])
            for i,line in enumerate(tracer_file):
                values = line.split()
                try: 
                    if values[3].isalpha(): print("extra header in file: " + filename + " on line: " + str(i)); continue
                except IndexError: continue
                if float(values[3]) > max_v1:
                    max_v1 = float(values[3])
                if float(values[4]) > max_v2:
                    max_v2 = float(values[4])
        else:
            max_v1 = ""
            max_v2 = ""
            for line in tracer_file:
                values = line.split()

        final_values_out = 'final/max values\t'
        for value in values[:3]:
            final_values_out += value + '\t'
        final_values_out += str(max_v1) + '\t' + str(max_v2)
        out_str += final_values_out + '\n'

        if float(initial_values[2]) not in out_dict.keys(): out_dict[float(initial_values[2])] = out_str
        else: out_dict[float(initial_values[2])] += out_str

    keys = out_dict.keys()
    keys.sort(cmp=cart_comp)

    if not os.path.exists("TracerResults/"):
        os.makedirs("TracerResults")
    if overwrite_old_data: output_file = open("TracerResults/tracer-parsed.txt", 'w+')
    else: output_file = open("TracerResults/tracer-parsed.txt", 'a+') #+ tracer_num + "-out.txt", 'a+')

    for key in keys:
        output_file.write(out_dict[key] + "new layer\n")

    tracer_file.close()
    output_file.close()

def mk_final_results(directory="TracerResults", filename='tracer-parsed.txt', overwrite_old_data=False):
    """
    takes parsed tracer file from iSALE and outputs a file containing info such as dip and distance between tracers as well as any saved iSALE plot variables and positions for the first tracer.
    @param: directory -> directory to look for input file
    @param: filename -> name of parsed iSALE tracer file
    @writes: TracerResults/tracer-out.txt containing info meantioned above
    """
    if not directory.endswith('/'):
        directory += '/'
    tracer_out_file = open(directory + filename, 'r')
    if overwrite_old_data: final_results_file = open(directory + 'tracer-out.txt', 'w+')
    else: final_results_file = open(directory + 'tracer-out.txt', 'a+')
    current_tracer = tracer_out_file.readline().strip('\n')
    header = tracer_out_file.readline().split()
    CT_inital_values = map(float,tracer_out_file.readline().split()[2:])
    CT_final_values = map(float,tracer_out_file.readline().split()[2:])
    newlayer = False
    while current_tracer:
        current_tracer_str = ''
        next_tracer = tracer_out_file.readline().strip('\n')
        if next_tracer == 'new layer': next_tracer = tracer_out_file.readline().strip('\n'); newlayer=True
        NT_header = tracer_out_file.readline().split()
        NT_initial_values = map(float,tracer_out_file.readline().split()[2:])
        NT_final_values = map(float,tracer_out_file.readline().split()[2:])
        if len(CT_inital_values) < 3 or len(CT_final_values) < 3 or len(NT_initial_values) < 3 or len(NT_final_values) < 3:
            break
        current_tracer_str += current_tracer + "-" + next_tracer + '\n'
        try: current_tracer_str += 'Time\tXmark\tYmark\tDis\tDip\tAng\t' + header[3] + '\t' + header[4] + '\n'
        except IndexError: current_tracer_str += 'Time\tXmark\tYmark\tDis\tDip\tAng\t' + '\n'
        for CT_values, NT_values in zip((CT_inital_values,CT_final_values), (NT_initial_values,NT_final_values)):
            if CT_values[0] != NT_values[0]:
                break
            CT_x = float(CT_values[1])
            CT_y = float(CT_values[2])
            NT_x = float(NT_values[1])
            NT_y = float(NT_values[2])
            a = (NT_x - CT_x)
            b = (NT_y - CT_y)
            a_d,b_d = abs(a),abs(b)
            c = sqrt(a**2 + b**2)
            if a == 0: theta,theta_d = pi/2,pi/2
            else:
                theta = acos((c**2 + a**2 - b**2)/(2*a*c))*(180/pi)
                theta_d = acos((c**2 + a_d**2 - b_d**2)/(2*a_d*c))*(180/pi)
            if b < 0: theta = -theta
            theta = theta
            if newlayer: c,theta,d_theta = 'None','None','None'; newlayer = False
            try: current_tracer_str += str(CT_values[0]) + '\t' + str(CT_values[1]) + '\t' + str(CT_values[2]) + '\t' + str(c) + '\t' + str(theta_d) + '\t' + str(theta) + '\t' + str(CT_values[3]) + '\t' + str(CT_values[4]) + '\n'
            except IndexError: current_tracer_str += str(CT_values[0]) + '\t' + str(CT_values[1]) + '\t' + str(CT_values[2]) + '\t' + str(c) + '\t' + str(theta_d) + '\t' + str(theta) + '\n'
        final_results_file.write(current_tracer_str)
        current_tracer = next_tracer
        CT_inital_values = NT_initial_values
        CT_final_values = NT_final_values
    tracer_out_file.close()
    final_results_file.close()

def read_tracer_out_file(filepath):
    """
    A utility function that reads in tracer out formated files into a dictionary of layers determined by initial y position each of which contains it's own dictionary of either final or initial values.
    @param: filepath -> the path to the tracer-out file to read in
    """
    tracer_out_file = open(filepath, 'r')
    layers = {}
    line,l = 'mary had a little lamb',0
    while line:
        l += 1
        values = line.split()
        if values[0] == 'Time':
            initial = tracer_out_file.readline().split()
            final = tracer_out_file.readline().split()
            if initial[2] not in layers.keys():
                layers[initial[2]] = {'initial':{}, 'final':{}}
            if ('Xmark_short' not in layers[initial[2]]['initial']):
                layers[initial[2]]['initial']['Xmark_short'] = []
            if ('Ymark_short' not in layers[initial[2]]['initial']):
                layers[initial[2]]['initial']['Ymark_short'] = []
            if ('Xmark_short' not in layers[initial[2]]['final']):
                layers[initial[2]]['final']['Xmark_short'] = []
            if ('Ymark_short' not in layers[initial[2]]['final']):
                layers[initial[2]]['final']['Ymark_short'] = []
            for i in range(len(values)):
                if values[i] not in layers[initial[2]]['initial'].keys():
                    layers[initial[2]]['initial'][values[i]] = []
                if values[i] not in layers[initial[2]]['final'].keys():
                    layers[initial[2]]['final'][values[i]] = []
                if initial[i] == 'None' or final[i] == 'None': continue
                layers[initial[2]]['initial'][values[i]].append(initial[i])
                layers[initial[2]]['final'][values[i]].append(final[i])
                if values[i] == 'Xmark' and 'None' not in initial and 'None' not in final:
                    layers[initial[2]]['initial']['Xmark_short'].append(initial[i])
                    layers[initial[2]]['final']['Xmark_short'].append(final[i])
                elif values[i] == 'Ymark' and 'None' not in initial and 'None' not in final:
                    layers[initial[2]]['initial']['Ymark_short'].append(initial[i])
                    layers[initial[2]]['final']['Ymark_short'].append(final[i])
        line = tracer_out_file.readline()
    return layers

def plot_tracer_data(v,layers_to_plot="All",filepath="TracerResults/tracer-out.txt",title='My_Plot',initial_or_final="fff",size=30,colormap='gist_rainbow',save_plot=False, preform_regression=False, degree_regression=1,display_max_min=False):
    layers = read_tracer_out_file(filepath)
    try:
        legend = []
        x_tot,y_tot,z_tot = [],[],[]
        keys = map(float, layers.keys())
        keys.sort(cmp=cart_comp)
        keys = map(str, keys)
        x_max,x_min,y_max,y_min,z_max,z_min = 0.,1e9,0.,1e9,0.,1e9
        if preform_regression: all_x,all_y = [],[]
        if layers_to_plot=="All": layers_to_plot=range(len(keys))
        if ('Xmark' or 'Ymark' in v):
            if ('Dip' or 'Ang' or 'Dis' in v):
                f = lambda p: p + '_short' if (p == 'Xmark' or p == 'Ymark') else p
                v = map(f,v)
        try: v1,v2,v3 = v
        except ValueError: v1,v2 = v
        x_time,y_time,z_time = None,None,None
        if initial_or_final[0] == 'f': x_time = 'final'
        elif initial_or_final[0] == 'i': x_time = 'initial'
        if initial_or_final[1] == 'f': y_time = 'final'
        elif initial_or_final[1] == 'i': y_time = 'initial'
        if len(initial_or_final) > 2 and len(v) > 2 and initial_or_final[2] == 'f': z_time = 'final'
        elif len(initial_or_final) > 2 and len(v) > 2 and initial_or_final[2] == 'i': z_time = 'initial'
        if not x_time and (not y_time or not z_time):
            print(initial_or_final + " is not a valid time input options are [ff,if,fi,ii,iii,ifi,iff,fii,ffi,fif,fff]")
            return
        for n in layers_to_plot:<commit_msg>tracer_interpreter.py: fixed problem with not flagging endpoints in some angle data<commit_after>from math import *
from matplotlib import pyplot as plt
from matplotlib import colors as col
import numpy as np
import sys,os

def cart_comp(x,y):
    """
    A basic comparison function to insure that layers are selected in terms of larger positive to larger negative
    """
    return int(y-x)

def parse_tracer_file(files,overwrite_old_data=False):
    """
    takes iSALE tracer file and appends the information at time 0 and final time and smashes it into a text file with tracer numbers and in a readable format this is then used to generate the final results file which can be used to plot by calling mk_final_results
    @param: file_name -> name of the tracer file 
    @writes: TracerResults/tracer-out.txt -> parsed output file
    """
    out_dict = {}
    for filename in files:

        out_str = ""

        tracer_file_name = filename.split('/')
        tracer_num = tracer_file_name[-1].strip('tracer-').strip('.txt')
        tracer_file = open(filename)

        header = tracer_file.readline().split()
        header = [item for item in header if item != '(m)']
        header_out = tracer_num + '\n'
        for item in header[0:-1]:
            header_out += item + '\t'
        try: header_out += header[3][0:3] + '\t' + header[3][3:6]
        except IndexError: header_out += header[-1]
        out_str += header_out + '\n'

        initial_values = tracer_file.readline().split()
        initial_values_out = 'initial values\t'
        for value in initial_values:
            initial_values_out += value + '\t'
        out_str += initial_values_out + '\n'

        if len(initial_values) > 3:
            max_v1 = float(initial_values[3])
            max_v2 = float(initial_values[4])
            for i,line in enumerate(tracer_file):
                values = line.split()
                try: 
                    if values[3].isalpha(): print("extra header in file: " + filename + " on line: " + str(i)); continue
                except IndexError: continue
                if float(values[3]) > max_v1:
                    max_v1 = float(values[3])
                if float(values[4]) > max_v2:
                    max_v2 = float(values[4])
        else:
            max_v1 = ""
            max_v2 = ""
            for line in tracer_file:
                values = line.split()

        final_values_out = 'final/max values\t'
        for value in values[:3]:
            final_values_out += value + '\t'
        final_values_out += str(max_v1) + '\t' + str(max_v2)
        out_str += final_values_out + '\n'

        if float(initial_values[2]) not in out_dict.keys(): out_dict[float(initial_values[2])] = out_str
        else: out_dict[float(initial_values[2])] += out_str

    keys = out_dict.keys()
    keys.sort(cmp=cart_comp)

    if not os.path.exists("TracerResults/"):
        os.makedirs("TracerResults")
    if overwrite_old_data: output_file = open("TracerResults/tracer-parsed.txt", 'w+')
    else: output_file = open("TracerResults/tracer-parsed.txt", 'a+') #+ tracer_num + "-out.txt", 'a+')

    for key in keys:
        output_file.write(out_dict[key] + "new layer\n")

    tracer_file.close()
    output_file.close()

def mk_final_results(directory="TracerResults", filename='tracer-parsed.txt', overwrite_old_data=False):
    """
    takes parsed tracer file from iSALE and outputs a file containing info such as dip and distance between tracers as well as any saved iSALE plot variables and positions for the first tracer.
    @param: directory -> directory to look for input file
    @param: filename -> name of parsed iSALE tracer file
    @writes: TracerResults/tracer-out.txt containing info meantioned above
    """
    if not directory.endswith('/'):
        directory += '/'
    tracer_out_file = open(directory + filename, 'r')
    if overwrite_old_data: final_results_file = open(directory + 'tracer-out.txt', 'w+')
    else: final_results_file = open(directory + 'tracer-out.txt', 'a+')
    current_tracer = tracer_out_file.readline().strip('\n')
    header = tracer_out_file.readline().split()
    CT_inital_values = map(float,tracer_out_file.readline().split()[2:])
    CT_final_values = map(float,tracer_out_file.readline().split()[2:])
    newlayer = False
    while current_tracer:
        current_tracer_str = ''
        next_tracer = tracer_out_file.readline().strip('\n')
        if next_tracer == 'new layer': next_tracer = tracer_out_file.readline().strip('\n'); newlayer=True
        NT_header = tracer_out_file.readline().split()
        NT_initial_values = map(float,tracer_out_file.readline().split()[2:])
        NT_final_values = map(float,tracer_out_file.readline().split()[2:])
        if len(CT_inital_values) < 3 or len(CT_final_values) < 3 or len(NT_initial_values) < 3 or len(NT_final_values) < 3:
            break
        current_tracer_str += current_tracer + "-" + next_tracer + '\n'
        try: current_tracer_str += 'Time\tXmark\tYmark\tDis\tDip\tAng\t' + header[3] + '\t' + header[4] + '\n'
        except IndexError: current_tracer_str += 'Time\tXmark\tYmark\tDis\tDip\tAng\t' + '\n'
        for CT_values, NT_values in zip((CT_inital_values,CT_final_values), (NT_initial_values,NT_final_values)):
            if CT_values[0] != NT_values[0]:
                break
            CT_x = float(CT_values[1])
            CT_y = float(CT_values[2])
            NT_x = float(NT_values[1])
            NT_y = float(NT_values[2])
            a = (NT_x - CT_x)
            b = (NT_y - CT_y)
            a_d,b_d = abs(a),abs(b)
            c = sqrt(a**2 + b**2)
            if a == 0: theta,theta_d = pi/2,pi/2
            else:
                theta = acos((c**2 + a**2 - b**2)/(2*a*c))*(180/pi)
                theta_d = acos((c**2 + a_d**2 - b_d**2)/(2*a_d*c))*(180/pi)
            if b < 0: theta = -theta
            theta = theta
            if newlayer: c,theta,theta_d = 'None','None','None'; newlayer = False
            try: current_tracer_str += str(CT_values[0]) + '\t' + str(CT_values[1]) + '\t' + str(CT_values[2]) + '\t' + str(c) + '\t' + str(theta_d) + '\t' + str(theta) + '\t' + str(CT_values[3]) + '\t' + str(CT_values[4]) + '\n'
            except IndexError: current_tracer_str += str(CT_values[0]) + '\t' + str(CT_values[1]) + '\t' + str(CT_values[2]) + '\t' + str(c) + '\t' + str(theta_d) + '\t' + str(theta) + '\n'
        final_results_file.write(current_tracer_str)
        current_tracer = next_tracer
        CT_inital_values = NT_initial_values
        CT_final_values = NT_final_values
    tracer_out_file.close()
    final_results_file.close()

def read_tracer_out_file(filepath):
    """
    A utility function that reads in tracer out formated files into a dictionary of layers determined by initial y position each of which contains it's own dictionary of either final or initial values.
    @param: filepath -> the path to the tracer-out file to read in
    """
    tracer_out_file = open(filepath, 'r')
    layers = {}
    line,l = 'mary had a little lamb',0
    while line:
        values = line.split()
        if values[0] == 'Time':
            initial = tracer_out_file.readline().split()
            final = tracer_out_file.readline().split()
            if initial[2] not in layers.keys():
                layers[initial[2]] = {'initial':{}, 'final':{}}
            if ('Xmark_short' not in layers[initial[2]]['initial']):
                layers[initial[2]]['initial']['Xmark_short'] = []
            if ('Ymark_short' not in layers[initial[2]]['initial']):
                layers[initial[2]]['initial']['Ymark_short'] = []
            if ('Xmark_short' not in layers[initial[2]]['final']):
                layers[initial[2]]['final']['Xmark_short'] = []
            if ('Ymark_short' not in layers[initial[2]]['final']):
                layers[initial[2]]['final']['Ymark_short'] = []
            for i in range(len(values)):
                if values[i] not in layers[initial[2]]['initial'].keys():
                    layers[initial[2]]['initial'][values[i]] = []
                if values[i] not in layers[initial[2]]['final'].keys():
                    layers[initial[2]]['final'][values[i]] = []
                if initial[i] == 'None' or final[i] == 'None': continue
                layers[initial[2]]['initial'][values[i]].append(initial[i])
                layers[initial[2]]['final'][values[i]].append(final[i])
                if values[i] == 'Xmark' and 'None' not in initial and 'None' not in final:
                    layers[initial[2]]['initial']['Xmark_short'].append(initial[i])
                    layers[initial[2]]['final']['Xmark_short'].append(final[i])
                elif values[i] == 'Ymark' and 'None' not in initial and 'None' not in final:
                    layers[initial[2]]['initial']['Ymark_short'].append(initial[i])
                    layers[initial[2]]['final']['Ymark_short'].append(final[i])
        line = tracer_out_file.readline()
        l += 1
    return layers

def plot_tracer_data(v,layers_to_plot="All",filepath="TracerResults/tracer-out.txt",title='My_Plot',initial_or_final="fff",size=30,colormap='gist_rainbow',save_plot=False, preform_regression=False, degree_regression=1,display_max_min=False):
    layers = read_tracer_out_file(filepath)
    try:
        legend = []
        x_tot,y_tot,z_tot = [],[],[]
        keys = map(float, layers.keys())
        keys.sort(cmp=cart_comp)
        keys = map(str, keys)
        x_max,x_min,y_max,y_min,z_max,z_min = 0.,1e9,0.,1e9,0.,1e9
        if preform_regression: all_x,all_y = [],[]
        if layers_to_plot=="All": layers_to_plot=range(len(keys))
        if ('Xmark' in v or 'Ymark' in v) and ('Dip' in v or 'Ang' in v or 'Dis' in v):
            f = lambda p: p + '_short' if (p == 'Xmark' or p == 'Ymark') else p
            v = map(f,v)
        try: v1,v2,v3 = v
        except ValueError: v1,v2 = v
        x_time,y_time,z_time = None,None,None
        if initial_or_final[0] == 'f': x_time = 'final'
        elif initial_or_final[0] == 'i': x_time = 'initial'
        if initial_or_final[1] == 'f': y_time = 'final'
        elif initial_or_final[1] == 'i': y_time = 'initial'
        if len(initial_or_final) > 2 and len(v) > 2 and initial_or_final[2] == 'f': z_time = 'final'
        elif len(initial_or_final) > 2 and len(v) > 2 and initial_or_final[2] == 'i': z_time = 'initial'
        if not x_time and (not y_time or not z_time):
            print(initial_or_final + " is not a valid time input options are [ff,if,fi,ii,iii,ifi,iff,fii,ffi,fif,fff]")
            return
        for n in layers_to_plot:
{'fraction_non_alphanumeric': 0.14351742677922935, 'fraction_numerical': 0.011979436872140734, 'mean_word_length': 2.6571231459123834, 'pattern_counts': {'":': 2, '<': 13, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1267996', 'n_tokens_mistral': 7120, 'n_tokens_neox': 6937, 'n_words': 1591}