Skip to content

Commit e643104

Browse files
Merge pull request #1 from Loop3D/pythonic
Pythonic
2 parents 0fdbc1e + f7652f5 commit e643104

File tree

7 files changed

+490
-13
lines changed

7 files changed

+490
-13
lines changed

.github/workflows/publish_conda.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
name: publish_conda
22

33
on:
4-
push
4+
release:
5+
types: [published]
56
jobs:
67
conda-deploy:
78
name: Uploading to Loop3d for python ${{ matrix.os }})

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
__pycache__
22
*.swp
33
*.swo
4+
LoopProjectFile.egg-info/*

LoopProjectFile/LoopProjectFile.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -449,8 +449,23 @@ def ConvertDataFrame(df,dtype):
449449
else:
450450
raise NotADataFrame
451451

452+
def CheckFileIsLoopProjectFile(filename, verbose=False):
453+
"""
454+
Check that the file is a valid Loop Project File
455+
"""
456+
fileResp = OpenProjectFile(filename, readOnly=True, verbose=verbose)
457+
if fileResp["errorFlag"]:
458+
valid = False
459+
print('Project file is not a LoopProjectFile')
460+
else:
461+
rootgrp = fileResp["root"]
462+
rootgrp.close()
463+
valid = True
464+
return valid
465+
466+
452467
def ConvertToDataFrame(data,loopCompoundType):
453468
columns = list(loopCompoundType.names)
454469
df = pandas.DataFrame.from_records(data, columns=columns)
455470
df = df.applymap(lambda x:x.decode() if isinstance(x,bytes) else x)
456-
return df
471+
return df

LoopProjectFile/LoopProjectFileUtils.py

Lines changed: 64 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def GetGroup(node,groupName,verbose=False):
2626
if verbose: print(errStr)
2727
return {"errorFlag":True,"errorString":errStr}
2828

29-
def ElementFromCsv(loopFilename,importFilename,element,loopCompoundType):
29+
def ElementFromDataframe(loopFilename,df,element,loopCompoundType):
3030
"""
3131
**ElementFromCsv** - Imports one element of the loop project file
3232
from a csv file into the project file
@@ -46,22 +46,51 @@ def ElementFromCsv(loopFilename,importFilename,element,loopCompoundType):
4646
-------
4747
4848
"""
49-
if not os.path.isfile(importFilename):
50-
print(importFilename,'does not exist')
49+
if isinstance(df,pandas.DataFrame) == False:
50+
print('not a dataframedoes not exist')
5151
return
5252
if not os.path.isfile(loopFilename):
5353
print(loopFilename,'does not exist. Try LoopProjectFile.CreateBasic first')
5454
return
55-
df = pandas.read_csv(importFilename)
5655
if len(df.columns) != len(loopCompoundType):
57-
print('In ',importFilename,'columns do not match compound type')
56+
print('In dataframe columns do not match compound type')
5857
print(' Dataframe:',df.columns,' does not match\n Compound type:',loopCompoundType.names)
5958
return
6059
struct = LoopProjectFile.ConvertDataFrame(df,loopCompoundType)
6160
resp = LoopProjectFile.Set(loopFilename,element,data=struct)
6261
if resp['errorFlag']:
6362
print(resp['errorString'])
6463

64+
def ElementFromCsv(loopFilename,importFilename,element,loopCompoundType):
65+
"""
66+
**ElementFromCsv** - Imports one element of the loop project file
67+
from a csv file into the project file
68+
69+
Parameters
70+
----------
71+
loopFilename: string
72+
The filename of the loop project file
73+
importFilename: string
74+
The filename of the csv file containing the element data
75+
element: string
76+
The name of the element to extract
77+
loopCompoundType: numpy.compoundType
78+
The numpy data structure that the element is stored in
79+
80+
Returns
81+
-------
82+
83+
"""
84+
if not os.path.isfile(importFilename):
85+
print(importFilename,'does not exist')
86+
return
87+
if not os.path.isfile(loopFilename):
88+
print(loopFilename,'does not exist. Try LoopProjectFile.CreateBasic first')
89+
return
90+
df = pandas.read_csv(importFilename)
91+
ElementFromDataframe(loopFilename,df,element,loopCompoundType)
92+
93+
6594
def FromCsv(loopFilename,importPath,overwrite=False):
6695
"""
6796
**FromCsv** - Imports all elements of the loop project file
@@ -146,7 +175,7 @@ def FromCsv(loopFilename,importPath,overwrite=False):
146175
print(' Importing from',str(importPath)+'eventRel.csv','into project file')
147176
ElementFromCsv(loopFilename,importPath+'eventRel.csv','eventRelationships',LoopProjectFile.eventRelationshipType)
148177

149-
def ElementToCsv(loopFilename,outputFilename,element,loopCompoundType):
178+
def ElementToDataframe(loopFilename,element,loopCompoundType):
150179
"""
151180
**ElementToCsv** - Exports one element of the loop project file
152181
to a csv file outputFilename
@@ -155,8 +184,6 @@ def ElementToCsv(loopFilename,outputFilename,element,loopCompoundType):
155184
----------
156185
loopFilename: string
157186
The filename of the loop project file
158-
outputFilename: string
159-
The filename of the csv file which will contain the element data
160187
element: string
161188
The name of the element to extract
162189
loopCompoundType: numpy.compoundType
@@ -169,12 +196,40 @@ def ElementToCsv(loopFilename,outputFilename,element,loopCompoundType):
169196
resp = LoopProjectFile.Get(loopFilename,element)
170197
if resp['errorFlag']:
171198
print(resp['errorString'])
199+
return None
172200
else:
173201
columns = list(loopCompoundType.names)
174202
df = pandas.DataFrame.from_records(resp['value'],columns=columns)
203+
for name in columns:
204+
df[name] = df[name].astype(loopCompoundType[name])
175205
df = df.applymap(lambda x:x.decode() if isinstance(x,bytes) else x)
176-
df.set_index(columns[0],inplace=True)
206+
# df.set_index(columns[0],inplace=True)
207+
return df#.to_csv(outputFilename)
208+
209+
def ElementToCsv(loopFilename,outputFilename,element,loopCompoundType):
210+
"""
211+
**ElementToCsv** - Exports one element of the loop project file
212+
to a csv file outputFilename
213+
214+
Parameters
215+
----------
216+
loopFilename: string
217+
The filename of the loop project file
218+
outputFilename: string
219+
The filename of the csv file which will contain the element data
220+
element: string
221+
The name of the element to extract
222+
loopCompoundType: numpy.compoundType
223+
The numpy data structure that the element is stored in
224+
225+
Returns
226+
-------
227+
228+
"""
229+
df = ElementToDataframe(loopFilename,element,loopCompoundType)
230+
if df:
177231
df.to_csv(outputFilename)
232+
178233

179234
def ToCsv(loopFilename,outputPath):
180235
"""

LoopProjectFile/__init__.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
faultObservationType, foldObservationType, foliationObservationType, discontinuityObservationType, \
44
stratigraphicLayerType, stratigraphicObservationType, contactObservationType, \
55
eventRelationshipType, drillholeObservationType, drillholeDescriptionType, \
6-
ConvertDataFrame, ConvertToDataFrame, EventType, EventRelationshipType
6+
ConvertDataFrame, ConvertToDataFrame, EventType, EventRelationshipType, CheckFileIsLoopProjectFile
77
from .Permutations import Event, perm, ApproxPerm, CalcPermutation, checkBrokenRules, checkBrokenEventRules
8-
from .LoopProjectFileUtils import ToCsv, FromCsv, ElementToCsv, ElementFromCsv
8+
from .LoopProjectFileUtils import ToCsv, FromCsv, ElementToCsv, ElementFromCsv, ElementToDataframe, ElementFromDataframe
99
from .Version import LoopVersion
10+
from .projectfile import ProjectFile

0 commit comments

Comments
 (0)