Skip to content

summary

summary

Summary

Generates reports and exports results from the project.

This class collects and presents all relevant information about the fitted model, experiments, and analysis results.

Source code in src/easydiffraction/summary/summary.py
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
class Summary:
    """
    Generates reports and exports results from the project.

    This class collects and presents all relevant information about the
    fitted model, experiments, and analysis results.
    """

    def __init__(self, project: object) -> None:
        """
        Initialize the summary with a reference to the project.

        Parameters
        ----------
        project : object
            The Project instance this summary belongs to.
        """
        self.project = project

    # ------------------------------------------
    #  Report Generation
    # ------------------------------------------

    def show_report(self) -> None:
        """Print a full project report covering all sections."""
        self.show_project_info()
        self.show_crystallographic_data()
        self.show_experimental_data()
        self.show_fitting_details()

    def show_project_info(self) -> None:
        """Print the project title and description."""
        console.section('Project info')

        console.paragraph('Title')
        console.print(self.project.info.title)

        if self.project.info.description:
            console.paragraph('Description')
            # log.print('\n'.join(wrap(self.project.info.description,
            # width=80)))
            # TODO: Fix the following lines
            # Ensure description wraps with explicit newlines for tests
            desc_lines = wrap(self.project.info.description, width=60)
            # Use plain print to avoid Left padding that would break
            # newline adjacency checks
            print('\n'.join(desc_lines))

    def show_crystallographic_data(self) -> None:
        """Print crystallographic data for all phases."""
        console.section('Crystallographic data')

        for model in self.project.structures.values():
            console.paragraph('Phase datablock')
            console.print(f'🧩 {model.name}')

            console.paragraph('Space group')
            console.print(model.space_group.name_h_m.value)

            console.paragraph('Cell parameters')
            columns_headers = ['Parameter', 'Value']
            columns_alignment: List[str] = ['left', 'right']
            cell_data = [
                [p.name.replace('length_', '').replace('angle_', ''), f'{p.value:.5f}']
                for p in model.cell.parameters
            ]
            render_table(
                columns_headers=columns_headers,
                columns_alignment=columns_alignment,
                columns_data=cell_data,
            )

            console.paragraph('Atom sites')
            columns_headers = [
                'label',
                'type',
                'x',
                'y',
                'z',
                'occ',
                'Biso',
            ]
            columns_alignment = [
                'left',
                'left',
                'right',
                'right',
                'right',
                'right',
                'right',
            ]
            atom_table = []
            for site in model.atom_sites:
                atom_table.append([
                    site.label.value,
                    site.type_symbol.value,
                    f'{site.fract_x.value:.5f}',
                    f'{site.fract_y.value:.5f}',
                    f'{site.fract_z.value:.5f}',
                    f'{site.occupancy.value:.5f}',
                    f'{site.b_iso.value:.5f}',
                ])
            render_table(
                columns_headers=columns_headers,
                columns_alignment=columns_alignment,
                columns_data=atom_table,
            )

    def show_experimental_data(self) -> None:
        """Print experimental data for all experiments."""
        console.section('Experiments')

        for expt in self.project.experiments.values():
            console.paragraph('Experiment datablock')
            console.print(f'🔬 {expt.name}')

            console.paragraph('Experiment type')
            console.print(
                f'{expt.type.sample_form.value}, '
                f'{expt.type.radiation_probe.value}, '
                f'{expt.type.beam_mode.value}'
            )

            if 'instrument' in expt._public_attrs():
                if 'setup_wavelength' in expt.instrument._public_attrs():
                    console.paragraph('Wavelength')
                    console.print(f'{expt.instrument.setup_wavelength.value:.5f}')
                if 'calib_twotheta_offset' in expt.instrument._public_attrs():
                    console.paragraph('2θ offset')
                    console.print(f'{expt.instrument.calib_twotheta_offset.value:.5f}')

            if 'peak_profile_type' in expt._public_attrs():
                console.paragraph('Profile type')
                console.print(expt.peak_profile_type)

            if 'peak' in expt._public_attrs():
                if 'broad_gauss_u' in expt.peak._public_attrs():
                    console.paragraph('Peak broadening (Gaussian)')
                    columns_headers = ['Parameter', 'Value']
                    columns_alignment = ['left', 'right']
                    columns_data = [
                        ['U', f'{expt.peak.broad_gauss_u.value:.5f}'],
                        ['V', f'{expt.peak.broad_gauss_v.value:.5f}'],
                        ['W', f'{expt.peak.broad_gauss_w.value:.5f}'],
                    ]
                    render_table(
                        columns_headers=columns_headers,
                        columns_alignment=columns_alignment,
                        columns_data=columns_data,
                    )
                if 'broad_lorentz_x' in expt.peak._public_attrs():
                    console.paragraph('Peak broadening (Lorentzian)')
                    # TODO: Some headers capitalize, some don't -
                    #  be consistent
                    columns_headers = ['Parameter', 'Value']
                    columns_alignment = ['left', 'right']
                    columns_data = [
                        ['X', f'{expt.peak.broad_lorentz_x.value:.5f}'],
                        ['Y', f'{expt.peak.broad_lorentz_y.value:.5f}'],
                    ]
                    render_table(
                        columns_headers=columns_headers,
                        columns_alignment=columns_alignment,
                        columns_data=columns_data,
                    )

    def show_fitting_details(self) -> None:
        """Print fitting details including engines and metrics."""
        console.section('Fitting')

        console.paragraph('Calculation engine')
        for expt in self.project.experiments.values():
            console.print(f'  {expt.name}: {expt.calculator_type}')

        console.paragraph('Minimization engine')
        console.print(self.project.analysis.current_minimizer)

        console.paragraph('Fit quality')
        columns_headers = ['metric', 'value']
        columns_alignment = ['left', 'right']
        fit_metrics = [
            [
                'Goodness-of-fit (reduced χ²)',
                f'{self.project.analysis.fit_results.reduced_chi_square:.2f}',
            ]
        ]
        render_table(
            columns_headers=columns_headers,
            columns_alignment=columns_alignment,
            columns_data=fit_metrics,
        )

    # ------------------------------------------
    #  Exporting
    # ------------------------------------------

    def as_cif(self) -> str:
        """Export fitted data and analysis results as CIF."""
        from easydiffraction.io.cif.serialize import summary_to_cif

        return summary_to_cif(self)

