Skip to content

Commit 52b979f

Browse files
committed
Add D4xdata.save_D4x_correl()
1 parent 12b3b7b commit 52b979f

19 files changed

+823
-553
lines changed

D47crunch/__init__.py

Lines changed: 71 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3085,6 +3085,34 @@ def plot_bulk_compositions(
30853085
ppl.close(fig)
30863086

30873087

3088+
def save_D4x_correl(
3089+
self,
3090+
samples = None,
3091+
dir = 'output',
3092+
filename = None,
3093+
):
3094+
'''
3095+
Save D4x values along with their SE and correlation matrix.
3096+
'''
3097+
if samples is None:
3098+
samples = sorted([s for s in self.unknowns])
3099+
3100+
out = [['Sample']] + [[s] for s in samples]
3101+
out[0] += [f'D{self._4x}', f'D{self._4x}_SE', f'D{self._4x}_correl']
3102+
for k,s in enumerate(samples):
3103+
out[k+1] += [f'{self.samples[s][f"D{self._4x}"]:.4f}', f'{self.samples[s][f"SE_D{self._4x}"]:.4f}']
3104+
for s2 in samples:
3105+
out[k+1] += [f'{self.sample_D4x_correl(s,s2):.4f}']
3106+
3107+
if not os.path.exists(dir):
3108+
os.makedirs(dir)
3109+
if filename is None:
3110+
filename = f'D{self._4x}_correl.csv'
3111+
with open(f'{dir}/{filename}', 'w') as fid:
3112+
fid.write(make_csv(out))
3113+
3114+
3115+
30883116

30893117
class D47data(D4xdata):
30903118
'''
@@ -3175,7 +3203,7 @@ def D47fromTeq(self, fCo2eqD47 = 'petersen', priority = 'new'):
31753203
if priority != 'old' or s not in self.Nominal_D47:
31763204
self.Nominal_D47[s] = foo[s]
31773205

3178-
3206+
save_D47_correl = D4xdata.save_D4x_correl
31793207

31803208

31813209
class D48data(D4xdata):
@@ -3228,6 +3256,7 @@ def __init__(self, l = [], **kwargs):
32283256
'''
32293257
D4xdata.__init__(self, l = l, mass = '48', **kwargs)
32303258

3259+
save_D48_correl = D4xdata.save_D4x_correl
32313260

32323261

32333262
class _SessionPlot():
@@ -3253,6 +3282,8 @@ def _cli(
32533282
):
32543283
"""
32553284
Process raw D47 data and return standardized results.
3285+
3286+
See [](https://mdaeron.github.io/D47crunch/#3-command-line-interface-cli) for more details.
32563287
"""
32573288

32583289
data = D47data()
@@ -3270,21 +3301,24 @@ def _cli(
32703301

32713302
if anchors != 'none':
32723303
anchors = read_csv(anchors)
3273-
data.Nominal_d13C_VPDB = {
3274-
_['Sample']: _['d13C_VPDB']
3275-
for _ in anchors
3276-
if 'd13C_VPDB' in _
3277-
}
3278-
data.Nominal_d18O_VPDB = {
3279-
_['Sample']: _['d18O_VPDB']
3280-
for _ in anchors
3281-
if 'd18O_VPDB' in _
3282-
}
3283-
data.Nominal_D4x = {
3284-
_['Sample']: _['D47']
3285-
for _ in anchors
3286-
if 'D47' in _
3287-
}
3304+
if len([_ for _ in anchors if 'd13C_VPDB' in _]):
3305+
data.Nominal_d13C_VPDB = {
3306+
_['Sample']: _['d13C_VPDB']
3307+
for _ in anchors
3308+
if 'd13C_VPDB' in _
3309+
}
3310+
if len([_ for _ in anchors if 'd18O_VPDB' in _]):
3311+
data.Nominal_d18O_VPDB = {
3312+
_['Sample']: _['d18O_VPDB']
3313+
for _ in anchors
3314+
if 'd18O_VPDB' in _
3315+
}
3316+
if len([_ for _ in anchors if 'D47' in _]):
3317+
data.Nominal_D4x = {
3318+
_['Sample']: _['D47']
3319+
for _ in anchors
3320+
if 'D47' in _
3321+
}
32883322

32893323
data.refresh()
32903324
data.wg()
@@ -3294,6 +3328,7 @@ def _cli(
32943328
data.plot_residuals(dir = output_dir, filename = 'D47_residuals.pdf', kde = True)
32953329
data.plot_bulk_compositions(dir = output_dir + '/bulk_compositions')
32963330
data.plot_sessions(dir = output_dir)
3331+
data.save_D47_correl(dir = output_dir)
32973332

32983333
if not run_D48:
32993334
data.table_of_samples(dir = output_dir)
@@ -3303,26 +3338,30 @@ def _cli(
33033338

33043339
if run_D48:
33053340
data2 = D48data()
3341+
print(rawdata)
33063342
data2.read(rawdata)
33073343

33083344
data2 = D48data([r for r in data2 if r['UID'] not in exclude_uid and r['Sample'] not in exclude_sample])
33093345

33103346
if anchors != 'none':
3311-
data2.Nominal_d13C_VPDB = {
3312-
_['Sample']: _['d13C_VPDB']
3313-
for _ in anchors
3314-
if 'd13C_VPDB' in _
3315-
}
3316-
data2.Nominal_d18O_VPDB = {
3317-
_['Sample']: _['d18O_VPDB']
3318-
for _ in anchors
3319-
if 'd18O_VPDB' in _
3320-
}
3321-
data2.Nominal_D4x = {
3322-
_['Sample']: _['D48']
3323-
for _ in anchors
3324-
if 'D48' in _
3325-
}
3347+
if len([_ for _ in anchors if 'd13C_VPDB' in _]):
3348+
data2.Nominal_d13C_VPDB = {
3349+
_['Sample']: _['d13C_VPDB']
3350+
for _ in anchors
3351+
if 'd13C_VPDB' in _
3352+
}
3353+
if len([_ for _ in anchors if 'd18O_VPDB' in _]):
3354+
data2.Nominal_d18O_VPDB = {
3355+
_['Sample']: _['d18O_VPDB']
3356+
for _ in anchors
3357+
if 'd18O_VPDB' in _
3358+
}
3359+
if len([_ for _ in anchors if 'D48' in _]):
3360+
data2.Nominal_D4x = {
3361+
_['Sample']: _['D48']
3362+
for _ in anchors
3363+
if 'D48' in _
3364+
}
33263365

33273366
data2.refresh()
33283367
data2.wg()
@@ -3332,6 +3371,7 @@ def _cli(
33323371
data2.plot_sessions(dir = output_dir)
33333372
data2.plot_residuals(dir = output_dir, filename = 'D48_residuals.pdf', kde = True)
33343373
data2.plot_distribution_of_analyses(dir = output_dir)
3374+
data2.save_D48_correl(dir = output_dir)
33353375

33363376
table_of_analyses(data, data2, dir = output_dir)
33373377
table_of_samples(data, data2, dir = output_dir)

changelog.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
# Changelog
22

3+
### New feature
4+
* New method `D4xdata.save_D4x_correl()` to export a list of Δ<sub>4x</sub> values along with their SE and correlation matrix.
5+
6+
### Other changes
7+
* The CLI now also calls `save_D4x_correl()`.
8+
39
## v2.2.1
410
*Released on 2023-07-20*
511

612
### New feature
7-
813
* The CLI now processes Δ48 as well as Δ47 data, thanks to the `--D48` option.
914

1015

0 commit comments

Comments
 (0)