<jupyter_start><jupyter_text>Tutorial Part 4: Importing and Exporting Arrays<jupyter_code>import isopy
import io
import ipywidgets as widgets
import pandas as pd
import numpy as np<jupyter_output><empty_output><jupyter_text>There are a number of options for reading and writing isopy arrays to the disk. Isopy comes with functions for working with [CSV](CSV-files) files and [excel](Excel-files) files. Isopy also has a function for importing data from *exp* files produced by Neptune and Triton instruments.You can also use I/O functions provided by [numpy](Numpy-I/O-functions) and [pandas](Pandas-I/O-functions) with relative ease.  CSV files<jupyter_code>#Create an array of random values to work with
array = isopy.random(5, keys='ru pd cd'.split(), seed=46)
array<jupyter_output><empty_output><jupyter_text>The ``read_csv()`` and ``write_csv()`` functions included with isopy allow you to load and save arrays as csv files. By default the functions assume that the first row, that is not a comment, contains the column keys and that subsequent rows contain the values.**Note** These functions do not preserve the column data type. To preserve these use the builtin numpy IO functions.<jupyter_code>isopy.write_csv('csv_file.csv', array, comments='A randomly created array')
with open('csv_file.csv') as file: 
    print(file.read())<jupyter_output>#A randomly created array,,
Ru,Pd,Cd
-0.5029158999360099,-1.4294668995364466,0.3161798146820059
1.2178904370877544,-0.023272781996066142,-0.35797332652478464
-0.27111940695477094,-0.44779253386270806,-0.9287436068353231
0.7301833239723954,0.08807514133629571,-1.0528274607881316
-1.1693055169964495,0.5252955930066644,0.23593193513123287<jupyter_text>The ``read_csv()`` functions returns a dictionary that can easily be converted to an array.<jupyter_code>data = isopy.read_csv('csv_file.csv')
data
isopy.array(data)<jupyter_output><empty_output><jupyter_text>You can read/write data where the column keys are stored as the first entry in each row by setting the ``keys_in`` argument to ``'r'``<jupyter_code>isopy.write_csv('csv_file2.csv', array, keys_in ='r', comments='A randomly created array')
with open('csv_file2.csv') as file: 
    print(file.read())
data = isopy.read_csv('csv_file2.csv', keys_in='r')
isopy.array(data)<jupyter_output><empty_output><jupyter_text>You can also use these functions to save normal numpy arrays by specifying ``keys_in=None``<jupyter_code>array = isopy.random(10).reshape((-1, 5))
array
isopy.write_csv('csv_file3.csv', array, keys_in=None, comments='A randomly created array')
with open('csv_file3.csv') as file: 
    print(file.read())
data = isopy.read_csv('csv_file3.csv', keys_in=None)
data<jupyter_output><empty_output><jupyter_text>Excel files<jupyter_code>#Create an array of random values to work with
array = isopy.random(5, keys='<KEY>'.split(), seed=46)
array<jupyter_output><empty_output><jupyter_text>The ``read_xlsx()`` and ``write_xlsx()`` functions reads/writes arrays to excel files and work much like CSV functions described above.<jupyter_code>isopy.write_xlsx('excel_file.xlsx', array) # Default sheet name is 'sheet1'<jupyter_output><empty_output><jupyter_text>You can specify the sheet name by passing the array as a keyword argument. You can also add data to exciting excel files by setting ``append=True``<jupyter_code>array2 = isopy.random(3, keys='rh ag'.split(), seed=46)
isopy.write_xlsx('excel_file.xlsx', appended_sheet=array2, append=True)<jupyter_output><empty_output><jupyter_text>Unless you specify a sheetname ``read_xlsx()`` will return a dictionary containing all the sheets in the file.<jupyter_code>isopy.read_xlsx('excel_file.xlsx').keys()<jupyter_output><empty_output><jupyter_text>You can specify the sheet you want to load either by name of by index.<jupyter_code>isopy.array( isopy.read_xlsx('excel_file.xlsx', sheetname=0) )
isopy.array( isopy.read_xlsx('excel_file.xlsx', sheetname='appended_sheet') )<jupyter_output><empty_output><jupyter_text>Neptune/Triton export files ``read_exp()`` can be used to read the data contained with in the Neptune and Triton export files.<jupyter_code>data = isopy.read_exp('neptune_file.exp')
data.info # Dictonary containing the meta data included in the file<jupyter_output><empty_output><jupyter_text>The ``.measurements`` attribute is a dictionary containing isopy arrays of the individual measurements for each line. For static measurements the line number defaults to 1.<jupyter_code>data.measurements[1]<jupyter_output><empty_output><jupyter_text>You can copy only the part of the array containing isotope data using the ``flavour`` key filter<jupyter_code>data.measurements[1].copy(flavour_eq='isotope')<jupyter_output><empty_output><jupyter_text>In memory files<jupyter_code>#Create an array of random values to work with
array = isopy.random(5, keys='ru pd cd'.split(), seed=46)
array<jupyter_output><empty_output><jupyter_text>The isopy read/write functions for CSV and excel files also accept file like objects.<jupyter_code>file = io.StringIO()
isopy.write_csv(file, array, comments='This is not a real file')
file.seek(0) # Move to the beggining of the file stream
print( file.read() )<jupyter_output>#This is not a real file,,
Ru,Pd,Cd
-0.5029158999360099,-1.4294668995364466,0.3161798146820059
1.2178904370877544,-0.023272781996066142,-0.35797332652478464
-0.27111940695477094,-0.44779253386270806,-0.9287436068353231
0.7301833239723954,0.08807514133629571,-1.0528274607881316
-1.1693055169964495,0.5252955930066644,0.23593193513123287<jupyter_text>This could be used for example with the ``FileUpload()`` widget which loads files from the local disk into the memory of the jupyter session.<jupyter_code>uploader = widgets.FileUpload()
uploader
#Assuming you uploaded CSV files
for i in range(len(uploader.value)):
    file = uploader.value[i].content.tobytes()
    print( isopy.array( isopy.read_csv(file) ) )<jupyter_output><empty_output><jupyter_text>Numpy I/O functions You can also use the I/O functions included with [numpy](https://numpy.org/doc/stable/reference/routines.io.html). It is recommended to convert the isopy array into an numpy array first.<jupyter_code>array = isopy.random(5, keys='<KEY>'.split(), seed=46)
np.save('numpy_file.npy', array.to_ndarray())
isopy.array( np.load('numpy_file.npy') )
array.tofile('numpy_file.npy')<jupyter_output><empty_output><jupyter_text>Pandas I/O functions Another option is to use the powerful I/O functions included in [pandas](https://pandas.pydata.org/docs/reference/io.html).<jupyter_code>array = isopy.random(5, keys='<KEY>'.split(), seed=46)
df = pd.DataFrame(array)
df.to_json() #Convert the array to a JSON string<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11247199402539208, 'fraction_numerical': 0.08379387602688573, 'mean_word_length': 7.145985401459854, 'pattern_counts': {'":': 0, '<': 78, '<?xml version=': 0, '>': 78, 'https://': 2, '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}}
{'chain_length': '19', 'dir': 'jupyter-structured-clean-dedup', 'id': '173792', 'license': 'MIT', 'n_tokens_mistral': 2636, 'n_tokens_neox': 2310, 'n_words': 598, 'path': 'docs/tutorials/part4_import_export.ipynb', 'repo_name': 'mattias-ek/isopy', 'size': '6704'}