__init__(project)

Initialize the summary with a reference to the project.

Parameters:

Name Type Description Default
project object

The Project instance this summary belongs to.

required
Source code in src/easydiffraction/summary/summary.py
19
20
21
22
23
24
25
26
27
28
def __init__(self, project: object) -> None:
    """
    Initialize the summary with a reference to the project.

    Parameters
    ----------
    project : object
        The Project instance this summary belongs to.
    """
    self.project = project

as_cif()

Export fitted data and analysis results as CIF.

Source code in src/easydiffraction/summary/summary.py
207
208
209
210
211
def as_cif(self) -> str:
    """Export fitted data and analysis results as CIF."""
    from easydiffraction.io.cif.serialize import summary_to_cif

    return summary_to_cif(self)

show_crystallographic_data()

Print crystallographic data for all phases.

Source code in src/easydiffraction/summary/summary.py
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
def show_crystallographic_data(self) -> None:
    """Print crystallographic data for all phases."""
    console.section('Crystallographic data')

    for model in self.project.structures.values():
        console.paragraph('Phase datablock')
        console.print(f'🧩 {model.name}')

        console.paragraph('Space group')
        console.print(model.space_group.name_h_m.value)

        console.paragraph('Cell parameters')
        columns_headers = ['Parameter', 'Value']
        columns_alignment: List[str] = ['left', 'right']
        cell_data = [
            [p.name.replace('length_', '').replace('angle_', ''), f'{p.value:.5f}']
            for p in model.cell.parameters
        ]
        render_table(
            columns_headers=columns_headers,
            columns_alignment=columns_alignment,
            columns_data=cell_data,
        )

        console.paragraph('Atom sites')
        columns_headers = [
            'label',
            'type',
            'x',
            'y',
            'z',
            'occ',
            'Biso',
        ]
        columns_alignment = [
            'left',
            'left',
            'right',
            'right',
            'right',
            'right',
            'right',
        ]
        atom_table = []
        for site in model.atom_sites:
            atom_table.append([
                site.label.value,
                site.type_symbol.value,
                f'{site.fract_x.value:.5f}',
                f'{site.fract_y.value:.5f}',
                f'{site.fract_z.value:.5f}',
                f'{site.occupancy.value:.5f}',
                f'{site.b_iso.value:.5f}',
            ])
        render_table(
            columns_headers=columns_headers,
            columns_alignment=columns_alignment,
            columns_data=atom_table,
        )

