Skip to content

Commit

Permalink
Made script to generate Big 'Ol CSV of Derivatives; retooled column s…
Browse files Browse the repository at this point in the history
…orting algorithm; bugfixes galore!
  • Loading branch information
nick5435 committed Mar 7, 2017
1 parent 9ea6a18 commit 82a6495
Show file tree
Hide file tree
Showing 21 changed files with 236 additions and 93,878 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
finalData/Water_T-216_P-216_U_with_derivatives.csv filter=lfs diff=lfs merge=lfs -text
14 changes: 7 additions & 7 deletions TODO.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
# TODO:

* [ ] Improve README
* [ ] Whitepaper
* [ ] Figure out how CoolProp generates its data
* [ ] Flesh out whitepaper
* [ ] Path Length should logically correspond to **Energy**, or something like that.
* [ ] **Internal Energy should be able to work as an Input variable** Note: Having same issues as with entropy, might not be feasible as of *2017-02-11*
* [ ] <pre> do \<function> to \<col> where \<condition></pre> (Pandas?) (Flag col?)
* [ ] **Big 'ol CSV of derivatives**
* [ ] CSV Contour Plot w/ column order option
* [ ] Find host for Documentation
* [ ] Cythonize Code - Would increase speed by a lot!

* [x] ~~UML Diagram~~ **2017-01-19 NM**
Expand All @@ -22,3 +16,9 @@
* [x] ~~metadata function for gDG~~ **2017-02-11 NM**
* [x] ~~Wrap everything up for eventual Pip deployment~~ **2017-02-11 NM**
* [x] ~~CSV to Plot function in genericDataGrabber with column order changed~~ **2017-02-11 NM**
* [x] ~~<pre> do \<function> to \<col> where \<condition></pre> (Pandas?) (Flag Col?)~~ _Similar functionality already exists in Pandas_ **2017-02-19 NM**
* [x] ~~Big 'ol CSV of derivatives~~ **2017-03-06 NM**
* [x] ~~Find host for Documentation~~ **2017-02-26 NM**
* [x] ~~**Internal Energy should be able to work as an Input variable** Note: Having same issues as with entropy, might not be feasible as of *2017-02-11*~~ _It works great if we pivot!_ **2017-03-06 NM**
* [x] ~~Figure out how CoolProp generates its data~~ _It uses C++ routines and a rediculously complicated EoS_ **2017-02-26 NM**
* [x] ~~CSV Contour Plot w/ column order option~~ _CSV import and reorder column functionality done, plotting is left as an exercise_ **2017-03-06 NM**
91 changes: 63 additions & 28 deletions ThermoPyle/ThermoPyle/ThermoPyle.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
.. moduleauthor:: Nick Meyer <nmeyer5435@gmail.com>
"""

import copy
import json
from copy import deepcopy
from itertools import permutations

import arrow
import CoolProp.CoolProp as CP
import matplotlib.pyplot as plt
import numpy as np
Expand All @@ -17,7 +19,6 @@
from cytoolz import get
from mpl_toolkits.mplot3d import Axes3D

import arrow
from typing import Any, Callable, Dict, List, Text, Tuple, TypeVar, Union

T = TypeVar("ThermoFluid")
Expand All @@ -30,7 +31,8 @@
"G": "J/kg",
"U": "J/kg",
"D": "kg/m^3",
"V": "m^3"
"V": "m^3",
"PHASE": "",
}


Expand Down Expand Up @@ -59,7 +61,7 @@ def __init__(self,
var2: str="P",
outvar: str="S",
numPoints: Union[List[int], int]=[216, 216],
colorMap: str="nipy_spectral") -> None:
colorMap: str="viridis") -> None:
"""
Call the class with these arguments
Expand Down Expand Up @@ -166,7 +168,7 @@ def __init__(self,

def make_units(self) -> None:
"""(Re)make the units list"""
self.units = [get(var, UNITS, "UnknownVar") for var in self.vars]
self.units = {var: get(var, UNITS, "UnknownVar") for var in self.vars}

def make_meta(self) -> None:
"""
Expand All @@ -180,7 +182,7 @@ def make_meta(self) -> None:
"xvar": self.xvar,
"yvar": self.yvar,
"zvar": self.zvar,
"vars": self.vars,
"vars": list(self.vars),
"numPoints": self.numPoints,
"colorMap": self.colorMap,
"units": self.units
Expand All @@ -201,17 +203,20 @@ def add_column(self, variables: Union[List[Text], Text]) -> None:
Paramaters:
variable (Union[List[Text],Text]): What variable(s) to add
"""
if type(variables) is not list:
variables = [variables]

for var in variables:
try:
assert var not in self.vars
except AssertionError:
print("Cannot add column {0}: already in frame".format(var))
return None
raise ValueError(
"Cannot add column {0}: already in frame".format(var))

try:
assert var != "V"
except AssertionError:
print("Cannot add Volume as a column just yet, TODO")
return None
raise TypeError("Cannot add Volume as a column just yet, TODO")

self.vars += variables
buffer = dict([])
Expand All @@ -227,7 +232,6 @@ def add_column(self, variables: Union[List[Text], Text]) -> None:
for index, row in self.data.iterrows():
for key in newcols:
get(key, buffer).append(get(key, newcols)(row))

for key in newcols:
self.data[key] = pd.Series(buffer[key], index=self.data.index)
self.make_units()
Expand All @@ -248,29 +252,51 @@ def clean(self) -> None:
if "V" in self.vars:
self.data = self.data[self.data["V"] >= 0.1]

def write_data(self, path: str) -> None:
def write_data(self, path: str, **kwargs) -> None:
"""
Does what it says on the tin. Makes a CSV and JSON files and saves them to data/FluidName_X-xpoints_Y-ypoints_Z.
Parameters:
path (str): path where file should be saved
filename (str): what to name the file
mode (str): How to name the file
- **default**: ``FluidName_X-xpoints_Y-ypoints_Z``
- **custom**: fully custom name.
- **dual**: default + custom (with custom appended).
"""
if bool(kwargs):
if "filename" not in kwargs and "mode" in kwargs and get("nameMode", kwargs) in {"dual", "custom"}:
raise TypeError(
"When supplying {0} mode, filename is required; None given".format(get('nameMode', kwargs)))
nameMode = get("mode", kwargs, "default")
else:
nameMode = "default"

if nameMode != "custom":
default_string = self.fluid + "_" + "_".join([str(varname) + "-" + str(point) for (
varname, point) in zip([self.xvar, self.yvar], self.numPoints)] + [self.zvar])

if nameMode == "custom":
middle_string = get("filename", kwargs)

elif nameMode == "default":
middle_string = default_string

elif nameMode == "dual":
middle_string = default_string + "_" + str(get("filename", kwargs))

self.make_meta()
middle_string = self.fluid + "_" + "_".join([
str(varname) + "-" + str(point)
for (varname, point) in zip(self.vars[:1], self.numPoints)
] + [self.vars[2]])

self.data.to_csv(path + middle_string + ".csv", mode="w+", index=False)
with open(path + middle_string + ".json", mode="w+") as f:
json.dump(dict(self.meta), f)

def copy(self) -> T:
"""
Returns a copy of itself
"""
return copy.deepcopy(self)
return deepcopy(self)


class CSVFluid():
Expand Down Expand Up @@ -319,34 +345,43 @@ def __init__(self, pathToFile: str) -> None:
self.xvar = self.meta["xvar"]
self.yvar = self.meta["yvar"]
self.zvar = self.meta["zvar"]
self.vars = self.meta["vars"]
self.vars = list(self.meta["vars"])
self.fluid = self.meta["fluid"]
self.colorMap = self.meta["colorMap"]
self.numPoints = self.meta["numPoints"]
self.units = self.meta["units"]

def changeOrder(self, order: List[int]) -> None:
def changeOrder(self, order: List[Text]) -> None:
"""
Changes order of the columns:
Parameters:
order (List[int]): is a permutation on {0,1,2}.
order (List[Text]): is a permutation of length 3 on vars.
"""
try:
assert set(order).issubset(set(self.vars))
except AssertionError:
raise ValueError("All entries order MUST be in vars")

try:
assert order in permutations(self.vars, 3)
except AssertionError:
raise ValueError(
"Order of columns must be a permutation of columns of data")

self.data = self.data[[self.vars[i] for i in order]]
self.vars = [self.vars[i] for i in order]
self.xvar = self.vars[0]
self.yvar = self.vars[1]
self.zvar = self.vars[2]
self.units = [self.units[i] for i in order]
self.data = self.data[list(
order) + [var for var in set(self.vars) if var not in set(order)]]
self.xvar = order[0]
self.yvar = order[1]
self.zvar = order[2]

def copy(self) -> C:
"""
Returns a copy of itself
"""
return copy.deepcopy(self)
return deepcopy(self)


def fluid_plot(fluid: Union[CSVFluid, ThermoFluid]) -> None:
Expand Down
9 changes: 4 additions & 5 deletions ThermoPyle/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@ CoolProp>=6.0.0
matplotlib>=2.0.0
numpy>=1.12.0
pandas>=0.19.2
pyrsistent>=0.12.0
arrow>=0.10.0
cytoolz>=0.8.2
mpld3>=0.3
sphinx>=1.5.1
sphinx_bootstrap_theme>=0.4.13
sphinx-autobuild>=0.6.0
recommonmark>=0.4.0
pypandoc>=1.3.3
typing>=3.5.3.0
pyrsistent>=0.12.0
arrow>=0.10.0
sphinx-autodoc-typehints>=1.1.0
cytoolz>=0.8.2
mpld3>=0.3
16 changes: 11 additions & 5 deletions ThermoPyle/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def readme():

setup(
name='ThermoPyle',
version='0.5.3',
version='0.5.4',
description='Creation of Thermodynamic Surfaces using CoolProp',
long_description=readme(),
url='https://github.com/nick5435/thermo-bridge',
Expand All @@ -20,11 +20,17 @@ def readme():
zip_safe=False,
install_requires=[
'CoolProp>=6.0.0', 'matplotlib>=2.0.0', 'numpy>=1.12.0',
'pandas>=0.19.2', 'sphinx>=1.5.1', 'sphinx_bootstrap_theme>=0.4.13',
'sphinx-autobuild>=0.6.0', 'sphinx-autodoc-typehints>=1.1.0',
'recommonmark>=0.4.0', 'pypandoc>=1.3.3', 'typing>=3.5.3.0',
'pyrsistent>=0.12.0', 'arrow>=0.10.0', 'cytoolz>=0.8.2'
'pandas>=0.19.2',
'pyrsistent>=0.12.0', 'arrow>=0.10.0', 'cytoolz>=0.8.2',
'mpld3>=0.3'
],
extra_require={
'dev': [
'sphinx>=1.5.1', 'sphinx_bootstrap_theme>=0.4.13',
'sphinx-autobuild>=0.6.0', 'sphinx-autodoc-typehints>=1.1.0',
'recommonmark>=0.4.0', 'pypandoc>=1.3.3',
]
},
python_requires='>=3.5.0',
include_package_data=True,
classifiers=[
Expand Down
2 changes: 1 addition & 1 deletion autobuild.bat
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
@ECHO ON
sphinx-autobuild docs/source docs/build/html
sphinx-autobuild docs/source docs/build/html

0 comments on commit 82a6495

Please sign in to comment.