show_experimental_data()

Print experimental data for all experiments.

Source code in src/easydiffraction/summary/summary.py
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
def show_experimental_data(self) -> None:
    """Print experimental data for all experiments."""
    console.section('Experiments')

    for expt in self.project.experiments.values():
        console.paragraph('Experiment datablock')
        console.print(f'🔬 {expt.name}')

        console.paragraph('Experiment type')
        console.print(
            f'{expt.type.sample_form.value}, '
            f'{expt.type.radiation_probe.value}, '
            f'{expt.type.beam_mode.value}'
        )

        if 'instrument' in expt._public_attrs():
            if 'setup_wavelength' in expt.instrument._public_attrs():
                console.paragraph('Wavelength')
                console.print(f'{expt.instrument.setup_wavelength.value:.5f}')
            if 'calib_twotheta_offset' in expt.instrument._public_attrs():
                console.paragraph('2θ offset')
                console.print(f'{expt.instrument.calib_twotheta_offset.value:.5f}')

        if 'peak_profile_type' in expt._public_attrs():
            console.paragraph('Profile type')
            console.print(expt.peak_profile_type)

        if 'peak' in expt._public_attrs():
            if 'broad_gauss_u' in expt.peak._public_attrs():
                console.paragraph('Peak broadening (Gaussian)')
                columns_headers = ['Parameter', 'Value']
                columns_alignment = ['left', 'right']
                columns_data = [
                    ['U', f'{expt.peak.broad_gauss_u.value:.5f}'],
                    ['V', f'{expt.peak.broad_gauss_v.value:.5f}'],
                    ['W', f'{expt.peak.broad_gauss_w.value:.5f}'],
                ]
                render_table(
                    columns_headers=columns_headers,
                    columns_alignment=columns_alignment,
                    columns_data=columns_data,
                )
            if 'broad_lorentz_x' in expt.peak._public_attrs():
                console.paragraph('Peak broadening (Lorentzian)')
                # TODO: Some headers capitalize, some don't -
                #  be consistent
                columns_headers = ['Parameter', 'Value']
                columns_alignment = ['left', 'right']
                columns_data = [
                    ['X', f'{expt.peak.broad_lorentz_x.value:.5f}'],
                    ['Y', f'{expt.peak.broad_lorentz_y.value:.5f}'],
                ]
                render_table(
                    columns_headers=columns_headers,
                    columns_alignment=columns_alignment,
                    columns_data=columns_data,
                )

show_fitting_details()

Print fitting details including engines and metrics.

Source code in src/easydiffraction/summary/summary.py
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
def show_fitting_details(self) -> None:
    """Print fitting details including engines and metrics."""
    console.section('Fitting')

    console.paragraph('Calculation engine')
    for expt in self.project.experiments.values():
        console.print(f'  {expt.name}: {expt.calculator_type}')

    console.paragraph('Minimization engine')
    console.print(self.project.analysis.current_minimizer)

    console.paragraph('Fit quality')
    columns_headers = ['metric', 'value']
    columns_alignment = ['left', 'right']
    fit_metrics = [
        [
            'Goodness-of-fit (reduced χ²)',
            f'{self.project.analysis.fit_results.reduced_chi_square:.2f}',
        ]
    ]
    render_table(
        columns_headers=columns_headers,
        columns_alignment=columns_alignment,
        columns_data=fit_metrics,
    )

show_project_info()

Print the project title and description.

Source code in src/easydiffraction/summary/summary.py
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
def show_project_info(self) -> None:
    """Print the project title and description."""
    console.section('Project info')

    console.paragraph('Title')
    console.print(self.project.info.title)

    if self.project.info.description:
        console.paragraph('Description')
        # log.print('\n'.join(wrap(self.project.info.description,
        # width=80)))
        # TODO: Fix the following lines
        # Ensure description wraps with explicit newlines for tests
        desc_lines = wrap(self.project.info.description, width=60)
        # Use plain print to avoid Left padding that would break
        # newline adjacency checks
        print('\n'.join(desc_lines))

show_report()

Print a full project report covering all sections.

Source code in src/easydiffraction/summary/summary.py
34
35
36
37
38
39
def show_report(self) -> None:
    """Print a full project report covering all sections."""
    self.show_project_info()
    self.show_crystallographic_data()
    self.show_experimental_data()
    self.show_fitting_details()